【发布时间】:2018-12-22 23:38:39
【问题描述】:
我想使用 1D-Conv 层和 LSTM 层来分类 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)。
sn-p代码如下:
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) 遍历每个时间步长,如下图所示。
谢谢。
【问题讨论】:
-
难道你需要使用“TimeDistributed(Dense(1”)而不是“Dense(1”)?
-
回答您问题的最后一部分。由于数学运算的性质,理论上卷积将输入减少了一定的因素。为了解决这个问题,您需要使用填充。即在 CONV1D 中设置填充
padding='same' -
@GurmeetSingh 要应用
TimeDistributed,LSTM 层的return_sequences参数必须等于True。即使在这样做之后TimeDistributed(Dense(1))与Dense(1)相同。
标签: python keras time-series conv-neural-network lstm