2022.06.27 연구실 공부(PyTorch). 'PyTorch로 시작하는 딥 러닝 입문'< 이 글은 이 책의 내용을 요약 정리한 것임.>(내 저작물이 아니고 저 위 링크에 있는 것이 원본임)
03-03 Multivariable Linear Regression
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
torch.manual_seed(1)
# 훈련 데이터
x1_train = torch.FloatTensor([[78], [84], [79], [96], [75]])
x2_train = torch.FloatTensor([[80], [76], [87], [92], [92]])
x3_train = torch.FloatTensor([[98], [87], [95], [94], [85]])
x4_train = torch.FloatTensor([[75], [72], [80], [100], [89]])
y_train = torch.FloatTensor([[142], [138], [171], [152], [169]])
# 가중치 w와 편향 b 초기화
w1 = torch.zeros(1, requires_grad=True)
w2 = torch.zeros(1, requires_grad=True)
w3 = torch.zeros(1, requires_grad=True)
w4 = torch.zeros(1, requires_grad=True)
b = torch.zeros(1, requires_grad=True)
# optimizer 설정
optimizer = optim.SGD([w1, w2, w3, w4, b], lr=1e-5)
nb_epochs = 1000
for epoch in range(nb_epochs + 1):
# H(x) 계산
hypothesis = x1_train * w1 + x2_train * w2 + x3_train * w3 + x4_train * w4 + b
# cost 계산
cost = torch.mean((hypothesis - y_train) ** 2)
# cost로 H(x) 개선
optimizer.zero_grad()
cost.backward()
optimizer.step()
# 100번마다 로그 출력
if epoch % 100 == 0:
print('Epoch {:4d}/{} w1: {:.3f} w2: {:.3f} w3: {:.3f} w4: {:.3f} b: {:.3f} Cost: {:.6f}'.format(
epoch, nb_epochs, w1.item(), w2.item(), w3.item(), w4.item(), b.item(), cost.item()
))
위 처럼 일일이 선언할 경우 x의 개수가 1000개일 경우에는 x,w 선언이 어렵다
행렬의 곱셈 연산 혹은 벡터의 내적을 통해 이 문제를 해결한다.
이는 곧 1 x 7 + 2 x 9 + 3 x 11 = 58이 된다.
# 훈련 데이터
x_train = torch.FloatTensor([[78, 80, 98, 75],
[84, 76, 87, 72],
[79, 87, 95, 80],
[96, 92, 94, 100],
[75, 92, 85, 89]])
y_train = torch.FloatTensor([[142], [138], [171], [152], [169]])
print(x_train.shape)
print(y_train.shape)
torch.Size([5, 4])
torch.Size([5, 1])
# 가중치와 편향 선언
W = torch.zeros((4, 1), requires_grad=True)
b = torch.zeros(1, requires_grad=True)
# optimizer 설정
optimizer = optim.SGD([W, b], lr=1e-5)
nb_epochs = 1000
for epoch in range(nb_epochs + 1):
# H(x) 계산
# 편향 b는 브로드 캐스팅되어 각 샘플에 더해집니다.
hypothesis = x_train.matmul(W) + b
# cost 계산
cost = torch.mean((hypothesis - y_train) ** 2)
# cost로 H(x) 개선
optimizer.zero_grad()
cost.backward()
optimizer.step()
# 100번마다 로그 출력
if epoch % 100 == 0:
print('Epoch {:4d}/{} hypothesis: {} Cost: {:.6f}'.format(
epoch, nb_epochs, hypothesis.squeeze().detach(), cost.item()
))
출처 : 'PyTorch로 시작하는 딥 러닝 입문' <이 책의 내용을 요약 정리한 것임.>