nsorflow学习之tf.cast类型转换函数
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.
参数
- x:输入
- dtype:转换目标类型
- name:名称
Tensor
例子
- #将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]