Sequence-to-sequence models help computers turn one sequence of things into another sequence. For example, changing a sentence in English into a sentence in French.
Sequence-to-sequence basics in TensorFlow
import tensorflow as tf from tensorflow.keras.models import Model from tensorflow.keras.layers import Input, LSTM, Dense # Encoder encoder_inputs = Input(shape=(None, num_features)) encoder_lstm = LSTM(units, return_state=True) encoder_outputs, state_h, state_c = encoder_lstm(encoder_inputs) encoder_states = [state_h, state_c] # Decoder decoder_inputs = Input(shape=(None, num_features)) decoder_lstm = LSTM(units, return_sequences=True, return_state=True) decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states) decoder_dense = Dense(num_classes, activation='softmax') decoder_outputs = decoder_dense(decoder_outputs) # Model model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
The encoder reads the input sequence and summarizes it into states.
The decoder uses these states to generate the output sequence step-by-step.
encoder_inputs = Input(shape=(None, 50)) encoder_lstm = LSTM(64, return_state=True) encoder_outputs, state_h, state_c = encoder_lstm(encoder_inputs) encoder_states = [state_h, state_c]
decoder_inputs = Input(shape=(None, 50)) decoder_lstm = LSTM(64, return_sequences=True, return_state=True) decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states) decoder_dense = Dense(100, activation='softmax') decoder_outputs = decoder_dense(decoder_outputs)
This example builds a simple sequence-to-sequence model with LSTM layers. It trains on random data for 2 epochs and then predicts output for one sample.
import numpy as np import tensorflow as tf from tensorflow.keras.models import Model from tensorflow.keras.layers import Input, LSTM, Dense # Parameters num_encoder_tokens = 10 num_decoder_tokens = 15 latent_dim = 16 # Encoder encoder_inputs = Input(shape=(None, num_encoder_tokens)) encoder_lstm = LSTM(latent_dim, return_state=True) encoder_outputs, state_h, state_c = encoder_lstm(encoder_inputs) encoder_states = [state_h, state_c] # Decoder decoder_inputs = Input(shape=(None, num_decoder_tokens)) decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True) decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states) decoder_dense = Dense(num_decoder_tokens, activation='softmax') decoder_outputs = decoder_dense(decoder_outputs) # Model model = Model([encoder_inputs, decoder_inputs], decoder_outputs) # Compile model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # Dummy data encoder_input_data = np.random.random((100, 5, num_encoder_tokens)) decoder_input_data = np.random.random((100, 6, num_decoder_tokens)) decoder_target_data = np.random.random((100, 6, num_decoder_tokens)) # Train history = model.fit([encoder_input_data, decoder_input_data], decoder_target_data, batch_size=16, epochs=2) # Predict on one sample test_encoder_input = encoder_input_data[:1] test_decoder_input = decoder_input_data[:1] predictions = model.predict([test_encoder_input, test_decoder_input]) print(f"Predictions shape: {predictions.shape}")
Sequence lengths can vary, so we use None in input shapes.
Return states from the encoder to pass to the decoder for context.
Softmax activation in the decoder output layer helps pick the most likely next item.
Sequence-to-sequence models turn one sequence into another, like translating languages.
They use an encoder to understand the input and a decoder to create the output.
TensorFlow makes it easy to build these models with LSTM layers and functional API.
Practice
Solution
Step 1: Understand the encoder's function
The encoder reads the input sequence and compresses its information into a fixed-size context vector.Step 2: Differentiate encoder from decoder
The decoder uses this context vector to generate the output sequence, so the encoder does not generate output directly.Final Answer:
To process the input sequence and create a summary representation -> Option BQuick Check:
Encoder = input summary [OK]
- Confusing encoder with decoder
- Thinking encoder generates output sequence
- Assuming encoder shuffles data
Solution
Step 1: Identify sequence processing layers
LSTM layers are designed to handle sequences by remembering information over time steps.Step 2: Exclude other layers
Dense layers are fully connected but not specialized for sequences; Conv2D is for images; Dropout is for regularization.Final Answer:
LSTM -> Option CQuick Check:
LSTM = sequence layer [OK]
- Choosing Dense for sequence memory
- Confusing Conv2D with sequence layers
- Using Dropout as main sequence layer
Solution
Step 1: Understand LSTM output with return_sequences=True
When return_sequences=True, LSTM returns output for each time step, so output shape is (batch_size, time_steps, units).Step 2: Match units and input shape
The LSTM has 128 units and input time steps are 10, output shape is (batch_size, 10, 128).Final Answer:
(batch_size, 10, 128) -> Option AQuick Check:
return_sequences=True keeps time steps [OK]
- Ignoring return_sequences effect
- Confusing units with input feature size
- Assuming output shape drops time steps
decoder_lstm = tf.keras.layers.LSTM(64, return_sequences=True, return_state=True) outputs, state_h = decoder_lstm(decoder_inputs, initial_state=encoder_states)
Solution
Step 1: Check LSTM return values
When return_state=True, LSTM returns three outputs: output, state_h, and state_c.Step 2: Verify unpacking variables
The code unpacks only two variables, missing the cell state (state_c), causing an error.Final Answer:
Missing unpacking of the second state variable -> Option DQuick Check:
return_state=True returns 3 outputs [OK]
- Unpacking only two outputs instead of three
- Changing units without reason
- Misusing return_sequences parameter
Solution
Step 1: Understand encoder-decoder connection
The encoder processes input and produces states that summarize the input sequence.Step 2: Use encoder states to initialize decoder
Passing encoder states as initial_state to decoder LSTM allows the decoder to start generating output based on input context.Final Answer:
Use encoder LSTM to get states, then pass these states as initial_state to decoder LSTM -> Option AQuick Check:
Encoder states initialize decoder [OK]
- Ignoring encoder states in decoder
- Feeding decoder inputs to encoder
- Using only Dense layers for sequences
Ready to prove it?
Pass the test and this pattern moves unknown
