6
我試圖實現keras的AUC度量,以便在model.fit()運行期間運行驗證集後運行AUC度量。定義keras的AUC度量標準以支持驗證數據集的評估
我定義的指標,因爲這:
def auc(y_true, y_pred):
keras.backend.get_session().run(tf.global_variables_initializer())
keras.backend.get_session().run(tf.initialize_all_variables())
keras.backend.get_session().run(tf.initialize_local_variables())
#return K.variable(value=tf.contrib.metrics.streaming_auc(
# y_pred, y_true)[0], dtype='float32')
return tf.contrib.metrics.streaming_auc(y_pred, y_true)[0]
這將導致以下錯誤,我不知道理解。
tensorflow.python.framework.errors_impl.FailedPreconditionError:
Attempting to use uninitialized value auc/true_positives...
從網上閱讀,似乎問題是2倍,在tensorflow/keras中的錯誤和部分和問題與tensorflow暫時無法從推理初始化局部變量。鑑於這兩個問題,我不明白爲什麼我會得到這個錯誤或如何克服它。有什麼建議麼?
爲了證明我不是懶惰的,我寫了2個其他指標可以正常工作。
# PFA, prob false alert for binary classifier
def binary_PFA(y_true, y_pred, threshold=K.variable(value=0.5)):
y_pred = K.cast(y_pred >= threshold, 'float32')
# N = total number of negative labels
N = K.sum(1 - y_true)
# FP = total number of false alerts, alerts from the negative class labels
FP = K.sum(y_pred - y_pred * y_true)
return FP/N
# P_TA prob true alerts for binary classifier
def binary_PTA(y_true, y_pred, threshold=K.variable(value=0.5)):
y_pred = K.cast(y_pred >= threshold, 'float32')
# P = total number of positive labels
P = K.sum(y_true)
# TP = total number of correct alerts, alerts from the positive class labels
TP = K.sum(y_pred * y_true)
return TP/P