类型不匹配

ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32 :’ Tensor (”b : 。”, shape=(2 ,), dtype=float32) ’ 
import tensorflow as tf

a = tf.constant([1, 2], name="a")
b = tf.constant([2.0, 3.0], name="b")
result = a + b
sess = tf.Session()
print(sess.run(result))

如果将第一个加数指定成实数类型"a= tf.constant([l, 2], name="a” , dtype=tf.float32)", 那么两个加数的类型相同就不会报错了 。 如果不指定类型, TensorFlow 会给出默认的类型, 比如不带小数点的数会被默认为 int32,带小数点的会默认为 float32。 因为使用默认类型有 可能会导致潜在的类型不匹配问题,所以一般建议通过指定 dtype 来明确指出变量或者常 量的类型。 TensorFlow 支持 14 种不同的类型, 主要包括了实数(tf.丑oat32、 tf.float64)、整 数(tf.int8、 tf.intl6、 tf.int32、 tf.int64、 tf.uint8)、布尔型 Ctf.bool) 和复数(tf.complex64、 tf.complex128 )。