TensorFlow tf.equal()、tf.cast、tf.reduce_mean函数组合求预测的正确率

tf.equal(x, y) 

判断x, y是否相等,相等返回true,不相等false

假设x为预测的结果,y为正确的标签值。

import tensorflow as tf
x = [1, 3, 0, 2]
y = [1, 4, 2, 2]
equal = tf.equal(x, y)
with tf.Session() as sess:
    print(sess.run(equal))

输出:

[ True False False  True]

tf.cast(x, dtype, name=None) 

数据类型转换

  • x:待转换的数据
  • dtype:目标数据类型
  • name=None:操作的名称
import tensorflow as tf
x = [1, 3, 0, 2]
y = [1, 4, 2, 2]
equal = tf.equal(x, y)
type = tf.cast(equal, tf.float32)
with tf.Session() as sess:
    print(sess.run(type))

输出:

[1. 0. 0. 1.]

tf.reduce_mean()

求均值,上面的代码最后一行改为:

print(sess.run(tf.reduce_mean(type)))

将会输出0.5

参考:https://blog.csdn.net/lrr_csdn/article/details/84645018

你可能感兴趣的:(TensorFlow)