<jupyter_start><jupyter_text>import torch<jupyter_code>import torch
import mnist_train as mt
torch.__version__<jupyter_output><empty_output><jupyter_text>GPU check<jupyter_code># Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device<jupyter_output><empty_output><jupyter_text>Download MNIST dataset What is MNIST dataset?> MNIST 데이터베이스 (Modified National Institute of Standards and Technology database)는  손으로 쓴 숫자들로 이루어진 대형 데이터베이스이며,   다양한 화상 처리 시스템을 트레이닝하기 위해 일반적으로 사용된다.   이 데이터베이스는 또한 기계 학습 분야의 트레이닝 및 테스트에 널리 사용된다.  >  > https://ko.wikipedia.org/wiki/MNIST_데이터베이스<jupyter_code>from torchvision import datasets
from torchvision.transforms import ToTensor
import numpy as np
import matplotlib.pyplot as plt

train_data = datasets.MNIST(
    root = 'data',
    train = True,                         
    transform = ToTensor(), 
    download = True,            
)
test_data = datasets.MNIST(
    root = 'data', 
    train = False, 
    transform = ToTensor()
)

# train data 특성
print(train_data)
print(train_data.data.size())
# 데이터에 어떤 내용이 있는지 확인
img, label = train_data[0]
print(img.shape)
print(label)
mt.plot_train_data(img, label)<jupyter_output><empty_output><jupyter_text>Plot multiple train_data<jupyter_code>mt.plot_multiple_train_data(train_data, rows=5, cols=8)<jupyter_output><empty_output><jupyter_text>Dataloader설정된 `batch_size` 단위로 데이터를 가져올 수 있도록 하는 모듈<jupyter_code>from torch.utils.data import DataLoader
loaders = {
    'train' : torch.utils.data.DataLoader(train_data, 
                                          batch_size=100, 
                                          shuffle=True),
    
    'test'  : torch.utils.data.DataLoader(test_data, 
                                          batch_size=100, 
                                          shuffle=True, ),
}
loaders<jupyter_output><empty_output><jupyter_text>Defile Model<jupyter_code>import torch.nn as nn
import torch.nn.functional as F
class CNN(nn.Module):
    
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Sequential(         
            nn.Conv2d(
                in_channels=1,              
                out_channels=32,            
                kernel_size=3,              
                stride=1,                   
                padding=1,                  
            ),                              
            nn.ReLU(),                      
            nn.MaxPool2d(kernel_size=2),    
        )
        self.conv2 = nn.Sequential(         
            nn.Conv2d(32, 64, 3, 1, 1),     
            nn.ReLU(),                      
            nn.MaxPool2d(2),                
        )
        # fully connected layer, output 10 classes
        self.out = nn.Linear(64 * 7 * 7, 10)
        
    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        
        # flatten the output of conv2 to (batch_size, 32 * 7 * 7)
        x = x.view(x.size(0), -1)       
        output = self.out(x)
        output = F.softmax(output, dim=1)

        return output    # return x for visualization
cnn = CNN()
cnn.to(device)
print(cnn)
#!pip install torchsummary
from torchsummary import summary
summary(cnn, input_size=(1,28,28))<jupyter_output>----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1           [-1, 32, 28, 28]             320
              ReLU-2           [-1, 32, 28, 28]               0
         MaxPool2d-3           [-1, 32, 14, 14]               0
            Conv2d-4           [-1, 64, 14, 14]          18,496
              ReLU-5           [-1, 64, 14, 14]               0
         MaxPool2d-6             [-1, 64, 7, 7]               0
            Linear-7                   [-1, 10]          31,370
================================================================
Total params: 50,186
Trainable params: 50,186
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.65
Params size (MB): 0.19
Estimated Total Size (MB): 0.84
-------------------------------------------[...]<jupyter_text>import matplotlib.pyplot as pltdef draw(image, label):    pixels = image.reshape((28, 28))    plt.imshow(pixels, cmap='gray')    plt.title(f"{label}")    plt.show() import randomdef show_predict(model):    idx = random.randrange(0, len(test_data)-1)     임의의 정수 1개     image, gt = test_data[idx]                      image하고 gt 가져오기    image = image.to(device)                           predicted = model(image.reshape(1,1,28,28))     모델에 이미지 넣은 결과값 -> predicted.shape = (10, )    confidence, label = torch.max(predicted, 1)     가장 큰 1개 값의 confidence와 label(class) 가져오기    label = torch.argmax(predicted).cpu().item()    print("idx:", idx)    print("predicted:", predicted)    print("confidence:", confidence)    print("label:", label)    draw(image.cpu(), label.item())<jupyter_code>mt.show_sample_predict_cnn(cnn, device, test_data)<jupyter_output>> gt: 2
> image shape: torch.Size([1, 1, 28, 28])
< out: tensor([[0.1072, 0.0901, 0.0981, 0.1015, 0.0964, 0.0947, 0.0972, 0.1057, 0.0985,
         0.1105]], device='cuda:0', grad_fn=<SoftmaxBackward0>)
