1D卷积神经网络输入形状的`ValueError`

我在生成1D卷积时遇到了数据输入形状的问题。我查看了几篇帖子,似乎错误在于数据必须是3D的,但我的数据已经是3D的了。

# shape# x_train shape: (1228, 1452, 20)# y_train shape: (1228, 1452, 8)# x_val shape: (223, 680, 20)# x_val shape: (223, 680, 8)###n_outputs = 8n_timesteps = 1452n_features = 20model = Sequential()model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(x_train.shape[1:]))) # ie 1452, 20model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))model.add(Dropout(0.5))model.add(MaxPooling1D(pool_size=2))model.add(Flatten())model.add(Dense(100, activation='relu'))model.add(Dense(n_outputs, activation='softmax'))model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])model.fit(x_train, y_train, epochs=9,                 batch_size=64,                 shuffle=True)

但我一直收到这个错误消息:

ValueError: A target array with shape (1228, 1452, 8) was passed for an output of shape (None, 8) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.

从中我了解到,目标形状是3维的,而输出是2维的,因此无法计算损失,但我只需要找到一种方法来重新调整形状,使它们相等。

编辑model.summary()如下所示

_________________________________________________________________Layer (type)                 Output Shape              Param #   =================================================================conv1d (Conv1D)              (None, 1450, 64)          3904      _________________________________________________________________conv1d_1 (Conv1D)            (None, 1448, 64)          12352     _________________________________________________________________dropout (Dropout)            (None, 1448, 64)          0         _________________________________________________________________max_pooling1d (MaxPooling1D) (None, 724, 64)           0         _________________________________________________________________flatten (Flatten)            (None, 46336)             0         _________________________________________________________________dense (Dense)                (None, 100)               4633700   _________________________________________________________________dense_1 (Dense)              (None, 8)                 808       =================================================================Total params: 4,650,764Traceback (most recent call last):Trainable params: 4,650,764Non-trainable params: 0

回答:

在我的案例中,问题在于目标向量是3D的,而输出向量是2D的,因此存在明显的不匹配。要解决这个问题,可以将y_train的形状更改为(batch, 8),或者使用return_sequences=True从前一层LSTM层返回相同形状的数据。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注