解決Pytorch 訓練與測試時爆顯存(out of memory)的問題
更新時間:2019年08月20日 13:45:37 作者:xiaoxifei
今天小編就為大家分享一篇解決Pytorch 訓練與測試時爆顯存(out of memory)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
Pytorch 訓練時有時候會因為加載的東西過多而爆顯存,有些時候這種情況還可以使用cuda的清理技術進行修整,當然如果模型實在太大,那也沒辦法。
使用torch.cuda.empty_cache()刪除一些不需要的變量代碼示例如下:
try:
output = model(input)
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()
else:
raise exception
測試的時候爆顯存有可能是忘記設置no_grad, 示例代碼如下:
with torch.no_grad():
for ii,(inputs,filelist) in tqdm(enumerate(test_loader), desc='predict'):
if opt.use_gpu:
inputs = inputs.cuda()
if len(inputs.shape) < 4:
inputs = inputs.unsqueeze(1)
else:
if len(inputs.shape) < 4:
inputs = torch.transpose(inputs, 1, 2)
inputs = inputs.unsqueeze(1)
以上這篇解決Pytorch 訓練與測試時爆顯存(out of memory)的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python利用K-Means算法實現(xiàn)對數(shù)據(jù)的聚類案例詳解
這篇文章主要介紹了python利用K-Means算法實現(xiàn)對數(shù)據(jù)的聚類,本文通過案例講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
python+pygame實現(xiàn)簡易五子棋小游戲的三種方式
這篇文章主要介紹了使用python實現(xiàn)簡易五子棋小游戲,文中提供了三種實現(xiàn)方式,解決思路和部分實現(xiàn)代碼,感興趣的朋友可以參考下2023-03-03
sklearn和keras的數(shù)據(jù)切分與交叉驗證的實例詳解
這篇文章主要介紹了sklearn和keras的數(shù)據(jù)切分與交叉驗證的實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06

