いくつかのモデル評価の実装コード

 

TFとしてインポートtensorflow

#正解率の評価
デフmetric_precision(y_true、y_pred):    
    TP = tf.reduce_sum(y_true * tf.round(y_pred))
    TN = tf.reduce_sum((1-y_true)*(1-tf.round(y_pred)))
    FP = tf.reduce_sum((1-y_true)* tf.round(y_pred))
    FN = tf.reduce_sum(y_true *(1-tf.round(y_pred)))
    精度= TP /(TP + FP)
    リターン精度

#リコール評価
デフmetric_recall(y_true、y_pred):  
    TP = tf.reduce_sum(y_true * tf.round(y_pred))
    TN = tf.reduce_sum((1-y_true)*(1-tf.round(y_pred)))
    FP = tf.reduce_sum((1-y_true)* tf.round(y_pred))
    FN = tf.reduce_sum(y_true *(1-tf.round(y_pred)))
    リコール= TP /(TP + FN)
    リターンリコール

#F1-スコア評価
デフmetric_F1score(y_true、y_pred):    
    TP = tf.reduce_sum(y_true * tf.round(y_pred))
    TN = tf.reduce_sum((1-y_true)*(1-tf.round(y_pred)))
    FP = tf.reduce_sum((1-y_true)* tf.round(y_pred))
    FN = tf.reduce_sum(y_true *(1-tf.round(y_pred)))
    精度= TP /(TP + FP)
    リコール= TP /(TP + FN)
    F1score = 2 *精度*リコール/(精度+リコール)
    リターンF1score

#コンパイルフェーズ基準サンプルカスタム評価 model.compile(オプティマイザ= 'アダム、 損失=「binary_crossentropy」 メトリック= [ '精度'、 metric_precision、 metric_recall、 metric_F1score])

  

#バイナリ分類器のためのAUC
(y_true、y_pred)デフAUC:
    PTAS = tf.stack([binary_PTA np.linspaceにおけるkに対する(y_true、y_pred、K)(0、1、1000)]、軸= 0)
    PFAS = tf.stack([binary_PFA np.linspaceにおけるkに対する(y_true、y_pred、K)(0、1、1000)]、軸= 0)
    PFAS = tf.concat([tf.ones((1))、PFAS]、軸= 0)
    binSizes =  - (PFAS [1:]  -  PFAS [ -  1])
    S = PTAS * binSizes
    リターンK.sum(S、軸= 0)
#------------------------------------------------- -------------------------------------------------- --------------------------------------------------
#PFAは、バイナリ分類器のための偽の警告をPROB
DEF binary_PFA(y_true、y_pred、閾値= K.variable(値= 0.5))。
    y_pred = K.cast(y_pred> =閾値、 'のfloat32')
    #Nは、負のラベルの総数=
    N = K.sum(1  -  y_true)
    誤ったアラートの#FP =合計数、負のクラスラベルからのアラート
    FP = K.sum(y_pred  -  * y_true y_pred)
    リターンFP / N
#------------------------------------------------- -------------------------------------------------- --------------------------------------------------
バイナリ分類器のための#P_TA PROB真アラート
DEF binary_PTA(y_true、y_pred、閾値= K.variable(値= 0.5))。
    y_pred = K.cast(y_pred> =閾値、 'のfloat32')
    #Pは正のラベルの総数=
    P = K.sum(y_true)
    正しいアラートの#TP =総数、陽性クラスラベルからのアラート
    TP = K.sum(y_pred * y_true)
    リターンTP / P
 
その後で提供#コンパイルモデルの指標で

  

おすすめ

転載: www.cnblogs.com/ylHe/p/12149873.html