TensorFlow中张量转置操作tf.cast/tf.dtypes.cast用法详解

一、环境

TensorFlow API r1.12

CUDA 9.2 V9.2.148

Python 3.6.3

二、官方说明

tf.cast 或 tf.dtypes.cast

将输入张量转换数据类型

tf.dtypes.cast(
    x,
    dtype,
    name=None
)

输入:

(1)x:可以是张量(Tensor)、稀疏张量(SparseTensor)或数值类型的索引切片(IndexedSlices),如:uint8, uint16, uint32, uint64, int8, int16, int32, int64, float16, float32, float64, complex64, complex128, bfloat16.

(2)dtype:要转换的目标数据类型,支持的数据类型种类同上面的x

(3)name:可选参数,操作的名称

三、示例

>>> x = tf.constant([1.8,2.2])
>>> x1 = tf.cast(x,tf.int32)
>>> with tf.Session() as sess:
...     print(sess.run(x1))
...
[1 2]

猜你喜欢

转载自blog.csdn.net/sdnuwjw/article/details/84985470