堆叠循环神经网络,又叫深层循环神经网络,就是将多个循环神经网络堆叠起来。在TensorFlow中,提供了tf.keras.layers.StackedRNNCells()类来封装堆叠神经网络的单元。要注意的是,这相当于是神经元,还要通过tf.keras.layers.RNN()封装成一个网络层才能正常使用。
下图对堆叠循环神经网络的解释:
左侧为堆叠循环神经网络,右侧为展开图。
代码如下:
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train=x_train/255.
x_test=x_test/255.
batch_size=128
cell_size=32
lstm_cells = [tf.keras.layers.LSTMCell(cell_size) for _ in range(2)]
stacked_lstm = tf.keras.layers.StackedRNNCells(lstm_cells)
stacked_lstm_layer = tf.keras.layers.RNN(stacked_lstm)
model = tf.keras.Sequential()
model.add(stacked_lstm_layer)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(10,activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=batch_size, epochs=3)
loss, accuracy = model.evaluate(x_test, y_test)
print('test loss', loss)
print('test accuracy', accuracy)