现象:训练阶段未报out of memory,但是在验证阶段报错,并且第一个batch是正常运行的。
参考https://blog.csdn.net/xijuezhu8128/article/details/86594478得知是未取消梯度,造成了梯度的累加。
解决方法:在调用模型进行预测时,加上一句with torch.no_grad():
with torch.no_grad(): # 加上这句
outputs = model.forward(data)
现象:训练阶段未报out of memory,但是在验证阶段报错,并且第一个batch是正常运行的。
参考https://blog.csdn.net/xijuezhu8128/article/details/86594478得知是未取消梯度,造成了梯度的累加。
解决方法:在调用模型进行预测时,加上一句with torch.no_grad():
with torch.no_grad(): # 加上这句
outputs = model.forward(data)