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.
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.
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:
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
Denselayer. - 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
- Tensorflow Sequence to sequence model using the seq2seq API ver 1.1 and above
- Tensorflow server I don't want to initialize global variables for every session
- Tensorflow Serving - Stateful LSTM
- Tensorflow Serving grouped convolutions
- Tensorflow serving No assets to save/writes when exporting models
- Tensorflow serving No versions of servable MODEL found under base path
- Tensorflow Serving Retrain using Inception Examples
- Tensorflow Serving When to use it rather than simple inference inside Flask service?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.