[PyTorch] model.eval() vs with torch.no_grad()

olxtar·2022년 3월 24일
0
# Let's start inference(Test!)
model.eval()
with torch.no_grad()
	for inputs, labels in testloader:
    ...
    

model.eval()은 뭐고
with torch.no_grad()는 뭘까?

참고 :
torch.autograd 소개(Fine-Tuning까지!)

no_grad()와 eval()의 차이점




with torch.no_grad()

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).




PyTorch에서 Tensor만들기
torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False)

PyTorch에서 Tensor를 만들때 requires_grad라는 Arguments가 있다!
requires_grad=True로 Tensor를 생성하면 torch.autograd, 즉 PyTorch의 자동 미분 엔진에게 해당 Tensor에 대한 모든 연산(Operation)을 추적해야 된다고 알려준다!

아래의 예제를 봐보자
ex)

import torch

a = torch.tensor( [2., 3.], requires_grad=True)
b = torch.tensor( [6., 4.], requires_grad=True)


a Tensor와 b Tensor를 연산하여 새로운 Tensor 만들기

Q=3a3b2Q=3a^3-b^2
Q = 3*a**3 - b**2


이제 a,b Tensor가 신경망(network)의 매개변수, 즉 weight라고 하고,
Q Tensor가 오차(error)라고 가정해보자. 신경망을 학습할 때, 아래와 같이 매개변수들에 대한 오차의 변화도(gradient)를 구해야 합니다. 즉,

Qa=9a2\frac{\partial Q}{\partial a}=9a^2
Qb=2b\frac{\partial Q}{\partial b}=-2b



Q에 대해서 .backward()를 호출 시, autograd는 이러한 변화도들을 계산하고 이를 각 텐서의 .grad 속성(attribute)에 저장합니다. 즉, Q, a, b Tensor에 Q에 대한 변화도(Gradient)를 계산해서 저장해놓는다!

이거 참고해서 이어서 작성하기
https://tutorials.pytorch.kr/beginner/blitz/autograd_tutorial.html

https://zhang-yang.medium.com/the-gradient-argument-in-pytorchs-backward-function-explained-by-examples-68f266950c29

profile
예술과 기술

0개의 댓글