我在我的模型中使用自定義召回和精度指標。我知道他們將它們內置到 Keras 中,但我只關心其中一個類。
當我開始一個紀元時,我會列印出指標的值,但經過許多步驟后,一個指標回傳 NaN,幾百個紀元后,第二個自定義指標顯示 NaN。
召回指標寫在相同的
def precision(y_true, y_pred):
'''
Calculates precision metric over gun label
Precision = TP/(TP FP)
'''
#I only care about the last label
y_true = y_true[:,-1]
y_pred = y_pred[:,-1]
y_pred = tf.where(y_pred>.5, 1, 0)
y_pred = tf.cast(y_pred, tf.float32)
y_true = tf.cast(y_true, tf.float32)
true_positives = K.sum(y_true * y_pred)
false_positive = tf.math.reduce_sum(tf.where(tf.logical_and(tf.not_equal(y_true,y_pred), y_pred==1), 1, 0))
false_positive = tf.cast(false_positive, tf.float32)
precision = true_positives / (true_positives false_positive)
return precision
訓練一個多標簽,所以我的最后一個密集層是preds = Dense(num_classes, activation='sigmoid', name='Classifier')(x).
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy', precision, recall])
model.fit(train_ds, steps_per_epoch=10000, validation_data=valid_ds, validation_steps=1181, epochs=200)
18/10000 [............] - ETA: 6:43 - loss: 0.6919 - accuracy: 0.0046 - precision: 0.2597 - recall: 0.4691
315/10000 [...........] - ETA: 7:56 - loss: 0.4174 - accuracy: 0.1145 - precision: nan - recall: 0.6115
10000/10000 [=========>] - ETA: 0s - loss: 0.0797 - accuracy: 0.5432 - precision: nan - recall: nan
10000/10000 [=========>] - 576s 56ms/step - loss: 0.0797 - accuracy: 0.5432 - precision: nan - recall: nan - val_loss: 0.0557 - val_accuracy: 0.5807 - val_precision: 0.9698 - val_recall: 0.9529
在每個時期開始時,指標再次顯示數字,但經過許多步驟后,它們又回到 NaN。通過觀察,我可以確認它們不會在 NaN 之前變為 0 或 1。
uj5u.com熱心網友回復:
問題是除以零。我在解決問題的每個分母中添加了一個小值。如果網路在任何批次中都沒有正面預測,就會發生這種情況。這就是它間歇性發生的原因。
import tensorflow.keras.backend as K
precision = true_positives / (true_positives false_positive K.epsilon())
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/334346.html
下一篇:沒有這樣的檔案或目錄:'Tensorflow/workspace/annotations\\label_map.pbtxtonJupyter為什么我的代碼不起作用?
