Given a tensor t=[[1,2], [3,4]]
, I need to produce ts=[[1,2,1,2], [1,2,3,4], [3,4,1,2], [3,4,3,4]]
. That is, I need to stack together all row pairs.
Important: the tensor has dimension [None, 2], ie. the first dimension is variable.
I have tried:
- Using a
tf.while_loop
to generate a list of indicesidx=[[0, 0], [0, 1], [1, 0], [1, 1]]
, thentf.gather(ts, idx)
. This works but is messy and I don't know what to do about gradients. - 2 for loops iterating over
tf.unstack(t)
, adding stacked rows to a buffer, thentf.stack(buffer)
. This does not work if the first dimension is variable. - To look for inspiration in broadcasting. For instance, given
x=t.expand_dims(t, 0), y=t.expand_dims(t, 1), s=tf.reshape(tf.add(x, y), [-1, 2])
s
will be [[2, 4], [4, 6], [4, 6], [6, 8]], ie. the sum of every row combination. But how can I do stacking instead of sum? I've been failing for 2 days :)