113.|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).
【113.|113. 【torch】反向传播弃inplace操作】舍弃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
    1. 避免a.operate(**)不赋值的情况等等

    推荐阅读