首页 > 解决方案 > 召回率、准确率、假阳性率和真阳性率都在不断地给出零值

问题描述

我正在使用贝叶斯神经网络。我以前在 f1 分数和其他指标(FPR、TPR、Precision 和 Recall)方面遇到过 nan,而我正在使用的其他指标在运行模拟时都会给出随机值。现在我应用了这个 f1 没有给出 nan 但其他指标不断给出零输出

I previously tried to compute the values of the performance metrics I am using in the models as follows: #TP, TN, FP, FN
    TP = tf.count_nonzero(predictions * labels_final)
    TN = tf.count_nonzero((predictions-1)*(labels_final-1))
    FP = tf.count_nonzero(predictions*(labels_final-1))
    FN = tf.count_nonzero((predictions-1)* labels_final)
    #precision, recall, f1
    precision = TP / (TP + FP)
    recall = TP / (TP + FN)
    f1 = 2 * precision * precision * recall / (precision + recall)
    tpr = TP/(TP+FN)
    fpr = FP/(TP+FN)
The above method has been giving me "nan" for f1 score with values of accuracy, fpr, tpr, precision and recall fluctating. I tried the formula as reflects on the show some code.


predictions = tf.argmax(input = logits, axis=1)

  predictions = tf.cast(predictions, tf.float32)

  epsilon = tf.constant(value=0.0000001)

  tp = tf.reduce_sum(tf.cast(labels_final * predictions, 'float'), axis = 0)
  tn = tf.reduce_sum(tf.cast((1-labels_final)*(1-predictions), 'float'), axis=0)
  fp = tf.reduce_sum(tf.cast((1- labels_final) * predictions,'float'), axis =0)
  fn = tf.reduce_sum(tf.cast (labels_final * (1 - predictions), 'float'), axis =0)

  precision = tp / (tp + fp + epsilon)
  recall = tp / (tp + fn + epsilon)
  tpr = tp/(tp + fn + epsilon)
  fpr = fp/(tp + fn + epsilon)

  def f1_score(labels_finals,predictions): # f1_score 

    f1_1 = 2*precision*recall/(precision + recall+ epsilon)

    f1_1 = tf.where(tf.is_nan(f1_1), tf.zeros_like(f1_1), 1) 
    return tf.math.reduce_mean(f1_1)

  f1 = f1_score(labels_final, predictions) # f1 function calling 

Actually, I am expecting some values other than zero in all the iterations in the rest of the performance metrics I am using other than 0.0. F1 score has been constantly 1 in all the iterations.

标签: tensorflow

解决方案


推荐阅读