(3주차) 9월28일
파이토치를 이용하여 회귀모형 학습하기 (2)
-
(1/3): 9월14-16일 강의노트의 일부내용 추가설명
-
(2/3): torch.nn.Linear()를 사용하여 yhat을 계산하기, torch.nn.MSELoss()를 이용하여 loss를 계산하기
-
(3/3): 과제설명
import torch
import numpy as np
-
model:
-
model:
torch.manual_seed(43052)
n=100
ones= torch.ones(n)
x,_ = torch.randn(n).sort()
X = torch.vstack([ones,x]).T
W = torch.tensor([2.5,4])
ϵ = torch.randn(n)*0.5
y = X@W + ϵ
ytrue = X@W
-
step1: yhat
-
step2: loss
-
step3: derivation
-
step4: update
-
feedforward 신경망을 설계하는 과정
-
이 단계가 잘 완료되었다면, 임의의 을 넣었을 때 를 계산할 수 있어야 함
What=torch.tensor([-5.0,10.0],requires_grad=True)
yhat1=X@What
yhat1
net = torch.nn.Linear(in_features=2 ,out_features=1, bias=False)
net.weight.data
net.weight.data=torch.tensor([[-5.0,10.0]])
net.weight.data
net(X)
yhat2=net(X)
net = torch.nn.Linear(in_features=1 ,out_features=1, bias=True)
net.weight.data
net.weight.data=torch.tensor([[10.0]])
net.bias.data=torch.tensor([-5.0])
net.weight,net.bias
net(x.reshape(100,1))
loss=torch.mean((y-yhat1)**2)
loss
loss=torch.mean((y-yhat2)**2)
loss
- 176.2661? 이건 잘못된 결과임
loss=torch.mean((y.reshape(100,1)-yhat2)**2)
loss
lossfn=torch.nn.MSELoss()
loss=lossfn(y,yhat1)
loss
loss=lossfn(y.reshape(100,1),yhat2)
loss
-
model:
torch.manual_seed(43052)
n=100
ones= torch.ones(n)
x1,_ = torch.randn(n).sort()
x2,_ = torch.randn(n).sort()
X = torch.vstack([ones,x1,x2]).T
W = torch.tensor([2.5,4,-2])
ϵ = torch.randn(n)*0.5
y = X@W + ϵ
ytrue = X@W
X
-
torch.nn.Linear() 를 이용하여 에 대한 를 구하라.