召回率,精度,假阳性率和真阳性率始终为零

时间:2019-07-05 20:54:12

标签: tensorflow

我正在使用贝叶斯神经网络。我以前在f1得分和其他指标(FPR,TPR,Precision和Recall)上遇到过难点,每次运行模拟时我都会给出随机值。现在我应用了      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.

0 个答案:

没有答案