< predicted: 9
< confidence: 0.11053552478551865<jupyter_text>Define loss function<jupyter_code>loss_func = nn.CrossEntropyLoss()   
loss_func<jupyter_output><empty_output><jupyter_text>Define optimizer<jupyter_code>from torch import optim
optimizer = optim.Adam(cnn.parameters(), lr = 0.01)   
optimizer<jupyter_output><empty_output><jupyter_text>Train model<jupyter_code>from torch.autograd import Variable

def train(num_epochs, cnn, loaders):
    
    cnn.train()
        
    # Train the model
    total_step = len(loaders['train'])
        
    for epoch in range(num_epochs):
        for i, (images, labels) in enumerate(loaders['train']):
            
            images = images.to(device, dtype=torch.float32)
            labels = labels.to(device)
            # gives batch data, normalize x when iterate train_loader
            b_x = Variable(images)   # batch x
            b_y = Variable(labels)   # batch y

            output = cnn(b_x)             

            loss = loss_func(output, b_y)
            
            # clear gradients for this training step   
            optimizer.zero_grad()           
            
            # backpropagation, compute gradients 
            loss.backward()    
            
            # apply gradients             
            optimizer.step()                
            
            if (i+1) % 100 == 0:
                print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' 
                       .format(epoch + 1, num_epochs, i + 1, total_step, loss.item()))
                pass
        
        pass
    
    
    pass
train(num_epochs, cnn, loaders)<jupyter_output>Epoch [1/3], Step [100/600], Loss: 1.6174
Epoch [1/3], Step [200/600], Loss: 1.5812
Epoch [1/3], Step [300/600], Loss: 1.6101
Epoch [1/3], Step [400/600], Loss: 1.6237
Epoch [1/3], Step [500/600], Loss: 1.6385
Epoch [1/3], Step [600/600], Loss: 1.5719
Epoch [2/3], Step [100/600], Loss: 1.5778
Epoch [2/3], Step [200/600], Loss: 1.5996
Epoch [2/3], Step [300/600], Loss: 1.5611
Epoch [2/3], Step [400/600], Loss: 1.6409
Epoch [2/3], Step [500/600], Loss: 1.5911
Epoch [2/3], Step [600/600], Loss: 1.5698
Epoch [3/3], Step [100/600], Loss: 1.6099
Epoch [3/3], Step [200/600], Loss: 1.5811
Epoch [3/3], Step [300/600], Loss: 1.5907
Epoch [3/3], Step [400/600], Loss: 1.6212
Epoch [3/3], Step [500/600], Loss: 1.6112
Epoch [3/3], Step [600/600], Loss: 1.6209<jupyter_text>Evaluate<jupyter_code>def evaluate():
    # Test the model
    cnn.eval()
    with torch.no_grad():
        correct = 0
        total = 0
        for images, labels in loaders['test']:
            images = images.to(device, dtype=torch.float32)
            labels = labels.to(device)
            test_output = cnn(images)
            pred_y = torch.max(test_output, 1)[1].data.squeeze()
            accuracy = (pred_y == labels).sum().item() / float(labels.size(0))
            pass

        print('Test Accuracy of the model on the 10000 test images: %.2f' % accuracy)
    
    pass
evaluate()<jupyter_output>Test Accuracy of the model on the 10000 test images: 0.84<jupyter_text>Evaluation data sampling<jupyter_code>figure = plt.figure(figsize=(10, 8))
cols, rows = 5, 5
for i in range(1, cols * rows + 1):
    cnn.eval()
    sample_idx = np.random.randint(len(test_data), size=(1,)).item()
    img, gt = test_data[sample_idx]
    img = img.to(device)
    predicted = cnn(img.view(1,1,28,28))
    #label = torch.argmax(predicted)
    confidence, label = torch.max(predicted, 1)
    label = label.cpu().item()
    figure.add_subplot(rows, cols, i)
    plt.title(f"{label}/(GT:{gt} / {gt==label})")
    plt.axis("off")
    plt.imshow(img.view(28,28).cpu(), cmap="gray")

plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Test<jupyter_code>show_predict(cnn)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15484931506849314, 'fraction_numerical': 0.05797260273972603, 'mean_word_length': 2.0851926977687625, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 56, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '289315', 'license': 'Apache-2.0', 'n_tokens_mistral': 3476, 'n_tokens_neox': 3196, 'n_words': 688, 'path': 'practice/day6/backup/MNIST_pytorch_cnn-Copy1.ipynb', 'repo_name': 'ideptor/mlips', 'size': '9125'}