Linear Regression

Imports

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>

1 Data loading

x y
1 1
2 2
3 3
# 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

yhypo=H(x)=w⋅x+b y_{hypo} = H(x) = w \cdot x + b

# 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 설정 (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

yhypo=H(𝐱)=𝐖⋅𝐱+b y_{hypo} = H(\textbf{x}) = \textbf{W} \cdot \textbf{x} + 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