TensorFlow
Dense Layer
LSTM
Neural Networks
Deep Learning

TensorFlow Combining Dense Layer with LSTM Cell

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

Combining an LSTM with a Dense layer is a normal sequence-model design in TensorFlow. The LSTM processes temporal structure, and the Dense layer turns the LSTM representation into the final prediction you actually care about.

The key design choice is where the Dense layer should act. Sometimes it should consume the final LSTM output for sequence classification, and sometimes it should be applied at every time step for sequence-to-sequence style outputs.

Dense After the Final LSTM Output

For tasks such as sentiment classification or sequence-level regression, the most common pattern is an LSTM followed by a Dense layer:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(20, 8)),
5    tf.keras.layers.LSTM(32),
6    tf.keras.layers.Dense(1)
7])
8
9model.summary()

Here, the LSTM reads the whole sequence and emits one final output vector. The Dense layer maps that vector to the target.

This pattern is appropriate when the whole sequence should produce one answer, such as:

  • one sentiment label
  • one forecast value
  • one class prediction

Dense at Every Time Step

If you need one prediction per time step, the Dense layer should be applied over the sequence outputs rather than only the last one. In Keras, that usually means keeping return_sequences=True and then using TimeDistributed or relying on broadcasting behavior from a Dense layer over the last axis.

python
1import tensorflow as tf
2
3inputs = tf.keras.Input(shape=(20, 8))
4x = tf.keras.layers.LSTM(32, return_sequences=True)(inputs)
5outputs = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(5))(x)
6model = tf.keras.Model(inputs, outputs)
7
8model.summary()

Now the model emits a 5-dimensional prediction at each time step. That is useful for tagging, sequence labeling, or per-step forecasting.

Dense Before the LSTM

A Dense layer can also appear before the LSTM if you want to project or transform features before recurrence:

python
1import tensorflow as tf
2
3inputs = tf.keras.Input(shape=(20, 8))
4x = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(16, activation="relu"))(inputs)
5x = tf.keras.layers.LSTM(32)(x)
6outputs = tf.keras.layers.Dense(1)(x)
7model = tf.keras.Model(inputs, outputs)

This can be useful when the raw feature vector at each time step needs a learned nonlinear projection before the recurrent layer processes it.

So "combining Dense with LSTM" is not one single architecture. It depends on whether the Dense layer is acting before recurrence, after recurrence, or at each time step.

The Cell-Level Version Is More Advanced

If you are working with low-level LSTMCell objects rather than high-level LSTM layers, you can still place a Dense transformation around the recurrent computation, but most everyday models do not need that level of manual control.

In practice, Keras LSTM layers plus ordinary Dense layers are clearer and easier to maintain than building a custom recurrent cell just to mix in a feed-forward transform.

Common Pitfalls

  • Forgetting return_sequences=True when you need a prediction at every time step.
  • Using a final Dense layer after an LSTM and expecting a full output sequence instead of one vector.
  • Applying a Dense layer to raw sequence tensors without being clear whether it acts per time step or on the final summary vector.
  • Building a custom LSTMCell solution when a standard Keras layer stack would be much simpler.
  • Confusing sequence classification with sequence-to-sequence prediction and choosing the wrong placement for the Dense layer.

Summary

  • The most common design is LSTM followed by Dense for one output per sequence.
  • If you need one output per time step, keep return_sequences=True and apply Dense across the sequence outputs.
  • A Dense layer can also be used before the LSTM as a per-step feature projection.
  • The right arrangement depends on whether the task is sequence-level or time-step-level.
  • In most TensorFlow code, high-level Keras layers are simpler than custom cell-level wiring.

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.