from keras.datasets import mnist
from keras.layers import LSTM, Dense
from keras.models import Sequential
from keras.utils import np_utils
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.astype('float32') / 255.
X_test = X_test.astype('float32') / 255.
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)
model = Sequential()
model.add(LSTM(30, input_shape=X_train.shape[1:]))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
model.fit(X_train, Y_train, nb_epoch=50, batch_size=128, shuffle=True, verbose=2)
=================================================================
lstm_1 (LSTM) (None, 30) 7080
_________________________________________________________________
dense_1 (Dense) (None, 10) 310
=================================================================
Total params: 7,390
Trainable params: 7,390
Non-trainable params: 0
这次只有一个新层LSTM
,他的输出维度是30,他适合于处理和预测时间序列中间隔和延迟相对较长的重要事件,具体怎么回事呢,后续理论部分在集中学。可以看到Keras还是非常方便的,要用LSTM的话直接一个函数就搞定了