<jupyter_start><jupyter_text>51.2 MNIST 학습하기<jupyter_code>import matplotlib.pyplot as plt

import numpy as np

import mytorch
from mytorch import DataLoader
from mytorch import optimizers
from mytorch import functions as F
from mytorch.models import MLP

max_epoch = 30
batch_size = 100
hidden_size = 100
train_set = mytorch.datasets.MNIST(train=True)
test_set = mytorch.datasets.MNIST(train=False)

train_loader = DataLoader(train_set, batch_size)
test_loader = DataLoader(test_set, batch_size, shuffle=False)

model = MLP((hidden_size, 10))
optim = optimizers.SGD(model.parameters)


train_loss_saver = []
test_loss_saver = []
train_acc_saver = []
test_acc_saver = []

for epoch in range(max_epoch):
    sum_loss, sum_acc = 0, 0
    for x, t in train_loader:
        pred = model(x)
        loss = F.softmax_cross_entropy(pred, t)
        acc = F.accuracy(pred, t)
        
        model.zerograd()
        loss.backward()
        optim.step()
        
        sum_loss += float(loss.data) * len(t)
        sum_acc += float(acc.data) * len(t)
    train_loss_saver.append((epoch, sum_loss / len(train_set)))
    train_acc_saver.append((epoch, sum_acc / len(train_set)))
    print(f"[{epoch+1}/{max_epoch}] train_loss: {sum_loss / len(train_set)}, "
          f"accuracy: {sum_acc / len(train_set)}")
    
    sum_loss, sum_acc = 0, 0
    with mytorch.no_grad():
        for x, t in test_loader:
            pred = model(x)
            loss = mytorch.functions.softmax_cross_entropy(pred, t)
            acc = mytorch.functions.accuracy(pred, t)
            sum_loss += float(loss.data) * len(t)
            sum_acc += float(acc.data) * len(t)
        test_loss_saver.append((epoch, sum_loss / len(test_set)))
        test_acc_saver.append((epoch, sum_acc / len(test_set)))
        print(f"test_loss: {sum_loss / len(test_set)}, "
              f"accuracy: {sum_acc / len(test_set)}")
        
fig, ax = plt.subplots(figsize=(15, 8), nrows=1, ncols=2)
train_loss_saver = np.array(train_loss_saver)
test_loss_saver = np.array(test_loss_saver)
ax[0].plot(train_loss_saver[:, 0], train_loss_saver[:, 1], label='train_loss')
ax[0].plot(test_loss_saver[:, 0], test_loss_saver[:, 1], label='test_loss')
ax[0].set_xlabel("epoch")
ax[0].set_ylabel("loss")
ax[0].legend()

train_acc_saver = np.array(train_acc_saver)
test_acc_saver = np.array(test_acc_saver)
ax[1].plot(train_acc_saver[:, 0], train_acc_saver[:, 1], label='train_acc')
ax[1].plot(test_acc_saver[:, 0], test_acc_saver[:, 1], label='test_acc')
ax[1].set_xlabel("epoch")
ax[1].set_ylabel("acc")
ax[1].legend()
plt.show()
import matplotlib.pyplot as plt

import numpy as np

import mytorch
from mytorch import DataLoader
from mytorch import optimizers
from mytorch import functions as F
from mytorch.models import MLP

max_epoch = 30
batch_size = 100
hidden_size = 100
train_set = mytorch.datasets.MNIST(train=True)
test_set = mytorch.datasets.MNIST(train=False)

train_loader = DataLoader(train_set, batch_size)
test_loader = DataLoader(test_set, batch_size, shuffle=False)

model = MLP((hidden_size, hidden_size, 10), activation=F.relu)
optim = optimizers.SGD(model.parameters)


relu_train_loss_saver = []
relu_test_loss_saver = []
relu_train_acc_saver = []
relu_test_acc_saver = []

for epoch in range(max_epoch):
    sum_loss, sum_acc = 0, 0
    for x, t in train_loader:
        pred = model(x)
        loss = F.softmax_cross_entropy(pred, t)
        acc = F.accuracy(pred, t)
        
        model.zerograd()
        loss.backward()
        optim.step()
        
        sum_loss += float(loss.data) * len(t)
        sum_acc += float(acc.data) * len(t)
    relu_train_loss_saver.append((epoch, sum_loss / len(train_set)))
    relu_train_acc_saver.append((epoch, sum_acc / len(train_set)))
    print(f"[{epoch+1}/{max_epoch}] train_loss: {sum_loss / len(train_set)}, "
          f"accuracy: {sum_acc / len(train_set)}")
    
    sum_loss, sum_acc = 0, 0
    with mytorch.no_grad():
        for x, t in test_loader:
            pred = model(x)
            loss = mytorch.functions.softmax_cross_entropy(pred, t)
            acc = mytorch.functions.accuracy(pred, t)
            sum_loss += float(loss.data) * len(t)
            sum_acc += float(acc.data) * len(t)
        relu_test_loss_saver.append((epoch, sum_loss / len(test_set)))
        relu_test_acc_saver.append((epoch, sum_acc / len(test_set)))
        print(f"test_loss: {sum_loss / len(test_set)}, "
              f"accuracy: {sum_acc / len(test_set)}")
        
fig, ax = plt.subplots(figsize=(15, 8), nrows=1, ncols=2)
relu_train_loss_saver = np.array(relu_train_loss_saver)
relu_test_loss_saver = np.array(relu_test_loss_saver)
ax[0].plot(relu_train_loss_saver[:, 0], relu_train_loss_saver[:, 1], label='relu_train_loss')
ax[0].plot(relu_test_loss_saver[:, 0], relu_test_loss_saver[:, 1], label='relu_test_loss')
ax[0].plot(train_loss_saver[:, 0], train_loss_saver[:, 1], label='train_loss')
ax[0].plot(test_loss_saver[:, 0], test_loss_saver[:, 1], label='test_loss')
ax[0].set_xlabel("epoch")
ax[0].set_ylabel("loss")
ax[0].legend()

relu_train_acc_saver = np.array(relu_train_acc_saver)
relu_test_acc_saver = np.array(relu_test_acc_saver)
ax[1].plot(relu_train_acc_saver[:, 0], relu_train_acc_saver[:, 1], label='relu_train_acc')
ax[1].plot(relu_test_acc_saver[:, 0], relu_test_acc_saver[:, 1], label='relu_test_acc')
ax[1].plot(train_acc_saver[:, 0], train_acc_saver[:, 1], label='train_acc')
ax[1].plot(test_acc_saver[:, 0], test_acc_saver[:, 1], label='test_acc')
ax[1].set_xlabel("epoch")
ax[1].set_ylabel("acc")
ax[1].legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.144184384491692, 'fraction_numerical': 0.01625871002322673, 'mean_word_length': 4.202602230483271, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '208695', 'license': 'MIT', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2205, 'n_words': 374, 'path': 'steps/step51.ipynb', 'repo_name': 'choiking10/mytorch', 'size': '5597'}