<jupyter_start><jupyter_text>environment check<jupyter_code>import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
torch.__version__
device = 'cuda' if torch.cuda.is_available() else 'cpu'
device<jupyter_output><empty_output><jupyter_text>system check<jupyter_code>import sys
import os
cwd = os.getcwd() # current path
sys.version_info<jupyter_output><empty_output><jupyter_text>seed settings<jupyter_code>import random
random.seed(1)
torch.manual_seed(1)
if device == 'cuda':
    torch.cuda.manual_seed_all(1)<jupyter_output><empty_output><jupyter_text>Data Load<jupyter_code>from mnist import *
mymnist = MyMNIST()
import matplotlib.pyplot as plt
for X, Y in mymnist.train_data_loader:
    r = random.randint(0, mymnist.train_data_loader.batch_size)
    
    X = X[r,:]
    
    plt.imshow(X.reshape(28, 28), cmap = 'gray')
    plt.title('{}'.format(Y[r]))
    plt.show()
    break<jupyter_output><empty_output><jupyter_text>CNN model build<jupyter_code>class CNN(nn.Module):
    def __init__(self):
        super().__init__()
        
        self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 16, kernel_size = 3, padding = 1)
        self.pool1 = nn.MaxPool2d(2, 2)
        
        self.conv2 = nn.Conv2d(in_channels = 16, out_channels = 32, kernel_size = 3, padding = 1)
        self.pool2 = nn.MaxPool2d(2, 2)
        
        self.layer = nn.Linear(in_features=32*7*7, out_features=10)
    
    def forward(self, x):
        out = self.conv1(x)
        out = F.relu(out)
        out = self.pool1(out)
        
        out = self.conv2(out)
        out = F.relu(out)
        out = self.pool2(out)
        
        out = out.view(out.size(0), -1)
        out = self.layer(out)
        
        return out
model = CNN().to(device)
model
torch.randn(1, 28, 28).shape
model(torch.randn(1, 1, 28, 28).to(device))<jupyter_output><empty_output><jupyter_text>Train model hyperprameter<jupyter_code>epochs = 10
learning_rate = 1e-3
batch_size = mymnist.train_data_loader.batch_size
total_batch = len(mymnist.train_data_loader)<jupyter_output><empty_output><jupyter_text>loss function<jupyter_code>criterion = nn.CrossEntropyLoss().to(device)
optimizer = optim.Adam(model.parameters(), lr=learning_rate)<jupyter_output><empty_output><jupyter_text>train model<jupyter_code>model.train()
%%time
for i in range(epochs):
    avg_loss = 0
    for X, Y in mymnist.train_data_loader:
        X = X.view(-1, 1, 28, 28).to(device)
        Y = Y.to(device)
        out = model(X)
        
        loss = criterion(out, Y)
        
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        avg_loss += loss / total_batch
    
    print('[Epoch: {:>4}] cost = {:>.9}'.format(i + 1, avg_loss))<jupyter_output>[Epoch:    1] cost = 0.332181007
[Epoch:    2] cost = 0.0875096768
[Epoch:    3] cost = 0.0631829053
[Epoch:    4] cost = 0.0507494174
[Epoch:    5] cost = 0.0435043909
[Epoch:    6] cost = 0.0389000922
[Epoch:    7] cost = 0.0336422808
[Epoch:    8] cost = 0.0295381472
[Epoch:    9] cost = 0.0263337921
[Epoch:   10] cost = 0.0243617184
Wall time: 1min 19s<jupyter_text>Test Model<jupyter_code>data_len = len(mymnist.mnist_test)
data_len
with torch.no_grad():
    model.eval()
    
    total_acc = 0
    
    for X, Y in mymnist.test_data_loader:
        X = X.view(-1, 1, 28, 28).to(device)
        Y = Y.to(device)
        
        prediction = model(X)
        correct_prediction = torch.argmax(prediction, 1) == Y
        
        total_acc += sum(correct_prediction.float()) / data_len
    
    print('Accuracy: {:.2f}%'.format(total_acc.cpu().numpy()*100))<jupyter_output>Accuracy: 98.77%<jupyter_text>CNN model save & load save path settings<jupyter_code>PATH = os.path.join(cwd , './cnn_weights/')

