【问题标题】:Confused about how to combine CONV1D and LSTM对如何结合 CONV1D 和 LSTM 感到困惑
【发布时间】:2021-01-09 21:11:25
【问题描述】:

我很难理解这个结合了CONV1D 和LSTM 的代码。

model = tf.keras.models.Sequential([
  tf.keras.layers.Conv1D(filters=32, kernel_size=5,
                      strides=1, padding="causal",
                      activation="relu",
                      input_shape=[None, 1]),
  tf.keras.layers.LSTM(64, return_sequences=True),
  tf.keras.layers.LSTM(64, return_sequences=True),
  tf.keras.layers.Dense(30, activation="relu"),
  tf.keras.layers.Dense(10, activation="relu"),
  tf.keras.layers.Dense(1),
  tf.keras.layers.Lambda(lambda x: x * 400)
])

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d (Conv1D)              (None, None, 32)          192       
_________________________________________________________________
lstm (LSTM)                  (None, None, 64)          24832     
_________________________________________________________________
lstm_1 (LSTM)                (None, None, 64)          33024     
_________________________________________________________________
dense (Dense)                (None, None, 30)          1950      
_________________________________________________________________
dense_1 (Dense)              (None, None, 10)          310       
_________________________________________________________________
dense_2 (Dense)              (None, None, 1)           11        
_________________________________________________________________
lambda (Lambda)              (None, None, 1)           0         
=================================================================
Total params: 60,319
Trainable params: 60,319
Non-trainable params: 0
_________________________________________________________________

如果我们删除CONV1D 层和第二个LSTM 层,我完全没问题。但是,我对这种结构深感困惑。

首先,我们为什么要在第二个LSTM 层中添加return_sequences=True?通常我们只在第一个LSTM 层添加return_sequences=True,以防stacked LSTM。在此模型中,我们将其添加到两者中。

第二,为什么input_shape=[None, 1]在CONV1D层? CONV1D 和 LSTM 在形状上是不是有点相似?

Conv1D: (batch, length, channels)
LSTM: (batch, timeSteps, features)

如果您能分享一个解释如何组合这两种模型的链接,我们将不胜感激。

【问题讨论】:

    标签: python python-3.x keras lstm tf.keras


    【解决方案1】:

    此代码使用 sequence-to-sequence 预测,而不是 sequence-to-vector,如本视频(下方链接)中所述。这就是我们将return_sequences=True 放在两个LSTM 层中的原因。

    https://classroom.udacity.com/courses/ud187/lessons/6d543d5c-6b18-4ecf-9f0f-3fd034acd2cc/concepts/c10fb954-25ea-43e3-b22c-21b3e423eb05

    对于时间序列forecasting 使用neural networks,这是一门很棒的课程

    【讨论】:

      【解决方案2】:

      如果您希望输出是一个序列,例如,如果您想预测接下来的 10 个值,则最后一个 return_sequences 应该是 True。如果您的数据中有 10 个时间步长,它将返回 10 个时间步长的序列。例如:

      import numpy as np
      import tensorflow as tf
      
      model = tf.keras.models.Sequential([
        tf.keras.layers.Conv1D(filters=32, kernel_size=5,
                            strides=1, padding="causal",
                            activation="relu",
                            input_shape=[None, 1]),
        tf.keras.layers.LSTM(64, return_sequences=True),
        tf.keras.layers.LSTM(64, return_sequences=True),
        tf.keras.layers.Dense(30, activation="relu"),
        tf.keras.layers.Dense(10, activation="relu"),
        tf.keras.layers.Dense(1),
        tf.keras.layers.Lambda(lambda x: x * 400)
      ])
      
      model(np.random.rand(1, 10, 1))
      
      <tf.Tensor: shape=(1, 10, 1), dtype=float32, numpy=
      array([[[ 0.12037009],
              [ 0.19570792],
              [ 0.3986496 ],
              [ 0.56172705],
              [-0.06423644],
              [-0.4780491 ],
              [-0.82960564],
              [-1.2271142 ],
              [-1.8704925 ],
              [-2.294954  ]]], dtype=float32)>
      

      我不明白你的第二个问题。

      【讨论】:

      • 感谢 Nicolas 的回答。我明白你的意思。但我的问题是我们怎么可能将return_sequences=True 与dense 层结合起来?我们通常使用return_sequences=False。关于我的第二个问题,我不明白CONV1D层的input_shape。
      猜你喜欢
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-29
      相关资源
      最近更新 更多