簡體   English   中英

遷移學習 model 和 Keras:使用准確性以外的其他指標

[英]Transfer Learning model with Keras: using other metrics than accuracy

我正在為瑞典樹葉數據中的樹葉進行二進制分類 model,我認為遷移學習可能很實用。 我找到了這個教程,但在編譯 function 時,我想使用與准確性不同的指標。 當我嘗試獲取 AUC 或 FP/FN/TP/TN 時,會引發 ValueError,聲稱 true y (None, 1) 的形狀與 y_pred (None, 2) 的形狀不兼容。

我不明白:

  1. 為什么 y_pred 會有這種形狀?
  2. 怎么能計算出准確率,卻不能計算出混淆矩陣的部分?!

也非常歡迎沒有合理解釋的解決方案:)

feature_extractor_model = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
pretrained_model_without_top_layer = hub.KerasLayer(
    feature_extractor_model, input_shape=(224, 224, 3), trainable=False)
classes_num = 2
model = tf.keras.Sequential([
  pretrained_model_without_top_layer,
  tf.keras.layers.Dense(classes_num)
])

model.compile(
  optimizer="adam",
  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
  metrics=[['acc'], [tf.keras.metrics.TruePositives(), tf.keras.metrics.FalsePositives(), tf.keras.metrics.TrueNegatives(), tf.keras.metrics.FalseNegatives()]])

model.fit(X_train_scaled, y_train, steps_per_epoch=9, epochs=5)

如果您有兩個類別(例如貓和狗),您可以將其稀疏編碼為零或一,或者將其編碼為 [0,1] 和 [1,0]。

你的訓練數據稀疏,所以你的損失是 SparseCCE。 指標只是功能上的損失,因此您使用的任何指標都需要接受稀疏。 在您的情況下,只需編寫一個接受稀疏 y_true 的“自定義”損失 function,對其進行一次性處理,並將其傳遞給召回/精度/等指標 function。

暫無
暫無

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

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