Python/PyTorch 공부

[PyTorch] Error: one of the variables needed for gradient computation has been modified by an inplace operation

AI 꿈나무 2021. 11. 25. 20:16
반응형

 

 파이토치 오류

https://discuss.pytorch.org/t/one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-cuda-floattensor-3-48-3-3-is-at-version-2-expected-version-1-instead/83241

 

One of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [3, 48,

When I was training the GAN, the first iteration worked well, and the second training probably caused an error on the Discriminator side. The details of the error are as follows. This dimension [3, 48, 3, 3] means the first layer of the Discriminator, but

discuss.pytorch.org

 

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation : [torch.cuda.FloatTensor [21, 1024, 1, 1]] is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

 

 위와 같은 error가 발생했다.

 

 inplace 연산때문에 gradient를 저장하고 있는 파라미터가 갱신되었고, 이전 파라미터에 대한 gradient를 loss를 사용하여 backward를 했기 때문이다.

 

 내가 연구하고 있는 모델은 generator, discriminator, backbone, classifier로 구성되어 있는데, 전체 loss를 계산하고 classifier를 update 한후에 backbone를 update해서 발생했다.

 

 즉, classifier가 update 되기 전 파라미터로 backbone을 위한 loss를 계산을 했고, 이 loss를 classifier가 업데이트 된 이후에 backbone에 대해 .backward()를 실행해서 발생한 오류.

 

 classifier를 업데이트하고, classifier에 대한 loss를 다시 구해서 backbone을 업데이트 해주면 된다.

 

 이거 때문에 엄청난 삽질을 ㅠㅠ

반응형