- TensorFlow 2.0 Quick Start Guide
- Tony Holdroyd
- 65字
- 2021-06-24 16:02:04
Casting a tensor to another (tensor) datatype
TensorFlow variables of one type may be cast (coerced) to another type. More details may be found at https://www.tensorflow.org/api_docs/python/tf/cast.
Take the following example:
i = tf.cast(t1, dtype=tf.int32) # 42
i
The output will be as follows:
<tf.Tensor: id=116, shape=(), dtype=int32, numpy=42>
With truncation, it would be as follows:
j = tf.cast(tf.constant(4.9), dtype=tf.int32) # 4
j
The output will be as follows:
<tf.Tensor: id=119, shape=(), dtype=int32, numpy=4>