【问题标题】:How lstm processes a different number of features from the previous layerlstm 如何处理与前一层不同数量的特征
【发布时间】:2020-10-15 11:39:26
【问题描述】:

例如,我们有输出=2 的 LSTM 层和两个输入形状的变体:

  1. (n_samples=1, n_timestep=1, n_features=4)

  2. (n_samples=1, n_timestep=1, n_features=2)

LSTM 如何处理这种不同的数字特征作为输入和常数特征作为输出?

代码示例:

inputs1 = tf.convert_to_tensor(tf.constant([[[0.1,0.2,0.3,0.4]]]), dtype=tf.float32)
##tf.Tensor([[[0.1 0.2 0.3 0.4]]], shape=(1, 1, 4), dtype=float32)


inputs2 = tf.convert_to_tensor(tf.constant([[[0.1,0.2]]]), dtype=tf.float32)
##tf.Tensor([[[0.1 0.2]]], shape=(1, 1, 2), dtype=float32)

output1 = tf.keras.layers.LSTM(2, return_sequences=True)(inputs1)
##tf.Tensor([[[-0.08766557 -0.01480309]]], shape=(1, 1, 2), dtype=float32)

output2 = tf.keras.layers.LSTM(2, return_sequences=True)(inputs2)
##tf.Tensor([[[-0.02775455  0.02273378]]], shape=(1, 1, 2), dtype=float32)

【问题讨论】:

    标签: python tensorflow machine-learning keras lstm


    【解决方案1】:

    它们唯一的区别是您的数据具有的时间步长,由最后一个维度(4 或 2)表示。 LSTM 本身会解开时间步,并在迭代每个时间步时反复计算您的输出值。这就是为什么 LSTM 需要不同长度的输入的原因

    【讨论】:

    • 但是timesteps的个数不是最后一个维度,因为最后一个维度是每个timestep的feature个数
    猜你喜欢
    • 1970-01-01
    • 2023-01-13
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2021-07-27
    • 1970-01-01
    • 2021-04-28
    相关资源
    最近更新 更多