<목차>
1. Cost 함수 구현하기
2. Softmax regression 구현하기
- nn.Module로 구현
- class로 구현
Low Level로 Cost함수 구현하기
import torch
import torch.nn.functional as F
torch.manual_seed(1)
z = torch.FloatTensor([1, 2, 3])
hypothesis = F.softmax(z, dim=0) #3개의 원소 값이 0과 1 사이의 값을 가지는 벡터로 변환됨
print(hypothesis) #tensor([0.0900, 0.2447, 0.6652])
import torch
import torch.nn.functional as F
torch.manual_seed(1)
z = torch.rand(3, 5, requires_grad=True)
hypothesis = F.softmax(z, dim=1)
y_one_hot = torch.zeros_like(hypothesis) # 모든 원소가 0의 값을 가진 3 × 5 텐서 생성
y_one_hot.scatter_(1, y.unsqueeze(1), 1) #(3,)의 크기를 가졌던 y 텐서는 (3 × 1) 텐서가 됨
'''
tensor([[1., 0., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 1., 0., 0., 0.]])
'''
cost = (y_one_hot * -torch.log(hypothesis)).sum(dim=1).mean()
print(cost) #tensor(1.5945, grad_fn=<MeanBackward0>)
softmax 함수의 결과에 log를 씌울 때 torch.log(F.softmax(z, dim = 1))
High Level로 Cost함수 구현하기
F.softmax() + torch.log() = F.log_softmax()
F.log_softmax(z, dim=1) #z의 softmax 결과에 log를 씌움
F.log_softmax() + F.nll_loss() = F.cross_entropy()
F.cross_entropy()는 비용함수와 softmax 함수를 모두 포함함
cost = (y_one_hot * -torch.log(hypothesis)).sum(dim=1).mean()
# 위는 아래와 전부 같은 식임
cost = (y_one_hot * - F.log_softmax(z, dim=1)).sum(dim=1).mean()
cost = F.nll_loss(F.log_softmax(z, dim=1), y)
F.cross_entropy(z, y)
Low Level로 softmax 회귀 구현하기
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
torch.manual_seed(1)
x_train = [[1, 2, 1, 1],
[2, 1, 3, 2],
[3, 1, 3, 4],
[4, 1, 5, 5],
[1, 7, 5, 5],
[1, 2, 5, 6],
[1, 6, 6, 6],
[1, 7, 7, 7]]
y_train = [2, 2, 2, 1, 1, 1, 0, 0]
x_train = torch.FloatTensor(x_train) #torch.Size([8, 4])
y_train = torch.LongTensor(y_train) #torch.Size([8])
# 최종 사용할 label은 y_train에서 one hot coding을 한 결과여야함
# class의 개수는 3개이므로 y_train에 one hot coding을 한 결과는 8*3이어야함
y_one_hot = torch.zeros(8, 3)
y_one_hot.scatter_(1, y_train.unsqueeze(1), 1)
print(y_one_hot.shape) #torch.Size([8, 3])
# 모델 초기화
W = torch.zeros((4, 3), requires_grad=True)
b = torch.zeros(1, requires_grad=True)
# optimizer 설정
optimizer = optim.SGD([W, b], lr=0.1)
nb_epochs = 1000
for epoch in range(nb_epochs + 1):
# 가설
hypothesis = F.softmax(x_train.matmul(W) + b, dim=1)
# 비용 함수
cost = (y_one_hot * -torch.log(hypothesis)).sum(dim=1).mean()
# cost로 H(x) 개선
optimizer.zero_grad()
cost.backward()
optimizer.step()
# 100번마다 로그 출력
if epoch % 100 == 0:
print('Epoch {:4d}/{} Cost: {:.6f}'.format(
epoch, nb_epochs, cost.item()
))
High Level로 softmax 회귀 구현하기
F.cross_entropy()는 비용함수와 softmax 함수를 모두 포함함 -> Hypothesis에 softmax 사용할 필요없음
# 모델 초기화
W = torch.zeros((4, 3), requires_grad=True)
b = torch.zeros(1, requires_grad=True)
# optimizer 설정
optimizer = optim.SGD([W, b], lr=0.1)
nb_epochs = 1000
for epoch in range(nb_epochs + 1):
# ***** Cost 계산 *****
z = x_train.matmul(W) + b
cost = F.cross_entropy(z, y_train)
# cost로 H(x) 개선
optimizer.zero_grad()
cost.backward()
optimizer.step()
# 100번마다 로그 출력
if epoch % 100 == 0:
print('Epoch {:4d}/{} Cost: {:.6f}'.format(
epoch, nb_epochs, cost.item()
))
nn.Module로 구현
# ***** 모델을 선언 및 초기화. 4개의 특성을 가지고 3개의 클래스로 분류. input_dim=4, output_dim=3. *****
model = nn.Linear(4, 3)
# optimizer 설정
optimizer = optim.SGD(model.parameters(), lr=0.1)
nb_epochs = 1000
for epoch in range(nb_epochs + 1):
# H(x) 계산
prediction = model(x_train)
# cost 계산
cost = F.cross_entropy(prediction, y_train)
# cost로 H(x) 개선
optimizer.zero_grad()
cost.backward()
optimizer.step()
# 20번마다 로그 출력
if epoch % 100 == 0:
print('Epoch {:4d}/{} Cost: {:.6f}'.format(
epoch, nb_epochs, cost.item()
))
Class로 구현
class SoftmaxClassifierModel(nn.Module):
def __init__(self):
super().__init__()
self.linear = nn.Linear(4, 3) # Output이 3!
def forward(self, x):
return self.linear(x)
model = SoftmaxClassifierModel()
'전공공부 > 인공지능' 카테고리의 다른 글
MNIST, Torchvision (0) | 2022.07.28 |
---|---|
[실습] softmax model, train, validation, learning rate 조절, data processing (0) | 2022.07.28 |
Softmax Classification (0) | 2022.07.27 |
Mini Batch 경사하강법, Data Load, Custom Dataset (0) | 2022.07.27 |
Class로 pytorch 모델 구현 (0) | 2022.07.27 |