如何在tensorflow中播放第三维?

时间:2017-07-28 11:44:13

标签: tensorflow-gpu

我有一个索贝尔过滤器

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].

2 个答案:

答案 0 :(得分:1)

你不能广播的原因是第三个维度不存在,所以你实际上有一个等级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>

答案 1 :(得分: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)