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

2024-09-29 21:24:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在keras中使用Conv1D和双向LSTM(非常类似于this question)进行信号处理,但是对每个时间步进行多类分类。在

问题是,尽管Conv1D和LSTM使用的形状有些相似:

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

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

更具体地说,我的训练集X有n个样本,有1000个时间步和一个通道(n_samples,1000,1),我使用LabelEncoder和onehotcoder,因此y有n个样本,1000个时间步和5个1个热编码类(n_samples,1000,5)。在

由于一个类比其他类更普遍(实际上是没有信号),所以我使用loss='sparse_category_crossentry'、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

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

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

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


Tags: sampleaddmodelbatch时间length形状sparse
1条回答
网友
1楼 · 发布于 2024-09-29 21:24:56

你的卷积切断了序列的尖端。在卷积层中使用padding='same'。在

不过,这个信息似乎并不适合你的模式。您的模型显然有5个输出特性(因为Dense(5)),但是消息显示它期望1个。也许这是因为“稀疏”的交叉熵。根据数据的格式,您可能应该使用“分类熵”。在

相关问题 更多 >

    热门问题