pytorch 验证模型阶段出现CUDA error: out of memory

现象:训练阶段未报out of memory,但是在验证阶段报错,并且第一个batch是正常运行的。

参考https://blog.csdn.net/xijuezhu8128/article/details/86594478得知是未取消梯度,造成了梯度的累加。

解决方法:在调用模型进行预测时,加上一句with torch.no_grad():

    with torch.no_grad():  # 加上这句
        outputs = model.forward(data)
发布了21 篇原创文章 · 获赞 15 · 访问量 4969

猜你喜欢

转载自blog.csdn.net/zdluffy/article/details/104715510