tf.cast(x, dtype, name=None)
此函数是类型转换函数,将目标x转为dtype类型。
Args:
x: A Tensor or SparseTensor.
dtype: The destination type.
name: A name for the operation (optional).
Returns:
A Tensor or SparseTensor with same shape as x.
Raises:
TypeError: If x cannot be cast to the dtype.
For example:
#将0/1转换成bool类型
import tensorflow as tf
a = tf.Variable([1,0,0,1,1])
b = tf.cast(a,dtype=tf.bool)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
print(sess.run(b))
#[ True False False True True]