简体   繁体   English

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

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

I am trying to use a Conv1D and Bidirectional LSTM in keras (much like in this question ) for signal processing, but doing a multiclass classification of each time step. 我正在尝试在keras中使用Conv1D和双向LSTM(非常类似于此问题 )进行信号处理,但是对每个时间步骤都进行了多类分类。

The problem is that even though the shapes used by Conv1D and LSTM are somewhat equivalent: 问题在于,即使Conv1D和LSTM使用的形状在某种程度上是等效的:

Conv1D: (batch, length, channels) Conv1D :(批次,长度,通道)
LSTM: (batch, timeSteps, features) LSTM :(批次,时间步长,功能)

The output of the Conv1D is = (length - (kernel_size - 1)/strides), and therefore doesn't match the LSTM shape anymore, even without using MaxPooling1D and Dropout. Conv1D的输出为=(长度-(kernel_size-1)/ strides),因此即使不使用MaxPooling1D和Dropout,也不再与LSTM形状匹配。

To be more specific, my training set X has n samples with 1000 time steps and one channel (n_samples, 1000, 1), and I used LabelEncoder and OneHotEncoder so y has n samples, 1000 time steps and 5 one hot encoded classes (n_samples, 1000, 5). 更具体地说,我的训练集X具有n个具有1000个时间步长和1个通道的样本(n_samples,1000、1),并且我使用了LabelEncoder和OneHotEncoder,所以y具有n个采样,1000个时间步长和5个热编码类(n_samples ,1000、5)。

Since one class is much more prevalent than the others (is actually the absence of signal), I am using loss='sparse_categorical_crossentropy', sample_weight_mode="temporal" and sample_weight to give a higher weight to time steps containing meaningful classes. 由于一个类别比其他类别更为普遍(实际上是没有信号),因此我使用loss ='sparse_categorical_crossentropy',sample_weight_mode =“ temporal”和sample_weight为包含有意义类别的时间步赋予更高的权重。

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 模型

When I try to fit the model I get this error message: 当我尝试拟合模型时,出现以下错误消息:

Error when checking target: expected time_distributed_1 to have shape (None, 998, 1) but got array with shape (100, 1000, 5). 检查目标时出错:预期time_distributed_1具有形状(无,998,1),但数组的形状为(100,1000,5)。

Is there a way to make such a neural network configuration work? 有没有办法使这种神经网络配置起作用?

Your convolution is cutting the tips of the sequence. 您的卷积削减了序列的技巧。 Use padding='same' in the convolutional layers. 在卷积层中使用padding='same'

The message, though, seems not to fit your model. 但是,该消息似乎不适合您的模型。 Your model clearly has 5 output features (because of Dense(5) ), but the massage says it expects 1. Maybe this is happening because of "sparse" crossentropy. 您的模型显然具有5个输出特征(由于Dense(5) ),但是按摩表示期望为1。也许这是由于“稀疏”交叉熵而发生的。 You should probably, by the format of your data, use a "categorical_crossentropy". 您可能应该根据数据的格式使用“ categorical_crossentropy”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM