介绍
数据类型转换
官网:
https://www.tensorflow.org/api_docs/python/tf/cast
常用别名
- tf.cast
tensorflow数据类型
详见:
https://www.malaoshi.top/show_1EF4vd8AxEVK.html
语法
tf.cast(
x,
dtype,
name=None
)
参数
- x:tensor或numpy类型
- dtype:要转换的数据类型。 数据类型详见
返回
- Tensor
例子:
x = tf.constant([1.8, 2.2], dtype=tf.float32)
# 转换类型
tf.dtypes.cast(x, tf.int32)
执行结果:
<tf.Tensor: id=532, shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>
另一种写法
下面写法也可以转换,效果与上面相同
tf.dtypes.cast(x, "int32")
执行结果:
<tf.Tensor: id=532, shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>