通过MNIST熟悉Keras——《TensorFlow 实战》读书笔记

Tensorflow 的使用者虽多,但真的很难用。幸亏有基于TF和Theano的高层框架Keras(不幸的是Theano已经停止更新了)。我们通过MNIST来熟悉一下Keras。

先推荐一个学习线性代数的教程http://www.bilibili.com/video/av6731067/,不管你多忙也请看上面这个视频。
3Blue1Brown制作,深入浅出、直观明了地分享数学之美。

下面的代码来源于keras examples

1
2
3
4
5
6
7
8
9
10
11
12
'''Trains a simple deep NN on the MNIST dataset.
Gets to 98.40% test accuracy after 20 epochs
(there is *a lot* of margin for parameter tuning).
2 seconds per epoch on a K520 GPU.
'''


import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
Using TensorFlow backend.

0x01 读取数据库

下面代码执行的时候会自动下载https://s3.amazonaws.com/img-datasets/mnist.npz~/.keras/datasets目录(自动下载过程不支持断点续传,如果一次下载没成功就会一直报错,可以用wget -c或其它下载软件进行下载)。如果你用Windows系统,请直接按Alt+F4

1
2
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

0x02 定义变量

很奇怪现在定义常量都不用大写字母了嘛>_<

1
2
3
4
5
6
7
8
9
10
11
12
batch_size = 128
num_classes = 10
epochs = 20

x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
60000 train samples
10000 test samples

0x03 配置模型

用Keras来配置模型真的很简单,一层一层的add进去就可以了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))

model.summary()

model.compile(loss='categorical_crossentropy',
optimizer=RMSprop(),
metrics=['accuracy'])
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 512)               401920    
_________________________________________________________________
dropout_1 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 512)               262656    
_________________________________________________________________
dropout_2 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 10)                5130      
=================================================================
Total params: 669,706
Trainable params: 669,706
Non-trainable params: 0
_________________________________________________________________

0x04 开始训练

1
2
3
4
5
6
7
8
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
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])
Train on 60000 samples, validate on 10000 samples
Epoch 1/20
60000/60000 [==============================] - 5s - loss: 0.2453 - acc: 0.9253 - val_loss: 0.0976 - val_acc: 0.9697
Epoch 2/20
60000/60000 [==============================] - 5s - loss: 0.1009 - acc: 0.9693 - val_loss: 0.0836 - val_acc: 0.9742
Epoch 3/20
60000/60000 [==============================] - 5s - loss: 0.0749 - acc: 0.9770 - val_loss: 0.0924 - val_acc: 0.9731
Epoch 4/20
60000/60000 [==============================] - 5s - loss: 0.0601 - acc: 0.9820 - val_loss: 0.0820 - val_acc: 0.9771
Epoch 5/20
...
Epoch 19/20
60000/60000 [==============================] - 5s - loss: 0.0181 - acc: 0.9953 - val_loss: 0.1228 - val_acc: 0.9829
Epoch 20/20
60000/60000 [==============================] - 5s - loss: 0.0192 - acc: 0.9951 - val_loss: 0.1171 - val_acc: 0.9828
Test loss: 0.117063564365
Test accuracy: 0.9828