【发布时间】: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