Linear Regression Colab 노트북 다운로드 Colab 실행 ↗ import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as func
import torch.optim as opt
from google.colab import drive
drive.mount('/content/drive')
# Seed 고정
torch.manual_seed(1)Mounted at /content/drive
<torch._C.Generator at 0x7f729acdb070> # Data 수동으로 입력하기
x_train = torch.FloatTensor([[1],[2],[3]])
y_train = torch.FloatTensor([[1],[2],[3]])
print(x_train)
print(x_train.shape)
print(y_train)
print(y_train.shape)tensor([[1.],
[2.],
[3.]])
torch.Size([3, 1])
tensor([[1.],
[2.],
[3.]])
torch.Size([3, 1])
2 Hypothesis (가정) and Cost
y h y p o = H ( x ) = w ⋅ x + b y_{hypo} = H(x) = w \cdot x + b
Linear(선형)임을 가정한 경우 위 수식을 만족해야한다
data가 3개인 경우, [ y 1 y 2 y 3 ] = w ⋅ [ x 1 x 2 x 3 ] + b \begin{bmatrix} y_1 \\ y_2 \\ y_3 \end{bmatrix} = w \cdot \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} + b
Cost function으로 Mean-Square-Error를 이용한다 1 N ∑ n = 1 n = N ( y h y p o , ( n ) − y t r a i n , ( n ) ) 2 \frac{1}{N} \sum_{n=1}^{n=N} (y_{hypo, (n)} - y_{train, (n)})^2
# Model 초기화 (입력 dim, 출력 dim)
model = nn.Linear(1, 1)
# Hypothesis 정의
y_hypo = model(x_train)
print(list(model.parameters()))
print(y_hypo)
# Cost(Mean Sqaure Error) 계산
cost = func.mse_loss(y_hypo, y_train)
print(cost)[Parameter containing:
tensor([[0.5153]], requires_grad=True), Parameter containing:
tensor([-0.4414], requires_grad=True)]
tensor([[0.0739],
[0.5891],
[1.1044]], grad_fn=<AddmmBackward0>)
tensor(2.1471, grad_fn=<MseLossBackward0>)
3 Cost Optimization
Optimizer로 stochastic gradient descent를 이용한다
# Optimizer 설정 (learning rate = 0.01로 설정)
optimizer = opt.SGD(model.parameters(), lr=0.01)# 반복
for epoch in range(1000):
# Cost 계산 / mse_loss(가정에의한값, 참값)
y_hypo = model(x_train)
cost = func.mse_loss(y_hypo, y_train)
# cost를 이용해 model update
optimizer.zero_grad()
cost.backward()
optimizer.step()
# 100번 마다 중간결과 출력
if epoch % 100 == 99:
params = list(model.parameters())
w = params[0].item()
b = params[1].item()
print('Epoch {:4d}/{} w: {:.3f} b: {:.3f} Cost: {:.6f}'.format(
epoch+1, 1000, w, b, cost.item()
))Epoch 100/1000 w: 1.006 b: -0.014 Cost: 0.000026
Epoch 200/1000 w: 1.005 b: -0.011 Cost: 0.000016
Epoch 300/1000 w: 1.004 b: -0.008 Cost: 0.000010
Epoch 400/1000 w: 1.003 b: -0.007 Cost: 0.000006
Epoch 500/1000 w: 1.002 b: -0.005 Cost: 0.000004
Epoch 600/1000 w: 1.002 b: -0.004 Cost: 0.000002
Epoch 700/1000 w: 1.001 b: -0.003 Cost: 0.000001
Epoch 800/1000 w: 1.001 b: -0.003 Cost: 0.000001
Epoch 900/1000 w: 1.001 b: -0.002 Cost: 0.000001
Epoch 1000/1000 w: 1.001 b: -0.002 Cost: 0.000000
4 Multivariable Linear Regression
y h y p o = H ( 𝐱 ) = 𝐖 ⋅ 𝐱 + b y_{hypo} = H(\textbf{x}) = \textbf{W} \cdot \textbf{x} + b
Linear(선형)임을 가정한 경우 위 수식을 만족해야한다 y h y p o = [ w 1 w 2 w 3 ] ⋅ [ x 1 x 2 x 3 ] + [ b ] y_{hypo} = \begin{bmatrix} w_{1} && w_{2} && w_{3} \end{bmatrix} \cdot \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} + \begin{bmatrix} b \end{bmatrix}
MSE cost를 최소화하는 w w 와 b b 를 구해야 한다
# Numpy를 이용해 csv file을 ndarray로 가져오기
dataset = np.loadtxt(
'/content/drive/MyDrive/Colab Notebooks/courses/ML_Exp/data_linear_regression.csv',
delimiter=',',
dtype=np.float32)
# 순서를 random으로 섞기
np.random.shuffle(dataset)
# torch tensor로 변환
x_train = torch.FloatTensor(dataset[:,:-1])
y_train = torch.FloatTensor(dataset[:,[-1]])
# Linear model
model = nn.Linear(3,1)
# Optimizer 설정
optimizer = opt.SGD(model.parameters(), lr=0.00001)
# 반복
for epoch in range(1000):
y_hypo = model(x_train)
cost = func.mse_loss(y_hypo, y_train)
# model update
optimizer.zero_grad()
cost.backward()
optimizer.step()
# 100번 마다 중간결과 출력
if epoch % 100 == 99:
print('Epoch {:4d}/{} Cost: {:.6f}'.format(epoch+1, 1000, cost.item()))
print(y_hypo-y_train)
Epoch 100/1000 Cost: 7.438078
Epoch 200/1000 Cost: 7.359958
Epoch 300/1000 Cost: 7.286556
Epoch 400/1000 Cost: 7.217497
Epoch 500/1000 Cost: 7.152554
Epoch 600/1000 Cost: 7.091449
Epoch 700/1000 Cost: 7.033942
Epoch 800/1000 Cost: 6.979786
Epoch 900/1000 Cost: 6.928793
Epoch 1000/1000 Cost: 6.880751
tensor([[ 2.6862],
[-3.3903],
[-0.6640],
[ 1.5607],
[ 0.4769],
[ 4.4378],
[-0.0459],
[-1.7203],
[ 1.9515],
[-0.2119],
[ 0.2966],
[ 0.6019],
[ 0.5383],
[ 2.6749],
[-8.3141],
[ 2.5375],
[-0.4138],
[-0.9415],
[-1.0623],
[ 2.7567],
[-0.3143],
[ 2.6057],
[-3.2611],
[-3.1444],
[ 1.7440]], grad_fn=<SubBackward0>)
5 Assignment
행렬 방정식 풀기
다음 행렬 방정식을 ’Linear Regression’을 이용해 풀어보자
적당한 learning rate를 찾아 1000 epoch 정도 계산해본다
’Pseudo Inverse’를 이용한 풀이와 비교해본다
Hint: y = wx 꼴로 변환해본다
Ax=B에서는 x가 미지수이지만, y=wx에서는 w가 미지수임에 주의!
linear model에서 b를 없애기 위해서 nn.Linear() 사용법을 검색해보자 A x = B Ax=B A = [ 0 1 1 1 2 1 3 1 ] A = \begin{bmatrix}0 & 1 \\ 1 & 1 \\ 2 & 1 \\ 3 & 1 \end{bmatrix} B = [ − 1 0.2 0.9 2.1 ] B = \begin{bmatrix}-1 \\ 0.2 \\ 0.9 \\ 2.1 \end{bmatrix}