報錯:
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操作解決方案總結(jié):
因為新版本torch不再支持inplace操作赤兴,所以要更版本或改變代碼書寫風(fēng)格
調(diào)試過程中使用x.backward()確定產(chǎn)生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復(fù)制方式
a = b - c - 避免a.operate(**)不賦值的情況等等