如何在Keras中设置1D卷积和LSTM

Thu*_* N. 6 python time-series conv-neural-network lstm keras

我想在LSTM层之后使用1D-Conv层对16通道400步距信号进行分类。

输入形状包括:

  • X = (n_samples, n_timesteps, n_features)其中n_samples=476n_timesteps=400n_features=16是信号的样本,时间步长,和特征(或通道)的数量。

  • y = (n_samples, n_timesteps, 1)。每个时间步标记为0或1(二进制分类)。

我使用1D-Conv提取时间信息,如下图所示。F=32K=8是过滤器和kernel_size。1D-Conv之后使用1D-MaxPooling。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) 
Run Code Online (Sandbox Code Playgroud)

模型摘要如下所示:

在此处输入图片说明

但是,出现以下错误:

ValueError: Error when checking target: expected dense_15 to have 2 dimensions, but got array with shape (476, 400, 1).
Run Code Online (Sandbox Code Playgroud)

我想问题是形状不正确。请让我知道如何解决它。

另一个问题是时间步数。因为input_shape是在1D转换中指定的,我们如何让LSTM知道时间步长必须是400?


我想根据@today的建议添加模型图。在这种情况下,LSTM的时间步为98。在这种情况下,我们是否需要使用TimeDistributed?我未能在Conv1D中应用TimeDistributed。

在此处输入图片说明

无论如何,是否有执行通道间卷积的方法,而不是时间步长?例如,过滤器(2,1)遍历每个时间步,如下图所示。 在此处输入图片说明

谢谢。

tod*_*day 8

如果您想为每个时间步预测一个值,我会想到两种略有不同的解决方案:

1)移除MaxPooling1D层,padding='same'Conv1Dlayer加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()
Run Code Online (Sandbox Code Playgroud)

模型摘要将是:

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
_________________________________________________________________
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

模型摘要将是:

_________________________________________________________________
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
_________________________________________________________________
Run Code Online (Sandbox Code Playgroud)

不要忘记,在这两种情况下,您都必须使用'binary_crossentropy'(not '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...
Run Code Online (Sandbox Code Playgroud)

虽然我不知道您为什么要这样做,但要使用卷积层的输出(即(?, n_timesteps, n_features, n_filters),一种解决方案是使用包裹在 TimeDistributed 层内的 LSTM 层。或者另一种解决方案是展平最后两个轴。