在https://keras.io/layers/recurrent/ 上,我看到 LSTM 层有 akernel
和 a recurrent_kernel
。它们的含义是什么?根据我的理解,我们需要 LSTM 单元的 4 个门的权重。但是,在 keras 实现中,kernel
形状为 (input_dim, 4*units),recurrent_kernel
形状为 (units, 4*units)。那么,他们都以某种方式实现了门吗?
我可以理解如何乘以密集层权重以获得预测输出,但如何解释 LSTM 模型中的矩阵?
这里有一些玩具示例(不介意拟合,这只是矩阵乘法)
密集示例:
from keras.models import Model
from keras.layers import Input, Dense, LSTM
import numpy as np
np.random.seed(42)
X = np.array([[1, 2], [3, 4]])
I = Input(X.shape[1:])
D = Dense(2)(I)
linear_model = Model(inputs=[I], outputs=[D])
print('linear_model.predict:\n', linear_model.predict(X))
weight, bias = linear_model.layers[1].get_weights()
print('bias + X @ weights:\n', bias + X @ weight)
Run Code Online (Sandbox Code Playgroud)
输出:
linear_model.predict:
[[ 3.10299015 0.46077788]
[ 7.12412453 1.17058146]]
bias + X @ weights:
[[ 3.10299003 0.46077788]
[ 7.12412441 1.17058146]]
Run Code Online (Sandbox Code Playgroud)
LSTM 示例:
X = X.reshape(*X.shape, 1)
I = Input(X.shape[1:]) …
Run Code Online (Sandbox Code Playgroud)