相关文章推荐
没有腹肌的八宝粥  ·  c# ...·  1 年前    · 
谦和的拐杖  ·  android studio : ...·  2 年前    · 
精彩文章免费看

113. 【torch】反向传播弃inplace操作

报错:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [32, 85742]], which is output 0 of MmBackward, is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

舍弃inplace操作解决方案总结:
因为新版本torch不再支持inplace操作,所以要更版本或改变代码书写风格
调试过程中使用x.backward()确定产生inplace操作的位置,如某处的该语句不报错,则之前x操作均正确

  • 1)torch版本降为0.3.0(不成功)
  • 2)在inplace为True的时候,将其改为Flase,如drop()
  • 3)去掉所有的inplace操作
  • 4)换掉”-=”“+=”之类的操作,且用b=a代替a = a
    a -=c
    b = a.clone() # tensor复制方式
    a = b - c
  • 避免a.operate(**)不赋值的情况等等
  • 最后编辑于:2020-12-23 18:35