ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Pytorch] MNIST 신경망 구현
    딥러닝 2020. 3. 28. 23:34

    Pytorch를 이용해 간단한 MNIST 신경망을 구현하겠습니다. Convolution을 사용하는 것이 아닌 단순한 신경망으로 이루어진 네트워크입니다. https://adventuresinmachinelearning.com/pytorch-tutorial-deep-learning/을 참고하였습니다.

     

    신경망 구조

    코드에 대한 설명은 주석에 첨부하였습니다.

    import torch.nn as nn
    import torch.nn.functional as F
    class NeuralNetwork(nn.Module): # 상속
        def __init__(self):
            super().__init__() # 기본 nn.Module 모델 생성
            self.fc1 = nn.Linear(in_features=28*28, out_features=200) # fully connected nn
            self.fc2 = nn.Linear(in_features=200, out_features=200)
            self.fc3 = nn.Linear(in_features=200, out_features=10)
            
        def forward(self, x): # data flow, dummy method에 오버라이딩
            x = F.relu(self.fc1(x))
            x = F.relu(self.fc2(x))
            x = self.fc3(x)
            return F.log_softmax(x)
        
    net = NeuralNetwork()
    # training
    import torch.optim as optim
    
    learning_rate = 0.01
    batch_size = 100
    
    optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9) # Storchastic GD optimizer
    criterion = nn.NLLLoss()
    # 모델의 출력 결과가 softmax 함수로 각각의 클래스에 대한 값이 0에서 1사이의 값으로 주어진다.
    # 더 잘 학습 하기 위해 negative log loss 함수 즉 -log 값을 취해 1에 가까운 값은 값이 작게 1에 조금이라도 멀면
    # 기하 급수적으로 값이 커지게 설정한다
    # dataset
    import torchvision
    import torchvision.transforms as transfroms
    
    train_set = torchvision.datasets.FashionMNIST(
        root = './data/FashionMNIST',
        train = True,
        download = True,
        transform = transfroms.Compose([
            transfroms.ToTensor() # 데이터를 0에서 255까지 있는 값을 0에서 1사이 값으로 변환
        ])
    )
    loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size)
    import pandas as pd
    from collections import OrderedDict
    from IPython.display import clear_output
    epochs = 3
    
    pd_results = []
    
    for epoch in range(epochs): # epoch 개수
        batch_idx = 0
        tot_num = 0
        correct_num = 0
        for batch in loader:
            batch_idx += 1
            
            images = batch[0] # 입력 데이터
            labels = batch[1] # 해당 클래스
            
            images = images.view(-1, 28*28) # 28x28 행렬을 벡터로 변환, -1은 나머지 차원들로 맞춰진다는 의미
            
            optimizer.zero_grad() # gradient 초기화 -> back propagation 준비
            
            net_out = net(images) # net의 forward실행, 1x10 형태 가진다 (0에서 9까지의 softmax 결과)
            loss = criterion(net_out, labels) # NLL 얻음
            loss.backward() # loss은 스칼라이므로 argument 필요없음
            optimizer.step() # GD 실행
            
            for i in range(len(labels)):
                tot_num += 1
                pred = torch.max(net_out[i], 0)[1]
                correct_num += pred.eq(labels[i]).sum()
            
            if batch_idx%100==0:
                # pandas로 해당 실행 결과를 표로 정리
                results = OrderedDict()
                results['epoch'] = epoch
                results['batch_idx'] = batch_idx
                results['loss'] = loss.item()
                results['accuracy'] = 100.*correct_num.item()/tot_num
                pd_results.append(results)
                df = pd.DataFrame.from_dict(pd_results, orient='columns')
    
                clear_output(wait=True)
                display(df)

    학습 결과

     

    Jupyter Notebook 파일

    Learn_NeuralNet.ipynb
    0.02MB

     

    '딥러닝' 카테고리의 다른 글

    [Pytorch] EMNIST CNN  (0) 2020.04.03
    [Pytorch] MNIST CNN  (0) 2020.03.31
    [Pytorch] 간단한 신경망 구현  (0) 2020.03.16
Designed by Tistory.