TensorFlow
seq2seq
multidimensional regression
machine learning
neural networks

Tensorflow seq2seq multidimensional regression

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

A seq2seq model is not limited to language tasks. It can also predict sequences of continuous vectors, which makes it a valid approach for multidimensional regression over time. The main adjustment is that the decoder output is not a probability distribution over tokens. It is a vector of real-valued regression targets at each step.

What Multidimensional Seq2Seq Regression Means

In ordinary regression, the model predicts one continuous value. In multidimensional sequence regression, the output at each time step contains several continuous values.

For example, an input sequence of sensor readings might have shape (batch, input_steps, input_features), while the target sequence might have shape (batch, output_steps, target_dims).

A seq2seq model is useful when:

  • input and output are both sequences
  • the output can have a different length from the input
  • each output step contains multiple numeric targets

A Simple Encoder-Decoder In Keras

Here is a compact TensorFlow example that maps one input sequence to a shorter output sequence of 2-dimensional regression targets.

python
1import numpy as np
2import tensorflow as tf
3from tensorflow.keras import layers
4
5input_steps = 12
6input_features = 4
7output_steps = 6
8target_dims = 2
9latent_dim = 32
10
11encoder_inputs = layers.Input(shape=(input_steps, input_features))
12encoder_output, state_h, state_c = layers.LSTM(latent_dim, return_state=True)(encoder_inputs)
13encoder_states = [state_h, state_c]
14
15decoder_inputs = layers.Input(shape=(output_steps, target_dims))
16decoder_lstm = layers.LSTM(latent_dim, return_sequences=True, return_state=True)
17decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
18decoder_outputs = layers.Dense(target_dims)(decoder_outputs)
19
20model = tf.keras.Model([encoder_inputs, decoder_inputs], decoder_outputs)
21model.compile(optimizer="adam", loss="mse")
22
23x_train = np.random.randn(64, input_steps, input_features).astype("float32")
24y_train = np.random.randn(64, output_steps, target_dims).astype("float32")
25
26teacher_forcing_inputs = np.zeros_like(y_train)
27teacher_forcing_inputs[:, 1:, :] = y_train[:, :-1, :]
28
29model.fit([x_train, teacher_forcing_inputs], y_train, epochs=2, batch_size=8)

The important line is layers.Dense(target_dims)(decoder_outputs). That final dense layer turns each decoder time step into a continuous vector of length target_dims.

Why Teacher Forcing Appears In The Example

During training, the decoder usually needs a previous output step as input. A common strategy is teacher forcing, where the true previous target is fed into the decoder instead of the model's own prediction.

That is what this line prepares:

python
teacher_forcing_inputs[:, 1:, :] = y_train[:, :-1, :]

The decoder input sequence is shifted by one step. The first decoder input is zero here, but in a real application you might use a learned start vector or domain-specific initial state.

Inference Is Different From Training

At inference time, you usually do not have the true future targets. The decoder must feed its own previous prediction back into the next step.

For a production seq2seq regressor, you often build separate inference models:

  • one encoder model that returns the final states
  • one decoder model that consumes the last prediction and state, then returns the next prediction and updated state

That makes generation autoregressive, just like sequence generation in translation models, except the generated values are continuous vectors rather than token IDs.

When A Simpler Model Is Enough

Not every sequence regression problem needs a full encoder-decoder. If input and output lengths are the same, or if you only need one future vector, a plain LSTM or temporal convolution with a Dense head may be simpler and easier to train.

Seq2seq becomes worth the extra complexity when the forecasting structure is truly sequence-to-sequence and the output horizon has its own temporal dependencies.

Common Pitfalls

A frequent mistake is using a softmax output layer from a classification example. For regression, the output layer should usually be linear, which is what Dense(target_dims) gives you by default.

Another issue is mismatching tensor shapes. The model target must have shape (batch, output_steps, target_dims) if the decoder returns full output sequences.

Developers also sometimes forget that inference and training decoder inputs differ. Teacher forcing helps training, but the model must still know how to generate future steps from its own predictions.

Finally, normalize continuous features and targets when scales differ a lot. Seq2seq regression is much harder to optimize when some dimensions dominate the loss numerically.

Summary

  • Seq2seq models can handle multidimensional regression, not just token generation.
  • The decoder should output continuous vectors, usually through a linear Dense layer.
  • Teacher forcing is a common training strategy for decoder inputs.
  • Inference usually requires autoregressive decoding with separate encoder and decoder logic.
  • Use a simpler sequence model if your problem does not truly need encoder-decoder behavior.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.