如何在Tensorflow中获得精确度和召回率而不是准确度

Jin*_*ang -1 classification machine-learning tensorflow rnn

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

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

该程序生成以下值.(损失,准确)

Veiw结果截图

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

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

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

Jin*_*ang 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"
    )
)
Run Code Online (Sandbox Code Playgroud)

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