Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
print(res.shape)

(60000, None, 1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading