【ERROR】InvalidArgumentError: cannot compute Pack as input #2(zero-based)......

錯誤信息

在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)

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章