解決Pytorch 訓(xùn)練與測試時(shí)爆顯存(out of memory)的問題
Pytorch 訓(xùn)練時(shí)有時(shí)候會(huì)因?yàn)榧虞d的東西過多而爆顯存,有些時(shí)候這種情況還可以使用cuda的清理技術(shù)進(jìn)行修整,當(dāng)然如果模型實(shí)在太大,那也沒辦法。
使用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
測試的時(shí)候爆顯存有可能是忘記設(shè)置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 訓(xùn)練與測試時(shí)爆顯存(out of memory)的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用pylab庫實(shí)現(xiàn)畫線功能的方法詳解
這篇文章主要介紹了Python使用pylab庫實(shí)現(xiàn)畫線功能的方法,結(jié)合具體實(shí)例分析了Python使用pylab庫的相關(guān)函數(shù)實(shí)現(xiàn)畫線功能的操作技巧,并附帶說明了相關(guān)函數(shù)與參數(shù)功能,需要的朋友可以參考下2017-06-06
python?Sweetviz探索性數(shù)據(jù)可視化分析庫使用特征詳解
這篇文章主要為大家介紹了python?Sweetviz探索性數(shù)據(jù)可視化分析庫特征使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01

