简体   繁体   English

tqdm 不显示条形图

[英]tqdm not showing bar

I'm using the tqdm library and it doesn't give me the progress bar, instead it gives me output that looks like this where it just tells me the iteration:我正在使用 tqdm 库,它没有给我进度条,而是给了我 output ,它看起来像这样,它只是告诉我迭代:

251it [01:44, 2.39it/s]

Any idea why the code would do this?知道为什么代码会这样做吗? I thought it was maybe because I was passing it a generator but then again I've used generators in the past that have worked.我想这可能是因为我给它传递了一个生成器,但我过去再次使用过有效的生成器。 I've never really messed with tdqm formatting before.我以前从来没有真正弄乱过 tdqm 格式。 Here is part of the source code:以下是部分源代码:

train_iter = zip(train_x, train_y) #train_x and train_y are just lists of elements
....
def train(train_iter, model, criterion, optimizer):
    model.train()
    total_loss = 0
    for x, y in tqdm(train_iter):
        x = x.transpose(0, 1)
        y = y.transpose(0, 1)
        optimizer.zero_grad()
        bloss = model.forward(x, y, criterion)   
        bloss.backward()
        torch.nn.utils.clip_grad_norm(model.parameters(), args.clip)
        optimizer.step()        
        total_loss += bloss.data[0]
    return total_loss

tqdm needs to known how many iters will be performed (the total amount) to show a progress bar. tqdm需要知道将执行多少迭代(总量)以显示进度条。

You can try this:你可以试试这个:

from tqdm import tqdm

train_x = range(100)
train_y = range(200)

train_iter = zip(train_x, train_y)

# Notice `train_iter` can only be iter over once, so i get `total` in this way.
total = min(len(train_x), len(train_y))

with tqdm(total=total) as pbar:
    for item in train_iter:
        # do something ...
        pbar.update(1)

Filling the "total" parameter with length worked for me.用长度填充“总”参数对我有用。 Now the progress bar appears.现在出现进度条。

from tqdm import tqdm

# ...
for imgs, targets in tqdm( train_dataloader, total=len(train_dataloader)):
   # ...

@Dogus's answer is more natural use of tqdm, but you need to make sure that your data loader (if it is a custom iterator) also exposes the len method. @Dogus 的答案是更自然地使用 tqdm,但您需要确保您的数据加载器(如果它是自定义迭代器)也公开len方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM