beginner/examples_nn/polynomial_module
Run in Google Colab
Colab
Download Notebook
Notebook
View on GitHub
GitHub
참고
Go to the end to download the full example code.
PyTorch: 사용자 정의 nn.Module#
\(y=\sin(x)\) 을 예측할 수 있도록, \(-\pi\) 부터 \(\pi\) 까지 유클리드 거리(Euclidean distance)를 최소화하도록 3차 다항식을 학습합니다.
이번에는 사용자가 새롭게 정의한 Module의 하위 클래스(subclass)로 모델을 정의합니다. 기존 Module들을 사용하는 간단한 구성보다 더 복잡한 모델을 원한다면, 이 방법으로 모델을 정의하면 됩니다.
99 1790.0306396484375
199 1186.8878173828125
299 787.9891967773438
399 524.1669921875
499 349.6788024902344
599 234.27296447753906
699 157.9431915283203
799 107.45744323730469
899 74.06480407714844
999 51.977508544921875
1099 37.367801666259766
1199 27.704023361206055
1299 21.311565399169922
1399 17.082918167114258
1499 14.285581588745117
1599 12.435097694396973
1699 11.210851669311523
1799 10.400947570800781
1899 9.865121841430664
1999 9.51059341430664
Result: y = -0.002869244199246168 + 0.8312788605690002 x + 0.0004949906724505126 x^2 + -0.08970864862203598 x^3
import torch
import math
class Polynomial3(torch.nn.Module):
def __init__(self):
"""
생성자에서 4개의 매개변수를 생성(instantiate)하고, 멤버 변수로 지정합니다.
"""
super().__init__()
self.a = torch.nn.Parameter(torch.randn(()))
self.b = torch.nn.Parameter(torch.randn(()))
self.c = torch.nn.Parameter(torch.randn(()))
self.d = torch.nn.Parameter(torch.randn(()))
def forward(self, x):
"""
순전파 함수에서는 입력 데이터의 텐서를 받고 출력 데이터의 텐서를 반환해야 합니다.
텐서들 간의 임의의 연산뿐만 아니라, 생성자에서 정의한 Module을 사용할 수 있습니다.
"""
return self.a + self.b * x + self.c * x ** 2 + self.d * x ** 3
def string(self):
"""
Python의 다른 클래스(class)처럼, PyTorch 모듈을 사용해서 사용자 정의 메소드를 정의할 수 있습니다.
"""
return f'y = {self.a.item()} + {self.b.item()} x + {self.c.item()} x^2 + {self.d.item()} x^3'
# 입력값과 출력값을 갖는 텐서들을 생성합니다.
x = torch.linspace(-math.pi, math.pi, 2000)
y = torch.sin(x)
# 위에서 정의한 클래스로 모델을 생성합니다.
model = Polynomial3()
# 손실 함수와 optimizer를 생성합니다. SGD 생성자에 model.paramaters()를 호출해주면
# 모델의 멤버 학습 가능한 (torch.nn.Parameter로 정의된) 매개변수들이 포함됩니다.
criterion = torch.nn.MSELoss(reduction='sum')
optimizer = torch.optim.SGD(model.parameters(), lr=1e-6)
for t in range(2000):
# 순전파 단계: 모델에 x를 전달하여 예측값 y를 계산합니다.
y_pred = model(x)
# 손실을 계산하고 출력합니다.
loss = criterion(y_pred, y)
if t % 100 == 99:
print(t, loss.item())
# 변화도를 0으로 만들고, 역전파 단계를 수행하고, 가중치를 갱신합니다.
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f'Result: {model.string()}')
Total running time of the script: (0 minutes 2.662 seconds)