# 第9章 TensorFlowとKeras¶

## 9.2 Keras によるMNIST 分類モデルの開発¶

[自分へのメモ] パッケージやライブラリは必要な段階で読み込む。またコードは自分がわかりやすいように変更するので、元本と同じとは限らない。

In [1]:
# list 9.1, 9.2
# p.182
import numpy as np
import tensorflow as tf

(X_train, Y_train), (X_test, Y_test) = tf.keras.datasets.mnist.load_data()

X_train, X_test = X_train / 255, X_test / 255
In [2]:
print(X_train.shape, Y_train.shape)
print(X_test.shape, Y_test.shape)
(60000, 28, 28) (60000,)
(10000, 28, 28) (10000,)
In [9]:
# list 9.3 モデルの作成
# p.182
# [自分へのメモ] tf.keras.models.Sequential よりは tf.keras.Sequential が自分では一般的

model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=X_train.shape[1:3]),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
In [10]:
model.summary()
Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
flatten_3 (Flatten)          (None, 784)               0
_________________________________________________________________
dense_9 (Dense)              (None, 64)                50240
_________________________________________________________________
dense_10 (Dense)             (None, 32)                2080
_________________________________________________________________
dense_11 (Dense)             (None, 10)                330
=================================================================
Total params: 52,650
Trainable params: 52,650
Non-trainable params: 0
_________________________________________________________________
In [12]:
# list 9.4
# p.183
model.compile(
optimizer=tf.keras.optimizers.SGD(learning_rate=0.01),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
In [13]:
# list 9.7 モデルの学習
# p.184
model.fit(
X_train,
Y_train,
batch_size=50,
epochs=20,
validation_data=(X_test, Y_test)
)
Train on 60000 samples, validate on 10000 samples
Epoch 1/20
60000/60000 [==============================] - 3s 50us/sample - loss: 0.8977 - accuracy: 0.7533 - val_loss: 0.4007 - val_accuracy: 0.8906
Epoch 2/20
60000/60000 [==============================] - 3s 49us/sample - loss: 0.3662 - accuracy: 0.8947 - val_loss: 0.3168 - val_accuracy: 0.9105
Epoch 3/20
60000/60000 [==============================] - 3s 51us/sample - loss: 0.3066 - accuracy: 0.9121 - val_loss: 0.2841 - val_accuracy: 0.9194
Epoch 4/20
60000/60000 [==============================] - 3s 56us/sample - loss: 0.2730 - accuracy: 0.9220 - val_loss: 0.2521 - val_accuracy: 0.9273
Epoch 5/20
60000/60000 [==============================] - 3s 57us/sample - loss: 0.2481 - accuracy: 0.9301 - val_loss: 0.2396 - val_accuracy: 0.9319
Epoch 6/20
60000/60000 [==============================] - 4s 59us/sample - loss: 0.2278 - accuracy: 0.9347 - val_loss: 0.2167 - val_accuracy: 0.9363
Epoch 7/20
60000/60000 [==============================] - 4s 61us/sample - loss: 0.2103 - accuracy: 0.9401 - val_loss: 0.2015 - val_accuracy: 0.9417
Epoch 8/20
60000/60000 [==============================] - 4s 66us/sample - loss: 0.1957 - accuracy: 0.9446 - val_loss: 0.1917 - val_accuracy: 0.9457
Epoch 9/20
60000/60000 [==============================] - 4s 68us/sample - loss: 0.1825 - accuracy: 0.9478 - val_loss: 0.1791 - val_accuracy: 0.9487
Epoch 10/20
60000/60000 [==============================] - 5s 78us/sample - loss: 0.1711 - accuracy: 0.9517 - val_loss: 0.1679 - val_accuracy: 0.9510
Epoch 11/20
60000/60000 [==============================] - 6s 92us/sample - loss: 0.1614 - accuracy: 0.9537 - val_loss: 0.1629 - val_accuracy: 0.9527
Epoch 12/20
60000/60000 [==============================] - 6s 95us/sample - loss: 0.1522 - accuracy: 0.9568 - val_loss: 0.1531 - val_accuracy: 0.9554
Epoch 13/20
60000/60000 [==============================] - 5s 89us/sample - loss: 0.1439 - accuracy: 0.9589 - val_loss: 0.1489 - val_accuracy: 0.9551
Epoch 14/20
60000/60000 [==============================] - 6s 94us/sample - loss: 0.1374 - accuracy: 0.9612 - val_loss: 0.1426 - val_accuracy: 0.9581
Epoch 15/20
60000/60000 [==============================] - 5s 90us/sample - loss: 0.1304 - accuracy: 0.9633 - val_loss: 0.1399 - val_accuracy: 0.9578
Epoch 16/20
60000/60000 [==============================] - 6s 103us/sample - loss: 0.1244 - accuracy: 0.9646 - val_loss: 0.1368 - val_accuracy: 0.9591
Epoch 17/20
60000/60000 [==============================] - 6s 96us/sample - loss: 0.1188 - accuracy: 0.9662 - val_loss: 0.1285 - val_accuracy: 0.9615
Epoch 18/20
60000/60000 [==============================] - 7s 116us/sample - loss: 0.1139 - accuracy: 0.9674 - val_loss: 0.1304 - val_accuracy: 0.9618
Epoch 19/20
60000/60000 [==============================] - 5s 91us/sample - loss: 0.1089 - accuracy: 0.9687 - val_loss: 0.1212 - val_accuracy: 0.9631
Epoch 20/20
60000/60000 [==============================] - 5s 87us/sample - loss: 0.1045 - accuracy: 0.9705 - val_loss: 0.1191 - val_accuracy: 0.9647
Out[13]:
<tensorflow.python.keras.callbacks.History at 0x7f01e1d16940>
In [14]:
# list 9.8 画像の表示
# p.186
image_index = 1
In [16]:
%matplotlib inline
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1,1,figsize=(4,4))
ax.imshow(X_test[image_index], cmap=plt.cm.gray)
ax.axis('off')
plt.show()
In [17]:
# list 9.9 推論
# p.187
Y_hat = model.predict(X_test)
print('Y_hat.shape: ', Y_hat.shape)
print('Y_hat[image_index]: \n', Y_hat[image_index])
Y_hat.shape:  (10000, 10)
Y_hat[image_index]:
[3.2581138e-06 5.9594342e-04 9.9263728e-01 4.6362304e-03 9.0929442e-09
1.4341930e-03 1.3373760e-04 2.1154263e-09 5.5936468e-04 4.2886517e-10]
In [18]:
# list 9.10 分類
# p.187
classified_class = np.argmax(Y_hat[image_index])
score = np.max(Y_hat[image_index])

print(f'Prediction: {classified_class}  score: {score * 100:.2f} %')
Prediction: 2  score: 99.26 %
In [19]:
# list 9.11 モデルの保存
# p.287
model.save('../../nitta/models/mnist_ch09_01.h5')
In [21]:
# list 9.13 logits 版のモデルを保存
model.layers[-1].activation = tf.keras.activations.linear
model.save('../../nitta/models/mnist_logits_ch09_01.h5')
In [22]:
model.summary()
Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
flatten_3 (Flatten)          (None, 784)               0
_________________________________________________________________
dense_9 (Dense)              (None, 64)                50240
_________________________________________________________________
dense_10 (Dense)             (None, 32)                2080
_________________________________________________________________
dense_11 (Dense)             (None, 10)                330
=================================================================
Total params: 52,650
Trainable params: 52,650
Non-trainable params: 0
_________________________________________________________________
In [ ]: