tf.cast(x, dtype, name=None)
将x的数据格式转化成dtype数据类型.例如,原来x的数据格式是bool.
参数
x:输入
dtype:转换目标类型
name:名称
返回:Tensor
import tensorflow as tf
a = tf.Variable([1.0, 1.3, 2.1, 3.41, 4.51])
b = tf.cast(a, dtype=tf.int8)
c = tf.cast(a > 3, dtype=tf.bool)
d = tf.cast(a > 3, dtype=tf.int8)
e = tf.cast(a < 2, dtype=tf.float32)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
print(sess.run(b))
print(sess.run(c))
print(sess.run(d))
print(sess.run(e))
输出:
[1 1 2 3 4]
[False False False True True]
[0 0 0 1 1]
[1. 1. 0. 0. 0.]
Process finished with exit code 0