在上一篇博文里,介绍了如何用CNN来对手写数字进行识别,CNN确实是图片识别的一个比较不错的选择,但是作为学习者,一定要多尝试,多coding,所以,在这一篇博文里,跟随作者一块学习一下如何用RNN来识别手写数字~
如果有不明白RNN原理的小伙伴,可以看一下Ng的关于RNN的视频,视频戳这里!
导入我们需要的包,注意,这里需要导入SimpleRNN,SimpleRNN是无隐藏层的循环神经网络。
from keras.layers import SimpleRNN, Activation, Dense
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.optimizers import Adam
导入数据,处理数据,观察数据。
from keras.datasets import mnist
# download the mnist to the path '~/.keras/datasets/' if it is the first time to be called
# X shape (60,000 28x28), y shape (10,000, )
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# data pre-processing
X_train = X_train.reshape(-1,28,28) / 255.
X_test = X_test.reshape(-1,28,28) / 255. # normalize
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
print(X_train[1].shape)
print(y_train[:3])
(28, 28)
[[ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]]
设置一些后边需要的参数:
TIME_STEPS = 28
INPUT_SIZE = 28
BATCH_SIZE = 50
BATCH_INDEX = 0
OUTPUT_SIZE = 10
CELL_SIZE = 50
LR = 0.001
开始搭建模型:
model = Sequential()
model.add(SimpleRNN(
# for batch_input_shape, if using tensorflow as the backend, we have to put None for the batch_size.
# Otherwise, model.evaluate() will get error.
batch_input_shape=(None, TIME_STEPS, INPUT_SIZE),
output_dim=CELL_SIZE,
unroll=True,
))
model.add(Dense(OUTPUT_SIZE))
model.add(Activation('softmax'))
设置adam优化方法,loss函数,metrics方法来观察输出结果:
adam = Adam(LR)
model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'])
下面开始训练网络,想一想就有点小激动:
for step in range(4001):
# data shape = (batch_num, steps, inputs/outputs)
X_batch = X_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, :, :]
Y_batch = y_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, :]
cost = model.train_on_batch(X_batch, Y_batch)
BATCH_INDEX += BATCH_SIZE
BATCH_INDEX = 0 if BATCH_INDEX >= X_train.shape[0] else BATCH_INDEX
if step % 500 == 0:
cost, accuracy = model.evaluate(X_test, y_test, batch_size=y_test.shape[0], verbose=False)
print('test cost is ', cost, 'test accuracy is ', accuracy)
test cost is 2.41335225105 test accuracy is 0.0386999994516
test cost is 0.629632055759 test accuracy is 0.810100018978
test cost is 0.425886720419 test accuracy is 0.876699984074
test cost is 0.353266894817 test accuracy is 0.899600028992
test cost is 0.325611412525 test accuracy is 0.905900001526
test cost is 0.269144177437 test accuracy is 0.92150002718
test cost is 0.287819743156 test accuracy is 0.91890001297
test cost is 0.236630275846 test accuracy is 0.931500017643
test cost is 0.20982426405 test accuracy is 0.938099980354
训练完成,准确率0.9381,如果想提高准确率,可以将step设置的大一些。
搞定!!!第四个keras案例到此结束,如果有疑问,私信博主或者参考keras中文文档。