tensorflow2.0深度学习 keras AUC性能评估
tensorflow2.0深度学习 keras AUC性能评估借鉴:keras中自定义验证集的性能评估(ROC,AUC)1. 步骤1keras 中编写自定义RocAucMetricCallback 类:import tensorflow as tffrom sklearn.metrics import roc_auc_scoreclass RocAucMetricCallback(t...
·
tensorflow2.0深度学习 keras AUC性能评估
1. AUC 计算公式
from tensorflow.keras import backend as K
# AUC for a binary classifier
def auc(y_true, y_pred):
ptas = tf.stack([binary_PTA(y_true,y_pred,k) for k in np.linspace(0, 1, 1000)],axis=0)
pfas = tf.stack([binary_PFA(y_true,y_pred,k) for k in np.linspace(0, 1, 1000)],axis=0)
pfas = tf.concat([tf.ones((1,)) ,pfas],axis=0)
binSizes = -(pfas[1:]-pfas[:-1])
s = ptas*binSizes
return K.sum(s, axis=0)
#-----------------------------------------------------------------------------------------------------------------------------------------------------
# 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
2. 运行实例
# 构建
input1 = tf.keras.Input(shape=[x_train.shape[0],],dtype=float32)
X1 = tf.keras.layers.Flatten()(input1)
X1 = tf.keras.layers.BatchNormalization()(X1)
X1 = tf.keras.layers.Dense(16, activation=tf.keras.layers.LeakyReLU(alpha=0.3))(X1)
X1 = tf.keras.layers.Dropout(0.5)(X1)
output = tf.keras.layers.Dense(1, activation='sigmoid')(X1)
model = tf.keras.Model(inputs=input1,outputs=output)
model.sammary()
# 编译
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=[auc]
)
更多推荐
所有评论(0)