Python中Tqdm模块–进度条配置

  • Post author:
  • Post category:python




Tqdm模块——进度条配置

from time import sleep
from tqdm import tqdm
for i in tqdm(range(1000)):
    sleep(0.01)

在模型训练时的应用

dataloader的使用:

for data, target in tqdm(train_loader):

epochs = 10
for epoch in range(epochs):
    # train
    net.train() # net.train() net.eval() 来管理dropout、BN层方法(关闭梯度)
    running_loss = 0.0
    # ------------------------------------------------------------
    train_bar = tqdm(train_loader, file=sys.stdout)
    for step, data in enumerate(train_bar):
        images, labels = data
        optimizer.zero_grad()
        outputs = net(images.to(device))
        loss = loss_function(outputs, labels.to(device))
        loss.backward()
        optimizer.step()
        # ----------------------------------------------------------------------
        # print statistics
        running_loss += loss.item()
        train_bar.desc = "train epoch[{}/{}] loss:{:.3f}".format(epoch + 1,
                                                                 epochs,
                                                                 loss)



版权声明:本文为qq_42958831原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。