tf.equal()、tf.cast()、tf.reduce_mean()三个函数的使用

1 语法

tf.equal(x, y)  判断x, y是否相等,相等返回true,不相等false

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

你可能感兴趣的:(tensorflow入门)