错误信息
在tensorflow堆叠张量的时候报错:InvalidArgumentError: cannot compute Pack as input #2(zero-based) was expected to be a float tensor but is a int32 tensor [Op:Pack] name: stack
import tensorflow as tf
x1 = tf.constant([137.97, 104.50, 100.00, 124.32, 79.20, 99.00, 124.00, 114.00, 106.69, 138.05, 53.75, 46.91, 68.00, 63.02, 81.26, 86.21])
x2 = tf.constant([3, 2, 2 ,3, 1, 2, 3, 2, 2, 3, 1, 1, 1, 1, 2, 2])
y = tf.constant([145.00, 110.00, 93.00, 116.00, 65.32, 104.00, 118.00, 91.00, 62.00, 133.00, 51.00, 45.00, 78.50, 69.65, 75.69, 95.30])
x0 = tf.ones(len(x1))
X = tf.stack([x0, x1, x2])
解决方法
原因很简单,就是堆叠的类型需要一致,因为没学过基础,直接上手的,所以都在报错中学习基础了。加上一句把x2转成float32类型就行了:
x2 = tf.cast(x2, dtype=tf.float32)