RepeatVector or expand_dims

Hey there,

I just want to know, if I want to expand only one dimension in the middle for a tensor like [3, 5], both of those statements give me [3, 1, 5], which one is better?

y1 = np.random.randn(3, 5)

y1_exp = tf.expand_dims(y1, axis=1)

y2_exp = RepeatVector(1)(y1)


1 Like


Yes both will give you the same output.

tf.expand_dims is a tensorflow built-in function that allows you to insert a new axis into a tensor at a specified position. It is the simplest and most straightforward method to achieve a desired result.

Whereas tf.keras.layers.RepeatVector is a keras layer that repeats the input n times.It is a bit more complex and may not be as intuitive as using tf.expand_dims .

Thank you!