Tensorflow `RNN` many to many Time series for binary labels
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
For a many-to-many time-series classifier, the model should emit one prediction per timestep rather than one prediction for the entire sequence. In TensorFlow, that usually means an RNN layer with return_sequences=True, followed by a sigmoid output applied across the sequence so each timestep gets its own binary score.
What Many-to-Many Means Here
Suppose each input sequence has shape (timesteps, features) and each timestep also has a binary label. Then the target shape is not one label per sequence. It is one label per timestep.
Typical shapes:
- input:
(batch, timesteps, features) - target:
(batch, timesteps, 1)
That is different from many-to-one classification, where the target might be only (batch, 1).
Build an RNN That Returns a Sequence
The most important configuration is return_sequences=True. Without it, the RNN outputs only the final hidden state, which is wrong for timestep-level labels.
The final Dense(1, activation="sigmoid") is applied at every timestep because the input to the dense layer is a 3D tensor. Keras handles that across the last axis automatically.
Train on Sequence Labels
Now create data where every timestep has its own label. The example below marks a timestep as 1 when the first feature is positive.
This is a simple synthetic dataset, but it demonstrates the correct shape relationship between inputs and labels.
Inspect Per-Timestep Predictions
Inference returns a probability for each timestep.
The output shape is (1, timesteps, 1), which is exactly what you want for many-to-many binary classification.
To turn probabilities into binary labels:
Padding and Masking
Real time-series datasets often contain sequences of different lengths. In that case, pad them to a common length and use masking so the model does not treat padding as real timesteps.
If you pad the labels too, be careful during loss computation. The padded timesteps should not influence the metric the same way real timesteps do.
When Bidirectional Layers Help
If labels depend on both past and future context, a bidirectional RNN can improve performance.
This is useful for offline labeling tasks, but less suitable when predictions must be causal and available in real time.
Common Pitfalls
- Forgetting
return_sequences=Trueand accidentally producing one output per sequence instead of one per timestep. - Shaping labels as
(batch, 1)when the task actually needs(batch, timesteps, 1). - Using the wrong loss for binary timestep labels.
- Ignoring padding and letting padded timesteps distort training.
- Expecting the model to be causal when a bidirectional layer uses future context by design.
Summary
- For timestep-level binary labels, use a many-to-many RNN with
return_sequences=True. - Keep target labels aligned to the sequence shape, usually
(batch, timesteps, 1). - A sigmoid dense layer on top of the RNN gives one binary probability per timestep.
- Use masking when sequences are padded.
- Choose bidirectional layers only when future context is allowed by the task.
Related reading
- tensorflow running error with cublas
- Tensorflow Sampled Softmax \`Loss\` Correct Usage
- Tensorflow save the model with smallest validation error
- TensorFlow Saver has 5 models limit
- Tensorflow save the model with smallest validation error
- TensorFlow Saver has 5 models limit
- TensorFlow saving into/loading a graph from a file
- tensorflow scalar summary tags name exception
.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.