Create a repeated sequence
A model has one fixed-size feature vector but a downstream sequence component expects timesteps.
Keras · Class
RepeatVector copies each input vector n times, turning a 2D batch of feature vectors into a 3D sequence tensor. The new middle dimension has length n.
RepeatVector accepts a batch of feature vectors and inserts a time-like axis containing n copies of each vector. Thus, a 2D input becomes a 3D output whose middle dimension is the configured repetition count.
keras.layers.RepeatVector(n, **kwargs)| Name | Type | Description |
|---|---|---|
n | integer | Integer repetition factor. |
A 3D tensor with shape (batch_size, n, features).
import keras
inputs = keras.Input(shape=(32,))
outputs = keras.layers.RepeatVector(3)(inputs)
model = keras.Model(inputs, outputs)
x = keras.ops.ones((4, 32))
y = model(x)
print(y.shape) # (4, 3, 32)This creates a model that converts each 32-feature input vector into a sequence of three identical 32-feature vectors.
A model has one fixed-size feature vector but a downstream sequence component expects timesteps.
A decoder needs the same context representation at every output timestep.
A vector must be expanded into a batch-first 3D tensor with a chosen sequence length.
RepeatVector repeats every input vector n times. For an input shaped (batch_size, features), the result is shaped (batch_size, n, features).
Follow-up: What input rank does the layer expect? It expects a 2D tensor containing a batch dimension and a features dimension.
The constructor takes n, an integer repetition factor. Calling RepeatVector(3) on inputs with 32 features produces outputs shaped (batch_size, 3, 32).
Follow-up: What changes if n is 5? The output shape becomes (batch_size, 5, features), while the batch and feature dimensions remain unchanged.
RepeatVector transforms a 2D tensor into a 3D tensor. Its input shape is (batch_size, features), and its output shape is (batch_size, n, features).
Follow-up: Why is this useful before a sequence layer? It supplies an explicit sequence-length axis by replicating a fixed feature representation across timesteps.
Reshaping layerskeras.ops.repeat