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.
CodePudding user response:
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)
CodePudding user response:
Try using tf.ragged.stack
and merge_dims
without converting them to ragged tensors:
import tensorflow as tf
a2 = tf.random.normal((10, 943, 1))
b2 = tf.random.normal((10, 260, 1))
c = tf.ragged.stack([a2, b2], axis=0).merge_dims(0, 1)
print(c.shape)
# (20, None, 1)