【问题标题】:Understanding Weights shape of an LSTM cell with 2-D input tensor了解具有二维输入张量的 LSTM 单元的权重形状
【发布时间】:2018-09-04 11:05:21
【问题描述】:

我正在构建一个简单的 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'])

在这里,我的输入是一个形状为 (8,8) 的 ndarray。从这个网络的训练模型中,当我转储权重时,我得到的值是:

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_iW_fW_cW_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) 的形状似乎是不正确的。只有矢量输入形状似乎符合上述等式。有人可以帮我理解上面输入形状为二维的方程吗?

TIA

【问题讨论】:

    标签: python keras lstm rnn mnist


    【解决方案1】:

    您提到的等式用于计算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 时间步产生的状态给出输出.

    【讨论】:

    • 但是input_shape 是 (8,8)。这是否意味着整个输入在 8 个t 时间步内得到处理?
    • @RachitAgrawal 没错。 LSTM 的输入是时间序列/序列;因此,它的输入形状为(n_timesteps, n_features)(8,8) 的输入形状意味着每个训练样本由 8 个时间步长组成,其中每个时间步长是一个长度为 8 的特征向量。
    • 好的。知道了。错过了input_shape = (batch, timesteps, features)的解释。谢谢。
    猜你喜欢
    • 2020-08-21
    • 2017-01-12
    • 1970-01-01
    • 2023-03-22
    • 2021-10-15
    • 2020-01-05
    • 2019-02-21
    • 1970-01-01
    相关资源
    最近更新 更多