Failed to convert object of type <class ‘tuple’> to Tensor. Contents: (None, -1, 128). Consider casting elements to a supported type.
tensorflow
中此類錯誤一般是 Tensor
計算中,使用 x.shape[i]
錯誤使用導致,正確的使用應該使用tf.shape(x)[i]
,即將x.shape
換成tf.shape(x)
就可以了,類型區別如下:
a.shape : TensorShape類型
tf.shape(a) : Tensor類型(符合計算類型)
a = tf.zeros(shape=(15,3,8))
tf.shape(a), a.shape, a.get_shape()
(<tf.Tensor: id=7, shape=(3,), dtype=int32, numpy=array([15, 3, 8], dtype=int32)>,
TensorShape([15, 3, 8]),
TensorShape([15, 3, 8]))
# 做切片後
tf.shape(a)[0], a.shape[0], a.get_shape()[0]
(<tf.Tensor: id=7, shape=(), dtype=int32, numpy=15>, 15, 15)
不滿足tensorflow計算tensor格式
補充:
獲得Python原生類型的維度信息:
x.shape.as_list() # [2,3]
x.shape.ndims # 2
獲得TensorFlow中Tensor類型的維度信息:
tf.shape(x)
tf.rank(x)