본문 바로가기
Error Note

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace ....

by ga.0_0.ga 2023. 3. 1.
728x90
반응형

- 전체 에러 문구

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [128, 32, 64, 64]], which is output 0 of ReluBackward0, 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).

 

 

신경망 모델을 학습할 때, 손실 함수를 2개 이상 사용하는 경우가 종종 있을겁니다. 이때 발생하기 쉬운 에러입니다.
원인은, 첫번째 손실함수 계산 이후 inplace=True로 설정되어 있던 텐서의  상태가 변형되어 역전파를 수행할 수 없는 상태가 되어버렸기 때문입니다. 

아래 두 가지 방법으로 해결할 수 있습니다.

1) .clone().detach() 함수를 통해 텐서를 복사해 사용해주면 됩니다. 

2) 각각의 손실 함수에 사용될 텐서들을 분리시켜주면 됩니다. (GPU RAM 사용량이 증가하는 단점이 있습니다.)

728x90
반응형

댓글