← Back to Python

All Topics

Advertisement

Learn/Python/Deep Learning

Recurrent Layers

Topic: Keras

Advertisement

Introduction

Recurrent layers process sequential data by maintaining internal state and passing information across timesteps.

LSTM

from tensorflow.keras import layers

model = keras.Sequential([
    layers.LSTM(64, return_sequences=True, input_shape=(timesteps, features)),
    layers.LSTM(32),
    layers.Dense(10, activation='softmax')
])

# Bidirectional LSTM
model.add(layers.Bidirectional(layers.LSTM(64)))

GRU

# GRU - fewer parameters than LSTM
model = keras.Sequential([
    layers.GRU(64, return_sequences=True, input_shape=(timesteps, features)),
    layers.GRU(32),
    layers.Dense(10)
])

Stacking RNN Layers

model = keras.Sequential([
    layers.LSTM(64, return_sequences=True),
    layers.LSTM(32, return_sequences=True),
    layers.LSTM(16),
    layers.Dense(10)
])

RNN with State

# Stateful RNN maintains state across batches
model = keras.Sequential([
    layers.LSTM(64, batch_input_shape=(8, timesteps, features), stateful=True)
])

# Manual state reset
model.reset_states()

Practice Problems

  1. Build LSTM for sequence prediction
  2. Use Bidirectional LSTM
  3. Stack multiple LSTM layers
  4. Implement GRU
  5. Handle variable length sequences

Advertisement

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →