import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from tensorflow.keras.layers import Dense
from keras.layers.recurrent import SimpleRNN,LSTM,GRU
from tensorflow.keras.optimizers import Adam
#设置输入
input_size =28
#序列长度一共有28行
time_steps=28
#隐藏层cell个数
cell_size =50
#载入数据
(x_train,y_train),(x_test,y_test)=mnist.load_data()
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)
#定义优化器
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('test loss',loss)
print('test accuracy',accuracy)
|