好的,你的问题让我思考,我想我可能有些过头了,但是这里有一段代码片段,我用它来获取LSTM实现背后的一些见解。
from keras.layers import LSTM
from keras.models import Sequential
model = Sequential()
model.add(LSTM(10, input_shape=(20, 30), return_sequences=True))
model.compile(loss='mse',optimizer='adam', metrics=['accuracy'])
weights = model.get_weights()
现在,通过检查权重的形状,我们可以对正在发生的事情有所了解。
In [12]: weights[0].shape
Out[12]: (30, 40)
In [14]: weights[1].shape
Out[14]: (10, 40)
In [15]: weights[2].shape
Out[15]: (40,)
以下是它们的描述:
In [26]: model.weights
Out[26]:
[<tf.Variable 'lstm_4/kernel:0' shape=(30, 40) dtype=float32_ref>,
<tf.Variable 'lstm_4/recurrent_kernel:0' shape=(10, 40) dtype=float32_ref>,
<tf.Variable 'lstm_4/bias:0' shape=(40,) dtype=float32_ref>]
那些是唯一可用的权重。我还去看了Keras实现,网址为
https://github.com/keras-team/keras/blob/master/keras/layers/recurrent.py#L1765。因此,您可以看到@gorjan是正确的,它实现了一个单元格,意味着4个门(用于循环输入和序列输入),以及它们的偏置。这里的“层”思想应该应用于LSTM展开的次数,在这种情况下为30。希望这有所帮助。