Is it possible to interleave two Ragged Tensors in Tensorflow? Example: I have two RaggedTensors with the same "shape":
a = [[[10,10]],[[20,20],[21,21]]]
b = [[[30,30]],[[40,40],[41,41]]]
I would like to interleave them so the resulting tensor looks like this:
c = [[[10,10],[30,30]],[[20,20],[40,40],[21,21],[41,41]]]
Note that both tensors a
and b
always have the same "shape".
I have been trying to use the stack and the concat functions but both of them return non-desired shapes:
tf.stack([a,b],axis=-1)
c = [[[[10, 30], [10, 30]]], [[[20, 40], [20, 40]], [[21, 41], [21, 41]]]]
tf.concat([a,b],axis=-1)
c = [[[10, 10, 30, 30]], [[20, 20, 40, 40], [21, 21, 41, 41]]]
I have seen some other solutions for regular tensors that reshape the resulting tensor c after applying the stack/concat functions. E.g.,:
a = [[[10, 10]], [[20, 20]]]
b = [[[30, 30]], [[40, 40]]]
tf.reshape(
tf.concat([a[..., tf.newaxis], b[..., tf.newaxis]], axis=1),
[a.shape[0], -1, a.shape[-1]])
c = [[[10, 10],[30, 30]],[[20, 20],[40, 40]]]
However, as far as I know, since I am using Ragged Tensors the shape in some dimensions is None (I am using TF2.6).