blog

MNIST

Published:

By nob

Category: Posts

Tags: 機械学習 ニューラルネットワーク Kaggle MNIST Python

MNISTを識別してみる

データファイルを THE MNIST DATABASE of handwritten digits からダウンロードしようとすると、以下のようにエラーとなる。

pic-0

Kaggleでデータセットを公開している人がいる のでダウンロードしてみる。

データローダ が公開されている。

データの読み込み

import os
import random

import numpy as np
from kaggle_mnist import MnistDataloader

input_path = "./data/mnist"

dataloader = MnistDataloader(
    os.path.join(input_path, "train-images-idx3-ubyte"),
    os.path.join(input_path, "train-labels-idx1-ubyte"),
    os.path.join(input_path, "t10k-images-idx3-ubyte"),
    os.path.join(input_path, "t10k-labels-idx1-ubyte"),
)
(x_train_org, y_train_org), (x_test_org, y_test_org) = dataloader.load_data()


def transform_x(old_x):
    old_x = np.array(old_x) / 255
    return old_x.reshape(old_x.shape[0], old_x.shape[1] * old_x.shape[2])


def transform_y(old_y):
    old_y = np.array(old_y)
    new_y = np.zeros((old_y.shape[0], 10))
    new_y[np.arange(len(old_y)), old_y] = 1
    return new_y


x_train = transform_x(x_train_org)
y_train = transform_y(y_train_org)
x_test = transform_x(x_test_org)
y_test = transform_y(y_test_org)

識別

import numpy as np
from mlp import (
    ActivationLayer,
    FullyConnectedLayer,
    InputLayer,
    MeanSquaredError,
    MultiLayerPerceptron,
    Sigmoid,
)

np.set_printoptions(precision=3)

rng = np.random.default_rng(123)

mlp = MultiLayerPerceptron(
    [
        InputLayer(28 * 28),
        FullyConnectedLayer(28 * 28, 64, rng),
        ActivationLayer(Sigmoid()),
        FullyConnectedLayer(64, 10, rng),
        ActivationLayer(Sigmoid()),
    ],
    MeanSquaredError(),
    epochs=3,
    num_batch=3000,
    learning_rate=0.2,
)

mlp.fit(x_train, y_train)
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot()
ax.plot(mlp.history, marker="o", markersize=1)
ax.grid()
plt.minorticks_on()
plt.show()

fig-1

def accuracy(pred, truth):
    return np.sum(np.all(truth == pred, axis=1)) / pred.shape[0]
x_train_pred = mlp.predict(x_train)
x_test_pred = mlp.predict(x_test)
print("train score", len(x_train), accuracy(x_train_pred, y_train))
print("test score", len(x_test), accuracy(x_test_pred, y_test))
train score 60000 0.9424666666666667
test score 10000 0.93