Concatenate tensors with different shapes in tensorflow


I am new to tensorflow and I’m trying to concatenate 2 tensors with different shapes.
The tensors have shape:

>>> a
# <tf.Tensor: id=38, shape=(30000, 943, 1), dtype=float64

>>> b
<tf.Tensor: id=2, shape=(30000, 260, 1), dtype=float64

Is it possible to concatenate them on axis=0 to obtain a tensor with shape (60000, ?, 1)?
I tried to convert them to ragged tensors before concatenating:

a2 = tf.ragged.constant(a)
b2 = tf.ragged.constant(b)

c = tf.concat([a2, b2], axis=0)

but it did not work.


You can convert the tensor to RaggedTensor then use your own code (tf.concat).

a = tf.random.uniform((30000, 943, 1), maxval=4, dtype=tf.int32)
b = tf.random.uniform((30000, 260, 1), maxval=4, dtype=tf.int32)

rag_a = tf.RaggedTensor.from_tensor(a)
rag_b = tf.RaggedTensor.from_tensor(b)

res = tf.concat([rag_a, rag_b], axis=0)

(60000, None, 1)

Answered By – I'mahdi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published