RNN应用手写数字识别

import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
# SimpleRNN是最简单的,还有 LSTM, GRU
from keras.layers.recurrent import SimpleRNN
# 数据长度-一行有28个像素
input_size = 28
# 序列长度-一共有28行,每一行看成一个序列
time_steps = 28
# 隐藏层cell个数
cell_size = 50

# 载入数据
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 如果数据是(60000, 784)60000个样本没个样本784个像素,先转成(60000,28,28)
# (60000,28,28) 第一个维度是总的样本数,第二个维度是time_steps,第三个维度是input_size
x_train = x_train/255.0
x_test = x_test/255.0

# 换one hot格式
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)

# 创建模型
model = Sequential()

# 循环神经网络,一个隐藏层
model.add(SimpleRNN(
    units=cell_size,  # 输出
    input_shape=(time_steps, input_size)
))

# 输出层
model.add(Dense(10, activation='softmax'))

# 定义优化器
adam = Adam(lr=1e-4)
# 定义优化器,loss function,训练过程中计算准确率
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])

# 训练模型
model.fit(x_train, y_train, batch_size=64, epochs=10)

# 评估模型
loss,accuracy = model.evaluate(x_test, y_test)

print('\ntest loss', loss)
print('test accuracy', accuracy)

# 保存模型
model.save('model.h5') # HDF5文件,pip install h5py
上一篇:悟透前端:javascript数组之includes、reduce


下一篇:基于keras+VGG16的识别汪星人