我希望下面的代碼產生相應的張量>>> A = tf.range(3)>>> B = tf.tile(tf.expand_dims(A), [4,1])>>> print(tf.Session().run(B))[[0,1,2], [0,1,2], [0,1,2], [0,1,2]]但是,這會導致ValueError. 重現問題的簡單方法如下>>> import tensorflow as tf>>> x = tf.constant([0,1,2])>>> y = tf.expand_dims(x)ValueError: Tried to convert 'dim' to a tensor and failed. Error: None values not supported.使用expand_dims和避免此錯誤的正確方法是什么?
1 回答

動漫人物
TA貢獻1815條經驗 獲得超10個贊
看起來您需要為 的axis
參數指定一個值expand_dims
。默認值None
似乎會導致您遇到的錯誤。這有點奇怪,因為默認參數通常會導致某種合理的默認行為......也許這是一個錯誤。
您的代碼應該與y = tf.expand_dims(x, axis=0)
. 這將導致[1, 3]
您的示例中的形狀為,允許您之后平鋪。另一種選擇是y = x[tf.newaxis, :]
它還添加了一個軸。
添加回答
舉報
0/150
提交
取消