欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決Pytorch 訓(xùn)練與測(cè)試時(shí)爆顯存(out of memory)的問(wèn)題

 更新時(shí)間:2019年08月20日 13:45:37   作者:xiaoxifei  
今天小編就為大家分享一篇解決Pytorch 訓(xùn)練與測(cè)試時(shí)爆顯存(out of memory)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

Pytorch 訓(xùn)練時(shí)有時(shí)候會(huì)因?yàn)榧虞d的東西過(guò)多而爆顯存,有些時(shí)候這種情況還可以使用cuda的清理技術(shù)進(jìn)行修整,當(dāng)然如果模型實(shí)在太大,那也沒(méi)辦法。

使用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

測(cè)試的時(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)練與測(cè)試時(shí)爆顯存(out of memory)的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論