2層フィードフォワードニューラルネットワークモデル
2層フィードフォワードニューラルネットワークモデルで、手書き数字のクラス分類問題がどれくらいできるのか試す。
import numpy as np
import matplotlib.pyplot as plt
import time
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import Adam
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_train = x_train.astype('float32')
x_train = x_train / 255
num_classes = 10
y_train = to_categorical(y_train, num_classes)
x_test = x_test.reshape(10000, 784)
x_test = x_test.astype('float32')
x_test = x_test / 255
y_test = to_categorical(y_test, num_classes)
np.random.seed(1)
# Sequentialモデルの作成
model = Sequential()
model.add(Dense(16, input_dim=784, activation='sigmoid'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy'])
# 学習
startTime = time.time()
history = model.fit(x_train, y_train, epochs=10, batch_size=1000, verbose=1, validation_data=(x_test, y_test))
# モデル評価
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
calculation_time = time.time() - startTime
print("Calculation time:{0:.3f} sec".format(calculation_time))
plt.figure(1, figsize=(10, 4))
plt.subplots_adjust(wspace=0.5)
plt.subplot(1, 2, 1)
plt.plot(history.history['loss'], label='training', color='black')
plt.plot(history.history['val_loss'], label='test', color='cornflowerblue')
plt.ylim(0, 10)
plt.legend()
plt.grid()
plt.xlabel('epoch')
plt.ylabel('loss')
plt.subplot(1, 2, 2)
plt.plot(history.history['accuracy'], 'black', label='training')
plt.plot(history.history['val_accuracy'], 'cornflowerblue', label='test')
plt.ylim(0, 1)
plt.legend()
plt.grid()
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.show()
左図が交差エントロピー誤差のグラフ、右図が正答率のグラフになる。
epoch(エポック)は学習の更新回数を決めるパラメータである。
このグラフから読み取れることは、テストデータからの誤差も単調に減少を続けているとわかり、オーバーフィッティングが発生していないというである。
また、正答率も順調に増加しているため、問題なく学習が行えている。
ただ、正答率は0.8~1.0の中間に位置するため0.9程だと言える。このとき、実際のテストデータを入力したときのモデルの出力を行う。
import numpy as np
import matplotlib.pyplot as plt
import time
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import Adam
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_train = x_train.astype('float32')
x_train = x_train / 255
num_classes = 10
y_train = to_categorical(y_train, num_classes)
x_test = x_test.reshape(10000, 784)
x_test = x_test.astype('float32')
x_test = x_test / 255
y_test = to_categorical(y_test, num_classes)
np.random.seed(1)
# Sequentialモデルの作成
model = Sequential()
model.add(Dense(16, input_dim=784, activation='sigmoid'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy'])
# 学習
startTime = time.time()
history = model.fit(x_train, y_train, epochs=10, batch_size=1000, verbose=1, validation_data=(x_test, y_test))
# モデル評価
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
calculation_time = time.time() - startTime
print("Calculation time:{0:.3f} sec".format(calculation_time))
def show_prediction():
n_show = 96
y = model.predict(x_test)
plt.figure(2, figsize=(12, 8))
plt.gray()
for i in range(n_show):
plt.subplot(8, 12, i + 1)
x = x_test[i, :]
x = x.reshape(28, 28)
plt.pcolor(1 - x)
wk = y[i, :]
prediction = np.argmax(wk)
plt.text(22, 25.5, "%d" % prediction, fontsize=12)
if prediction != np.argmax(y_test[i, :]):
plt.plot([0, 27], [1, 1], color='cornflowerblue', linewidth=5)
plt.xlim(0, 27)
plt.ylim(27, 0)
plt.xticks([], "")
plt.yticks([], "")
show_prediction()
plt.show()
96個分のデータを表示した。だいたい合っているように見えるが、8個の間違いが発生している。このとき7を2や2を6というような間違いをしているので、あまり精度が良いとは言えない。