代码示例如下:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
a = tf.constant(6)
b = tf.constant([1,2,3])
a1 = tf.to_float(a,name='ToFloat')
b1 = tf.cast(b,dtype = tf.float32)
with tf.Session() as session:
print(session.run(a1))
print(session.run(b1))