tf.cast 或 tf.dtypes.cast
将输入的张量转换数据类型
输入:
(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], dtype=tf.float32)
c=tf.dtypes.cast(x, tf.int32)
x1 = tf.cast(x,tf.int64)
print(tf.keras.backend.eval(c))
print(tf.keras.backend.eval(x1))
# 打印结果
[1 2]
[1 2]