gpt4 book ai didi

python - tqdm 不显示栏

转载 作者:太空狗 更新时间:2023-10-29 23:56:58 30 4
gpt4 key购买 nike

我正在使用 tqdm 库,它没有给我进度条,而是给我如下所示的输出,它只是告诉我迭代:

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

知道为什么代码会这样做吗?我想这可能是因为我给它传递了一个发电机,但我又一次使用了过去有效的发电机。我以前从来没有真正搞砸过 tdqm 格式。以下是部分源代码:

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 需要知道将执行多少次迭代(总数)才能显示进度条。

你可以试试这个:

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)

关于python - tqdm 不显示栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48935907/

30 4 0