使用Tensorflow构建多类分类时的目标形状错误

我正在尝试使用Tensorflow和Keras的多类分类的人工神经网络。我正在构建一个具有以下形状的模型:

print(X_train.shape, X_test.shape, y_train.shape, y_test.shape`
(2000, 5, 5) (800, 5, 5) (2000, 4) (800, 4)

标签是独热编码的。
这是我的模型:

from tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Densefrom tensorflow.keras.layers import Dropoutmodel = Sequential()model.add(Dense(64, input_shape=(X_train.shape[1], X_train.shape[2],), activation='relu'))model.add(Dense(32, activation='relu'))model.add(Dense(y_train.shape[1], activation='softmax'))model.compile(optimizer = 'adam', loss='categorical_crossentropy', metrics=['accuracy'])# model.summary()model.fit(X_train, y_train, epochs=5, batch_size=32, verbose=1, validation_data=(X_test, y_test)`

我得到了以下错误:

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

问题出在哪里?


回答:

你可能需要在网络中降低维度。你需要从3D转换到2D以匹配你的目标。你可以通过使用全局合并或平滑层来实现这一点。尝试在输出层之前使用Flatten (),或者使用GlobalAveragePooling1D()GlobalMaxPooling1D()

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中创建了一个多类分类项目。该项目可以对…

发表回复

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