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进行基本操作的方法,包括如何判断两个张量是否相等及数据类型转换,并通过具体代码示例展示了tf.equal()和tf.cast()函数的用法,最后讲解了如何利用tf.reduce_mean()计算均值。
1万+

被折叠的 条评论
为什么被折叠?



