700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python pytorch爆显存 内存溢出问题解决方法(总结)RuntimeError: CUDA out of memory.

python pytorch爆显存 内存溢出问题解决方法(总结)RuntimeError: CUDA out of memory.

时间:2021-07-29 23:42:08

相关推荐

python pytorch爆显存 内存溢出问题解决方法(总结)RuntimeError: CUDA out of memory.

问题描述

在运行python程序时,随运行时间增长,内存疯狂增加,直至运行内存爆满,出现以下错误:

RuntimeError: CUDA out of memory.

解决方法:

1.在模型验证和测试前加上【with torch.no_grad():】这句话。

with torch.no_grad(): # 加上这句话for ts_batch in train_gaussian_loader:output = self.model(ts_batch)

2.在出错的代码上添加释放内存的代码:

with torch.no_grad(): for ts_batch in train_gaussian_loader:try: # 加上这句话output = self.model(ts_batch) # 出错的地方except RuntimeError as exception: # 加上这句话if "out of memory" in str(exception): # 加上这句话print('WARNING: out of memory') # 加上这句话if hasattr(torch.cuda, 'empty_cache'): # 加上这句话torch.cuda.empty_cache() # 加上这句话

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。