我正在构建一个简单的LSTM模型如下:
model = Sequential()
model.add(LSTM(10, return_sequences = False, input_shape = (8, 8)))
model.add(Activation('softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['accuracy'])
在这里,我的输入是一个形状的ndarray(8,8)。从这个网络中训练有素的模型,当我转出权重时,我得到的值为:
print(model.layers.layer[0].get_weights[0].shape) # W [W_i, W_f, W_c, W_o]
print(model.layers.layer[0].get_weights[1].shape) # U
print(model.layers.layer[0].get_weights[2].shape) # b
输出:
(8, 40)
(10, 40)
(40,)
W是W_i
,W_f
,W_c
和W_o
的组合矩阵,每个都有(8, 10)
。但这与等式不符:
f_t = sigmoid( W_f * x + U_f * h_{t-1} + b_f )
如果我只采用上面等式的矩阵维数,它就像这样:
W_f' * x + U_f' * h_{t-1} + b_f
--> [10, 8] x [8, 8] + [10, 10] x [10, 1] + [10, 1]
--> [10, 8] + [10, 1] + [10, 1]
所以看看上面的等式,似乎X(input_tensor)
的形状是不正确的。只有矢量输入形状似乎符合上述等式。有人可以帮我理解输入形状为2-D的上述方程吗?
TIA
你提到的等式是用于计算t
-th时间步长的输出。因此,只使用时间步长t
的输入(即x_t
)而不是所有输入(即x
):
f_t = sigmoid( W_f * x_{t} + U_f * h_{t-1} + b_f )
结果我们会:
W_f' * x + U_f' * h_{t-1} + b_f
--> [10, 8] x [8, 1] + [10, 10] x [10, 1] + [10, 1]
--> [10, 1] + [10, 1] + [10, 1]
--> [10, 1] # output at timestep t
这与LSTM层的意图相一致:它们在时间步长t
处获得输入,并根据该输入给出输出,并通过处理第一个到(t-1)
-th时间步长得到状态。