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
- Build LSTM for sequence prediction
- Use Bidirectional LSTM
- Stack multiple LSTM layers
- Implement GRU
- Handle variable length sequences