2017-07-28 34 views
0

我有一個索貝爾濾波器如何在tensorflow中播放第三維?

sobel_x = tf.constant([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], tf.float32) 

我想要得到的64中深度的形狀是暫時[3,3,1],但應引起[3,3,64]。

這怎麼辦?隨着下面的行,我得到形狀錯誤。

tf.tile(sobel_x, [1, 1, 64]) 



ValueError: Shape must be rank 2 but is rank 3 for 'Tile' (op: 'Tile') with input shapes: [3,3], [3]. 

回答

0

你不能廣播的原因是第三維不存在,所以你實際上有一個秩2張量。

>>> sess.run(tf.shape(sobel_x)) 
array([3, 3], dtype=int32) 

我們可以通過先改變張量來解決這個問題。

>>> sobel_x = tf.reshape(sobel_x, [3, 3, 1]) 
>>> tf.tile(sobel_x, [1, 1, 64]) 
<tf.Tensor 'Tile_6:0' shape=(3, 3, 64) dtype=float32> 
0

我認爲你的問題是與sobel_x。

sobel_x.get_shape(): TensorShape([Dimension(3), Dimension(3)]) sobel_x: <tf.tensor 'Const:0' shape=(3, 3) dtype=float32

所以sobel_x是一個二維矩陣,你傳遞一個等級3輸入平鋪因此錯誤。

修正:使sobel_x 3維,使得形狀是shape=(3, 3, 1) 然後tf.tile(sobel_x, [1, 1, 64]將輸出shape=(1, 1, 64)

相關問題