为什么我的准确率和损失在Keras中分别为0.000和NaN?

我的理解是“sparse_categorical_crossentropy”适合我的多分类问题,且不需要独热编码。我还降低了Adam优化器的学习率,以防预测结果过度跳跃。

我不确定自己在哪里理解错误或操作不当。

我的输入数据看起来类似于这样:enter image description here

我的输出预测结果是标签:[1 2 3 4 5 6 7 8 9 10](未进行独热编码)。每个数字代表我希望网络最终选择的类别。

print(x_train.shape)print(x_test.shape)x_train = x_train.reshape(x_train.shape[0], round(x_train.shape[1]/5), 5)x_test = x_test.reshape(x_test.shape[0], round(x_test.shape[1]/5), 5)print(x_train.shape)print(np.unique(y_train))print(len(np.unique(y_train)))input_shape = (x_train.shape[1], 5)adam = keras.optimizers.Adam(learning_rate=0.0001)model = Sequential()model.add(Conv1D(512, 5, activation='relu', input_shape=input_shape))model.add(Conv1D(512, 5, activation='relu'))model.add(MaxPooling1D(3))model.add(Conv1D(512, 5, activation='relu'))model.add(Conv1D(512, 5, activation='relu'))model.add(GlobalAveragePooling1D())model.add(Dropout(0.5))model.add(Dense(1, activation='sigmoid'))model.compile(loss='sparse_categorical_crossentropy', optimizer=adam, metrics=['accuracy'])    model.fit(x_train, y_train, batch_size=32, epochs=25, validation_data=(x_test, y_test))print(model.summary())

这是错误结果:enter image description here

模型层(如果有帮助):

enter image description here


回答:

我认为你的方法中有两个主要问题

你的标签是从1到10…它们必须从0开始,这样才能在0到9的范围内。这可以通过简单地执行y_train-1y_test-1来实现(如果y_test和y_train是numpy数组的话)。

你的网络的最后一层必须是Dense(10, activation='softmax'),其中10是需要预测的类别数量,softmax用于生成多类问题的概率。

使用sparse_categorical_crossentropy是可以的,因为你的目标是整数编码的。

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

发表回复

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