通过克隆一个张量来连接3d张量?

时间:2017-01-01 18:02:03

标签: python tensorflow

我有两个张量:

a = tf.placeholder(tf.float32, [None, 20, 100])
b = tf.placeholder(tf.float32, [None, 1, 100])

我想将b追加到a[i, 20, 100],以创建c,例如c的形状为[None, 20, 200]

这似乎相当简单,但我还没有想出如何使用tf.concat执行此操作:

tf.concat(0, [a, b]) -> Shapes (20, 100) and (1, 100) are not compatible
tf.concat(1, [a, b]) => shape=(?, 28, 100) which is not what I wanted
tf.concat(2, [a, b]) -> Shapes (?, 20) and (?, 1) are not compatible

我是否需要首先重塑ab然后重新连接?

1 个答案:

答案 0 :(得分:1)

可以使用tf.tile完成此操作。您需要克隆尺寸1,20次的张量,使其与a兼容。然后沿着维度2的简单连接将为您提供结果。

这是完整的代码,

import tensorflow as tf

a = tf.placeholder(tf.float32, [None, 20, 100])
b = tf.placeholder(tf.float32, [None, 1, 100])
c = tf.tile(b, [1,20,1])
print c.get_shape()
# Output - (?, 20, 100)
d = tf.concat(2, [a,c])
print d.get_shape()
# Output - (?, 20, 200)
相关问题