성능 비교는 속도, 인식률을 비교한다.
epoch 1 번 돌렸을 때
epoch 2 번 돌렸을 때
....
epoch 10 번 돌렸을 때 성능 비교해보기
CNN_M1 은 3층 레이어 일 때 , CNN_M2는 2층 레이어 일 때
overfitting (과다적합) : epoch 가 과도하게 늘어나면 정확도가 0으로 수렴
▶수업시간에 해본 것
3층 레어어(784, 120, 80 , 10), 2층 레어어(784, 120, 10), 1층 레어어(784, 10)
에서 epoch가 1, 5, 10 일 때 정확도 확인해보기
컨볼루션 7x7x32 , 풀링 파트 (2,2) 하나 더 넣어보기 -> 출력은 3x3x32
import torch
from torch import tensor
from torchvision import datasets
from torchvision.transforms import ToTensor
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset
import matplotlib.pyplot as plt
import random
device = 'cuda' if torch.cuda.is_available() else 'cpu'
#torch.manual_seed(777)
if device == 'cuda':
torch.cuda.manual_seed_all(777)
print(device + " is using")
training_data = datasets.MNIST("data", train=True, download=True, transform=ToTensor())
test_data = datasets.MNIST("data", train=False, download=True, transform=ToTensor())
trainLoader = torch.utils.data.DataLoader(training_data, batch_size=100, shuffle=False, drop_last=True)
testLoader = torch.utils.data.DataLoader(test_data, batch_size=100, shuffle=False, drop_last=True)
class network(nn.Module): # 네트워크 모델 정의
def __init__(self):
super(network, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(1, 6, (3, 3), padding=1), # in_channels = 1, out_channels = 6, kernel_size = 3 output = 28 * 24 * 6
nn.ReLU(),
nn.MaxPool2d(2, 2), # max 풀링 kernel_size = 2, stride = 2 output = 14 * 14 * 6
nn.Conv2d(6, 16, (3, 3), padding=1), # in_channels = 6, out_channels = 16, kernel_size = 3 output = 14 * 14 * 16
nn.ReLU(),
nn.MaxPool2d(2, 2), # max 풀링 kernel_size = 2, stride = 2 output = 7 * 7 * 16
nn.Conv2d(16, 32, (3, 3), padding=1), # in_channels = 6, out_channels = 16, kernel_size = 3 output = 14 * 14 * 16
nn.ReLU(),
nn.MaxPool2d(2, 2)
)
# torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)
# torch.nn.MaxPool2d( kernel_size , stride = None , padding = 0 , dilation = 1 , return_indices = False , ceil_mode = False )
self.fully_connected = nn.Sequential(
nn.Linear(32 * 3 * 3, 10), # 1층 레이어
#nn.Linear(120, 84), # 2층 레이어
#nn.Linear(84, 10), # 3층 레이어
)
def forward(self, x):
x = self.conv(x)
x = x.view(-1, 32 * 3 * 3) # 1차원 전환 (nn.flatten)
x = self.fully_connected(x)
return x
def train():
for epoch in range(Epochs):
loss_sum = 0
for data, target in trainLoader:
X, y = data.to(device), target.to(device) # cross
optimizer.zero_grad()
prediction = model(X) # 결과 출력
loss = criterion(prediction, y) # cross 로스 계산
loss.backward() # 로스 역전파
optimizer.step() # 실질적 웨이트 수정
loss_sum += loss.item()
#print("epoch = %d loss = %f" % (epoch + 1, round(loss_sum / batch_count, 3)))
#test()
def test(cnt):
correct = 0
with torch.no_grad():
for data, target in testLoader:
data, target = data.to(device), target.to(device)
outputs = model(data) # 출력 계산
# 추론 계산
_, predicted = torch.max(outputs, 1) # 가장 큰 인덱스 위치를 리턴함 @ return value, index
correct += predicted.eq(target).sum() # 정답과 일치한 경우 정답 카운트를 증가
#print(f"predicted : {predicted}")
#print(f"target : {target}")
data_num = len(test_data) # 데이터 총 건수
print(f"epoch {cnt} ")
print("accuracy = {}/10000\n".format(correct))
model = network().to(device)
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
Epochs = 1
batch_count = len(trainLoader)
train()
test(Epochs)
Epochs = 5
batch_count = len(trainLoader)
train()
test(Epochs)
Epochs = 10
batch_count = len(trainLoader)
train()
test(Epochs)
MLP 와 CNN의 차이 :
시그모이드 함수 사용 vs 시그모이드 함수 사용x
CNN에서는 시그모이드 함수를 사용안하기 때문에
다음과 같이 ReLU 함수를 사용해보기
'학교 > 인공지능' 카테고리의 다른 글
25번째 수업(0613) (0) | 2024.06.13 |
---|---|
23번째 수업(0611) (1) | 2024.06.11 |
22번째 수업(0605) (0) | 2024.06.05 |
21번째 수업(0530) (0) | 2024.05.30 |
20번째 수업(0529) (0) | 2024.05.29 |