import torch
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
import torch.nn as nn
import matplotlib.pyplot as plt
import random
random.seed(777)
torch.manual_seed(777)
training_epochs = 15
batch_size = 100
mnist_train = dsets.MNIST(root='MNIST_data/',
train=True,
transform=transforms.ToTensor(),
download=True)
mnist_test = dsets.MNIST(root='MNIST_data/',
train=False,
transform=transforms.ToTensor(),
download=True)
data_loader = DataLoader(dataset=mnist_train,
batch_size=batch_size, # 배치 크기는 100
shuffle=True,
drop_last=True)
drop_last : 마지막 배치를 버릴 것인가?
다른 미니 배치보다 개수가 적은 마지막 배치를 경사하강법에 사용하여 마지막 배치가 상대적으로 과대 평가되는 현상 막아줌.
linear = nn.Linear(784, 10, bias=True).to(device)
#비용 함수와 옵티마이저 정의
criterion = nn.CrossEntropyLoss().to(device) # 내부적으로 소프트맥스 함수를 포함하고 있음.
optimizer = torch.optim.SGD(linear.parameters(), lr=0.1)
to() 함수 : 연산을 어디서 수행하는가? GPU 사용할 때 to('cuda')로.
나는 도대체 왜 cuda가 안써지냐고......................
bias : 편향 b를 사용할 것인가? 기본값 True
cost function과 optimizer 정의
torch.nn.CrossEntropyLoss()로 크로스 엔트로피 함수 사용
for epoch in range(training_epochs): # 앞서 training_epochs의 값은 15로 지정
avg_cost = 0
total_batch = len(data_loader) #총 배치의 크기
for X, Y in data_loader:
# 배치 크기가 100, 아래의 연산에서 X는 (100, 784)의 텐서가 된다.
X = X.view(-1, 28 * 28).to(device)
# 레이블은 원-핫 인코딩이 된 상태가 아니라 0 ~ 9의 정수.
Y = Y.to(device)
optimizer.zero_grad()
hypothesis = linear(X)
cost = criterion(hypothesis, Y)
cost.backward()
optimizer.step()
avg_cost += cost / total_batch
print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))
print('Learning finished')
##################output###################
Epoch: 0001 cost = 0.535150588
Epoch: 0002 cost = 0.359577745
Epoch: 0003 cost = 0.331264287
Epoch: 0004 cost = 0.316404700
Epoch: 0005 cost = 0.307106972
Epoch: 0006 cost = 0.300456554
Epoch: 0007 cost = 0.294933408
Epoch: 0008 cost = 0.290956199
Epoch: 0009 cost = 0.287074089
Epoch: 0010 cost = 0.284515619
Epoch: 0011 cost = 0.281914055
with torch.no_grad(): # torch.no_grad()를 하면 gradient 계산을 수행x
X_test = mnist_test.test_data.view(-1, 28 * 28).float().to(device)
Y_test = mnist_test.test_labels.to(device)
prediction = linear(X_test)
correct_prediction = torch.argmax(prediction, 1) == Y_test
accuracy = correct_prediction.float().mean()
print('Accuracy:', accuracy.item())
# MNIST 테스트 데이터에서 무작위로 하나를 뽑아서 예측
r = random.randint(0, len(mnist_test) - 1)
X_single_data = mnist_test.test_data[r:r + 1].view(-1, 28 * 28).float().to(device)
Y_single_data = mnist_test.test_labels[r:r + 1].to(device)
print('Label: ', Y_single_data.item())
single_prediction = linear(X_single_data)
print('Prediction: ', torch.argmax(single_prediction, 1).item())
plt.imshow(mnist_test.test_data[r:r + 1].view(28, 28), cmap='Greys', interpolation='nearest')
plt.show()
'Pytorch' 카테고리의 다른 글
딥러닝 과적합 문제, 성능 저하 문제, 경사 하강법 유형, 기울기 소실과 폭주 (0) | 2023.01.02 |
---|---|
퍼셉트론과 딥러닝, 활성화 함수, 손실 함수, 순전파와 역전파 (0) | 2023.01.02 |
SoftMax Regression (0) | 2022.12.29 |
Logistic Regression (0) | 2022.12.29 |
Mini Batch and Data Load (0) | 2022.12.29 |