【问题标题】:Why is my accuracy and loss, 0.000 and nan, in keras?为什么我的准确性和损失,0.000 和 nan,在 keras 中?
【发布时间】:2020-06-30 20:12:50
【问题描述】:

我的理解是,“sparse_categorical_crossentropy”适合我的多分类,没有单热编码的情况。我还放慢了亚当学习率,以防它超出预测。

我不确定自己做错了什么。

我的输入数据与此类似:

我的输出预测结果是标签:[1 2 3 4 5 6 7 8 9 10](不是 one-hot-encoded)。每个数字代表我希望网络最终选择。

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())

以下是错误结果:

模型层(如果有帮助的话):

【问题讨论】:

  • 所以看来你有 10 个类要预测,有 1 个密集输出和 sigmoid ?
  • 你能展示y_train的样本吗?除了单类输出,不清楚输入是否真的稀疏

标签: python tensorflow machine-learning keras conv-neural-network


【解决方案1】:

我发现您的方法存在两个主要问题

您的标签是从 1 到 10...它们必须从 0 开始才能使它们在 0-9 范围内。这可以通过 y_train-1y_test-1 来实现(如果 y_test 和 y_train 是 numpy 数组)

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

使用sparse_categorical_crossentropy 没问题,因为你有整数编码的目标

【讨论】:

    猜你喜欢
    • 2021-09-04
    • 2019-02-18
    • 1970-01-01
    • 2020-08-25
    • 2019-09-06
    • 2019-03-25
    • 2017-07-16
    • 2016-09-29
    • 2019-08-21
    相关资源
    最近更新 更多