tensorflow中 tf.equal、tf.cast、tf.reduce_mean函数使用

一、tf.equal()

      使用方法:tf.equal(a,b)

      判断 a 和 b 对应位置的值是否相等,相等则返回 true , 不等则返回 false   (注意返回的是布尔值)

二、tf.cast()

      使用方法:tf.cast(a,dtype)         

      将输入的 a 的数据类型转成指定的数据类型(dtype)。

三、tf.reduce_mean()

       使用方法:tf.reduce_mean(a)   

       求a的平均值,返回是一个标量,也就是一个数字,不论a是多少维的

四、下面看一个小例子

 

import tensorflow as tf
a = [[1,2,2],[1,3,4]]
b = [[1,2,3],[3,5,6]]
c = tf.equal(a,b)
d = tf.cast(c,dtype=tf.float32)
e = tf.reduce_mean(d)
with tf.Session() as sess:
    c,d,e = sess.run([c,d,e])
    print(c)
    print(d)
    print(e)

 


输出结果:

 


[[ True  True False]
 [False False False]]
[[1. 1. 0.]
 [0. 0. 0.]]
0.33333334


注意:equal和cast都不会改变输入的维度,reduce_mean输出一个数值

 

你可能感兴趣的:(tensorflow)