解决Pytorch 训练与测试时爆显存(out of memory)的问题
Pytorch训练时有时候会因为加载的东西过多而爆显存,有些时候这种情况还可以使用cuda的清理技术进行修整,当然如果模型实在太大,那也没办法。
使用torch.cuda.empty_cache()删除一些不需要的变量代码示例如下:
try:
output=model(input)
exceptRuntimeErrorasexception:
if"outofmemory"instr(exception):
print("WARNING:outofmemory")
ifhasattr(torch.cuda,'empty_cache'):
torch.cuda.empty_cache()
else:
raiseexception
测试的时候爆显存有可能是忘记设置no_grad,示例代码如下:
withtorch.no_grad(): forii,(inputs,filelist)intqdm(enumerate(test_loader),desc='predict'): ifopt.use_gpu: inputs=inputs.cuda() iflen(inputs.shape)<4: inputs=inputs.unsqueeze(1) else: iflen(inputs.shape)<4: inputs=torch.transpose(inputs,1,2) inputs=inputs.unsqueeze(1)
以上这篇解决Pytorch训练与测试时爆显存(outofmemory)的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
10 红颜生日祝福语简短独特
11 爸爸爱你祝福语大全简短
12 三八简短祝福语给婆婆
13 周日徒步祝福语大全简短
14 很暧昧的简短祝福语
15 春季祝福语女生文案简短
16 入伙英文祝福语大全简短
17 青春告白祖国简短祝福语
18 下雨开工吉祥祝福语简短