if os.path.exists(PATH):
    print('Already exists')
else:
    os.makedirs(PATH)<jupyter_output>Already exists<jupyter_text>method 1<jupyter_code>torch.save(model, PATH + '/model.pt')  # total model save<jupyter_output>C:\ProgramData\Anaconda3\envs\pytorch36\lib\site-packages\torch\serialization.py:256: UserWarning: Couldn't retrieve source code for container of type CNN. It won't be checked for correctness upon loading.
  "type " + obj.__name__ + ". It won't be checked "<jupyter_text>model check<jupyter_code>saved_model = torch.load(PATH + '/model.pt')
saved_model<jupyter_output><empty_output><jupyter_text>weights check<jupyter_code>model.conv1.weight.data[0]
saved_model.conv1.weight.data[0]
with torch.no_grad():
    saved_model.eval()
    
    total_acc = 0
    
    for X, Y in mymnist.test_data_loader:
        X = X.view(-1, 1, 28, 28).to(device)
        Y = Y.to(device)
        
        prediction = saved_model(X)
        correct_prediction = torch.argmax(prediction, 1) == Y
        
        total_acc += sum(correct_prediction.float()) / data_len
    
    print('Accuracy: {:.2f}%'.format(total_acc.cpu().numpy()*100))<jupyter_output>Accuracy: 98.77%<jupyter_text>method 2<jupyter_code>torch.save(model.state_dict(), PATH + '/model_state_dict.pt')  # state_dict save
saved_model2 = CNN().to(device)
saved_model2.load_state_dict(torch.load(PATH + '/model_state_dict.pt'))
saved_model2.conv1.weight.data[0]<jupyter_output><empty_output><jupyter_text>check model<jupyter_code>with torch.no_grad():
    saved_model2.eval()
    
    total_acc = 0
    
    for X, Y in mymnist.test_data_loader:
        X = X.view(-1, 1, 28, 28).to(device)
        Y = Y.to(device)
        
        prediction = saved_model2(X)
        correct_prediction = torch.argmax(prediction, 1) == Y
        
        total_acc += sum(correct_prediction.float()) / data_len
    
    print('Accuracy: {:.2f}%'.format(total_acc.cpu().numpy()*100))<jupyter_output>Accuracy: 98.77%<jupyter_text>method 3<jupyter_code>torch.save({
    'epoch': epochs,
    'model': model.state_dict(),
    'optimizer': optimizer.state_dict(),
    'loss': avg_loss
}, PATH + '/all.tar')  # hyperparameter + parameters save
saved_model3 = CNN().to(device)
optimizer = optim.Adam(saved_model3.parameters())
checkpoint = torch.load(PATH + '/all.tar')   # dict 불러오기
saved_model3.load_state_dict(checkpoint['model'])
optimizer.load_state_dict(checkpoint['optimizer'])
epoch = checkpoint['epoch']
loss = checkpoint['loss']
epoch
loss
optimizer<jupyter_output><empty_output><jupyter_text>check model<jupyter_code>with torch.no_grad():
    saved_model3.eval()
    
    total_acc = 0
    
    for X, Y in mymnist.test_data_loader:
        X = X.view(-1, 1, 28, 28).to(device)
        Y = Y.to(device)
        
        prediction = saved_model3(X)
        correct_prediction = torch.argmax(prediction, 1) == Y
        
        total_acc += sum(correct_prediction.float()) / data_len
    
    print('Accuracy: {:.2f}%'.format(total_acc.cpu().numpy()*100))<jupyter_output>Accuracy: 98.77%
{'fraction_non_alphanumeric': 0.13789566236811254, 'fraction_numerical': 0.041031652989449004, 'mean_word_length': 3.5409181636726546, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '577074', 'license': 'MIT', 'n_tokens_mistral': 2768, 'n_tokens_neox': 2579, 'n_words': 438, 'path': 'Pytorch/pytorch_basic/model_save_and_load.ipynb', 'repo_name': 'IzPerfect/My-dictionary', 'size': '6824'}