Image processing/etc.

model.eval()과 with torch.no_grad()

유니디니 2020. 7. 30. 19:03
728x90
반응형

pytorch 프레임워크에서 Train을 하거나 evaluation or Test를 수행할 때 자주 사용하게 되며, 함수간의 차이가 존재한다.

혼동하기 쉬우니 구분해서 사용하기 바랍니다.



  • model.eval() will notify all your layers that you are in eval mode, that way, batchnorm or dropout layers will work in eval mode instead of training mode.

  • torch.no_grad() impacts the autograd engine and deactivate it. It will reduce memory usage and speed up computations but you won’t be able to backprop (which you don’t want in an eval script).



내용을 보면 다음과 같다. 


model.eval()은 eval mode에서 사용할 것이라고 모든 레이어에 선언하는 것이며, 배치 정규화나 dropout layer들은 학습모드 대신에 eval mode로 작동한다. (eval 모드에서는 dropout은 비활성화, 배치 정규화는 학습에서 저장된 파라미터를 사용)


torch.no_grad()는 오차 역전파에 사용하는 계산량을 줄여서 처리 속도를 높인다.


참고자료  : https://discuss.pytorch.org/t/model-eval-vs-with-torch-no-grad/19615/24

반응형

'Image processing > etc.' 카테고리의 다른 글

numpy.testing.assert_allclose  (0) 2020.08.06
Annotation Tool 소개  (0) 2020.01.18