Dev record

[Python] torch.no_grad() 역할

Barrer 2023. 8. 14. 10:02
반응형

pytorch를 사용하다 보면 torch.no_grad()라는 걸 종종 확인할 수 있다. 

torch.no_grad()의 역할은 딥러닝 모델을 test, 즉 inference를 할 때에는 backpropagation이 진행되지 않는다.

즉, gradient 연산을 전혀 사용하지 않는다. 그렇기 때문에 굳이 torch에서 auto gradient 연산이 돌아가게 켜 둘 필요가 없다.

따라서 위 pytorch 공식 문서에 나와있는 것 처럼 gradient 연산을 꺼서 불필요한 메모리 사용량을 줄이는 것이 목적이다.

https://pytorch.org/docs/stable/generated/torch.no_grad.html

 

no_grad — PyTorch 2.0 documentation

Shortcuts

pytorch.org

 

반응형