本文主要是介绍pytorch 训练/测试模型时错误:RuntimeError: CUDA error: out of memory,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
方法1:batch-size设置多小
方法2:
with torch.no_grad():net = Net()out = net(imgs)
积累的梯度应该是会一直放在显存里的...用了这一行就会停止自动反向计算梯度
方法3:
设置cpu来加载模型:
model_path = 'path/to/model.pt' model = UNet(n_channels = 1, n_classes = 1) state_dict = torch.load(model_path,map_location='cpu') model.load_state_dict(state_dict) model.to(device)
这篇关于pytorch 训练/测试模型时错误:RuntimeError: CUDA error: out of memory的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!