[英]How to setup 1D-Convolution and LSTM in Keras
我想在 LSTM 层之后使用 1D-Conv 层来对 16 通道 400 时间步长的信号进行分类。
输入形状由以下部分组成:
X = (n_samples, n_timesteps, n_features)
,其中n_samples=476
, n_timesteps=400
, n_features=16
是信号的样本数、时间步长和特征(或通道)数。
y = (n_samples, n_timesteps, 1)
。 每个时间步都标有 0 或 1(二元分类)。
我使用1D-Conv提取时间信息,如下图所示。 F=32
和K=8
是过滤器和 kernel_size。 1D-MaxPooling 在 1D-Conv 之后使用。 32 单元 LSTM 用于信号分类。 该模型应返回y_pred = (n_samples, n_timesteps, 1)
。
代码片段如下所示:
input_layer = Input(shape=(dataset.n_timestep, dataset.n_feature))
conv1 = Conv1D(filters=32,
kernel_size=8,
strides=1,
activation='relu')(input_layer)
pool1 = MaxPooling1D(pool_size=4)(conv1)
lstm1 = LSTM(32)(pool1)
output_layer = Dense(1, activation='sigmoid')(lstm1)
model = Model(inputs=input_layer, outputs=output_layer)
模型摘要如下所示:
但是,我收到以下错误:
ValueError: Error when checking target: expected dense_15 to have 2 dimensions, but got array with shape (476, 400, 1).
我想问题是形状不正确。 请让我知道如何修复它。
另一个问题是时间步数。 因为input_shape
是在 1D-Conv 中赋值的,我们如何让 LSTM 知道 timestep 必须是 400?
我想根据@today 的建议添加模型图。 在这种情况下,LSTM 的时间步长将是 98。在这种情况下,我们是否需要使用 TimeDistributed? 我未能在 Conv1D 中应用 TimeDistributed。
无论如何要执行通道之间的卷积,而不是时间步长? 例如,过滤器 (2, 1) 遍历每个时间步,如下图所示。
谢谢。
如果你想为每个时间步长预测一个值,我会想到两个略有不同的解决方案:
1)删除MaxPooling1D
图层,将padding='same'
参数添加到Conv1D
图层,并将return_sequence=True
参数添加到LSTM,以便LSTM返回每个时间步的输出:
from keras.layers import Input, Dense, LSTM, MaxPooling1D, Conv1D
from keras.models import Model
input_layer = Input(shape=(400, 16))
conv1 = Conv1D(filters=32,
kernel_size=8,
strides=1,
activation='relu',
padding='same')(input_layer)
lstm1 = LSTM(32, return_sequences=True)(conv1)
output_layer = Dense(1, activation='sigmoid')(lstm1)
model = Model(inputs=input_layer, outputs=output_layer)
model.summary()
模型摘要将是:
Layer (type) Output Shape Param #
=================================================================
input_4 (InputLayer) (None, 400, 16) 0
_________________________________________________________________
conv1d_4 (Conv1D) (None, 400, 32) 4128
_________________________________________________________________
lstm_4 (LSTM) (None, 400, 32) 8320
_________________________________________________________________
dense_4 (Dense) (None, 400, 1) 33
=================================================================
Total params: 12,481
Trainable params: 12,481
Non-trainable params: 0
_________________________________________________________________
2)只需将Dense图层中的单位数更改为400,然后将y
重塑为(n_samples, n_timesteps)
:
from keras.layers import Input, Dense, LSTM, MaxPooling1D, Conv1D
from keras.models import Model
input_layer = Input(shape=(400, 16))
conv1 = Conv1D(filters=32,
kernel_size=8,
strides=1,
activation='relu')(input_layer)
pool1 = MaxPooling1D(pool_size=4)(conv1)
lstm1 = LSTM(32)(pool1)
output_layer = Dense(400, activation='sigmoid')(lstm1)
model = Model(inputs=input_layer, outputs=output_layer)
model.summary()
模型摘要将是:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_6 (InputLayer) (None, 400, 16) 0
_________________________________________________________________
conv1d_6 (Conv1D) (None, 393, 32) 4128
_________________________________________________________________
max_pooling1d_5 (MaxPooling1 (None, 98, 32) 0
_________________________________________________________________
lstm_6 (LSTM) (None, 32) 8320
_________________________________________________________________
dense_6 (Dense) (None, 400) 13200
=================================================================
Total params: 25,648
Trainable params: 25,648
Non-trainable params: 0
_________________________________________________________________
不要忘记,在这两种情况下,您都必须使用'binary_crossentropy'
(而不是'categorical_crossentropy'
)作为损失函数。 我希望这个解决方案的准确度低于解决方案#1; 但您必须尝试两者并尝试更改参数,因为它完全取决于您尝试解决的具体问题以及您拥有的数据的性质。
更新:
您要求的卷积层仅覆盖一个时间步长和k个相邻要素。 是的,你可以使用Conv2D层来做到这一点:
# first add an axis to your data
X = np.expand_dims(X) # now X has a shape of (n_samples, n_timesteps, n_feats, 1)
# adjust input layer shape ...
conv2 = Conv2D(n_filters, (1, k), ...) # covers one timestep and k features
# adjust other layers according to the output of convolution layer...
虽然我不知道你为什么要这样做,但是为了使用卷积层的输出(即(?, n_timesteps, n_features, n_filters)
,一种解决方案是使用包裹在TimeDistributed层内的LSTM层。或者另一种解决方案。解决方案是压扁最后两个轴。
输入和输出形状是(476,400,16)和(476,1) - 这意味着它只是每个完整序列输出一个值。
你的LSTM没有撤回序列(return_sequences = False)。 但即使你在LSTM之前进行Conv1D和MaxPooling也会挤压输入。 所以LSTM本身将获得(98,32)的样本。
我假设你想为每个输入步骤输出一个输出。
假设Conv1D和MaxPooling对于输入数据是相关的,您可以尝试seq到seq方法,其中您将第一个N / W的输出提供给另一个网络以获得400个输出。
我建议你看一下像编码器解码器seq2seq网络这样的模型,如下所示
https://blog.keras.io/a-ten-minute-introduction-to-sequence-to-sequence-learning-in-keras.html
我需要一些关于深度学习时间序列预测模型的帮助,我做了一个预测,但在我执行时它没有出现在图表中。 戈弗雷
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.