我有以下工作代码。 我想在新行上打印所有第 5000 个项目的每一项损失 (% 5000) 但进度条应该继续 它的方式一劳永逸(应该只打印一次)并显示总进度。我该如何修改代码?
import torch
from math import tanh
from tqdm import tqdm
from time import sleep
batch, dim_in, dim_h, dim_out = 32, 100, 10, 1
input_X = torch.randn(batch, dim_in)
output_Y = torch.randn(batch, dim_out)
SGD_model = torch.nn.Sequential(
torch.nn.Linear(dim_in, dim_h),
torch.nn.Sigmoid(),
torch.nn.Linear(dim_h, dim_out),
)
loss_fn = torch.nn.MSELoss(reduction='sum')
rate_learning = 0.01
optim = torch.optim.SGD(SGD_model.parameters(), lr=rate_learning, momentum=0.1)
for values in tqdm(range(10000)):
if values % 5000 == 0:
print(values, loss.item())
pred_y = SGD_model(input_X)
loss = loss_fn(pred_y, output_Y)
optim.zero_grad()
loss.backward()
optim.step()
编辑三个进度条而不是一个: