簡體   English   中英

為什么 keras model 如果與 one-hot 標簽和 categorical_crossentropy 和 softmax output 一起使用,則將所有預測為 1

[英]Why does keras model predicts all as ones if used with one-hot labels and categorical_crossentropy amnd softmax output

我有一個簡單的 tf.keras model:

inputs = keras.Input(shape=(9824,))
dense = layers.Dense(512, activation=keras.activations.relu, kernel_initializer=init)
x = dense(inputs)
x = layers.Dense(512, activation=keras.activations.relu)(x)
outputs = layers.Dense(3, activation=keras.activations.softmax)(x)
model = keras.Model(inputs=inputs, outputs=outputs)

當我用稀疏的分類交叉熵和實際標簽編譯它時,它按預期工作。 但是,當我嘗試對標簽進行一次熱編碼(使用tf.keras.utils.to_categorical )並使用 categorical_crossentropy (因此我可以在訓練期間使用召回率和精度作為指標)時,model 將所有內容預測為:

>>>print(predictions)
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]
 ...
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]

如果我理解正確,output 層中的 softmax 激活應該會導致 output 在范圍內(0,1)並且總和為 1。那么, ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 怎么可能是所有預測? 我一直在尋找答案幾個小時,但無濟於事。

編輯

這是一個簡約的例子

我忘了說我用的是scikeras package。 根據文檔中的示例,我假設 model 是隱式編譯的。 這是分類器構造函數:

clf = KerasClassifier(
    model=keras_model_target,
    loss=SparseCategoricalCrossentropy(),
    name="model_target",
    optimizer=Adam(),
    init=GlorotUniform(),
    metrics=[SparseCategoricalAccuracy()],
    epochs=5,
    batch_size=128
)

我適合 model

result = clf.fit(x_train, y_train)

並預測:

predictions = clf.predict(x)

這是 SciKeras 中的一個錯誤,已在 v0.3.1 版本中修復。 更新到最新版本應該可以解決問題。

至於錯誤本身,這是由於我們如何索引 numpy arrays,請參閱此差異了解詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM