如何在keras中使用Conv1D和Bidirectional LSTM对每个时间步进行多类分类?

How to use Conv1D and Bidirectional LSTM in keras to do multiclass classification of each timestep?

我正在尝试在 keras 中使用 Conv1D 和双向 LSTM(很像在 中)进行信号处理,但是对每个时间步长进行多重 class class 化.

问题在于,尽管 Conv1D 和 LSTM 使用的形状有些相同:

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

Conv1D 的输出为 = (length - (kernel_size - 1)/strides),因此不再匹配 LSTM 形状,即使不使用 MaxPooling1D 和 Dropout。

更具体地说,我的训练集 X 有 n 个样本,具有 1000 个时间步长和一个通道 (n_samples, 1000, 1),我使用了 LabelEncoder 和 OneHotEncoder,所以 y 有 n 个样本,1000 次步骤和 5 一个热编码 classes (n_samples, 1000, 5).

由于一个 class 比其他的更普遍(实际上是没有信号),我使用 loss='sparse_categorical_crossentropy'、sample_weight_mode="temporal" 和 sample_weight 为包含有意义的 classes.

的时间步赋予更高的权重
model = Sequential()
model.add(Conv1D(128, 3, strides=1, input_shape = (1000, 1), activation = 'relu'))
model.add(Bidirectional(LSTM(128, return_sequences=True)))
model.add(TimeDistributed(Dense(5, activation='softmax')))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['categorical_accuracy'], sample_weight_mode="temporal")
print(model.summary())

Model

当我尝试拟合模型时,我收到此错误消息:

Error when checking target: expected time_distributed_1 to have shape (None, 998, 1) but got array with shape (100, 1000, 5).

有没有办法让这样的神经网络配置起作用?

您的卷积正在切割序列的尖端。在卷积层中使用 padding='same'

不过,该消息似乎不适合您的模型。你的模型显然有 5 个输出特征(因为 Dense(5)),但消息说它期望 1 个。也许这是因为 "sparse" 交叉熵。根据数据格式,您可能应该使用 "categorical_crossentropy".