一、问题描述
如下图,笔者最近做一个Python深度学习项目时,输出迭代轮数时卡住:
如上图中所示,设置每10轮输出一次损失,相关参考代码如下:
python
# run style transfer
max_iter = 200 # 最大迭代次数
show_iter = 10 # 打印间隔
python
while n_iter[0] <= max_iter:
def closure():
optimizer.zero_grad()
out = extract_layers(loss_layers, opt_img, model=vgg)
layer_losses = [weights[a] * loss_fns[a](A, targets[a]) for a, A in enumerate(out)]
loss = sum(layer_losses)
loss.backward()
n_iter[0] += 1
# print loss
if n_iter[0] % show_iter == (show_iter - 1):
# show_iter = 10 9
# 9, 19, 29, 39, 49
print('Iteration: %d, loss: %f' % (n_iter[0] + 1, loss.data))
return losswhile n_iter[0] <= max_iter:
def closure():
optimizer.zero_grad()
out = extract_layers(loss_layers, opt_img, model=vgg)
layer_losses = [weights[a] * loss_fns[a](A, targets[a]) for a, A in enumerate(out)]
loss = sum(layer_losses)
loss.backward()
n_iter[0] += 1
# print loss
if n_iter[0] % show_iter == (show_iter - 1):
# show_iter = 10 9
# 9, 19, 29, 39, 49
print('Iteration: %d, loss: %f' % (n_iter[0] + 1, loss.data))
return loss
(代码参考书目:《Pytorch深度学习》,【印度】Vishnu Subramanian 著,王海玲 刘江峰 译,人民邮电出版社,北京,2019年4月)
二、解决方案
原本以为是代码运行过程中出现了问题,其实是Windows Powershell终端自身显示的问题。此时,按一次(注意仅一次 )Ctrl+C组合键,补全显示即可。(按两次会中止程序)。正确运行截图: