Tensorflow API - tf.cast
help( tf.cast ) 查看帮助信息
定义 - [tensorflow/python/ops/math_ops.py](https://www.github.com/tensorflow/tensorflow/blob/r1.8/tensorflow/python/ops/math_ops.py):
tf.cast( x, dtype, name=None )
将 tensor x 的数据格式转化为新的 dtype.
The operation castsx(in case ofTensor) orx.values(in case ofSparseTensor) todtype.
参数:
- x - Tensor 或 SparseTensor
- dtype - 目标数据格式
- name - 操作的名字(可选.)
返回值:
- 与 x shape 一致的 Tensor 或 SparseTensor.
异常:
- TypeError: If x cannot be cast to the dtype.
例如:
x = tf.constant([1.8, 2.2], dtype=tf.float32)
tf.cast(x, tf.int32) # [1, 2], dtype=tf.int32
将 tensor 的数据格式由 tf.float32 转换成 tf.int32.