我怎样才能获得精确度和在Tensorflow中回忆而不是准确性

时间:2017-12-11 01:13:33

标签: machine-learning tensorflow classification rnn

我看到垃圾邮件预测将邮件分类为其他人制作的垃圾邮件和火腿。

[源代码] https://github.com/nfmcclure/tensorflow_cookbook/blob/master/09_Recurrent_Neural_Networks/02_Implementing_RNN_for_Spam_Prediction/02_implementing_rnn.py

程序产生以下值。 (损失,准确)

Veiw Result Screenshot

在此代码中,结果只是损失,准确性

我认为准确性毫无意义。 我需要精确度,召回值(F1测量值)

但是,由于我的代码分析工作不正常, 我知道Precision和Recall。 但我不知道如何在此代码中计算(代码嵌入)Precision和Recall。

1 个答案:

答案 0 :(得分:5)

我自己成功了,欢呼!!

这是代码:

actuals = tf.cast(y_output, tf.int64)
predictions = tf.argmax(logits_out, 1)

ones_like_actuals = tf.ones_like(actuals)
zeros_like_actuals = tf.zeros_like(actuals)
ones_like_predictions = tf.ones_like(predictions)
zeros_like_predictions = tf.zeros_like(predictions)

tp_op = tf.reduce_sum(
    tf.cast(
      tf.logical_and(
        tf.equal(actuals, ones_like_actuals), 
        tf.equal(predictions, ones_like_predictions)
      ), 
      "float"
    )
)

tn_op = tf.reduce_sum(
    tf.cast(
      tf.logical_and(
        tf.equal(actuals, zeros_like_actuals), 
        tf.equal(predictions, zeros_like_predictions)
      ), 
      "float"
    )
)

fp_op = tf.reduce_sum(
    tf.cast(
      tf.logical_and(
        tf.equal(actuals, zeros_like_actuals), 
        tf.equal(predictions, ones_like_predictions)
      ), 
      "float"
    )
)

fn_op = tf.reduce_sum(
    tf.cast(
      tf.logical_and(
        tf.equal(actuals, ones_like_actuals), 
        tf.equal(predictions, zeros_like_predictions)
      ), 
      "float"
    )
)

我在github看到混乱矩阵开源谢谢@Mistobaan !! https://gist.github.com/Mistobaan/337222ac3acbfc00bdac

相关问题