Home > Mobile >  How to stack tensors without using tf.stack?
How to stack tensors without using tf.stack?

Time:02-28

Is there any way to merge Tensors in Tensorflow? For example: I have 128 Tensor shape all are (40, 10), Now, I want merge them to shape(128, 40, 10). I can't use *tf.stack([Tensor1, Tensor2, Tensor3, ...])* directly.

So, Is there any function that can help achieve this?

CodePudding user response:

Use tf.expand_dims with tf.concat:

import tensorflow as tf

x1 = tf.expand_dims(tf.random.normal((40, 10)), axis=0)
x2 = tf.expand_dims(tf.random.normal((40, 10)), axis=0)
x3 = tf.expand_dims(tf.random.normal((40, 10)), axis=0)
x4 = tf.expand_dims(tf.random.normal((40, 10)), axis=0)

x = tf.concat([x1, x2, x3, x4], axis=0)
print(x.shape)
# (4, 40, 10)

CodePudding user response:

I saw there is a correct answers but me also provide another way by it layer(s) that I use for my working cases:

X = tf.keras.layers.Concatenate(axis=1)([group_1_ShoryuKen_Left, group_1_ShoryuKen_Right])

Working with multiple sequences

  • Related