Learn Before
Code

Seq2Seq Model Implementation

The Seq2Seq class is a concrete implementation that combines a separately defined RNN encoder and decoder into a single, trainable sequence-to-sequence model. It inherits from the EncoderDecoder base class and is initialized with four arguments: an encoder instance, a decoder instance, the index of the target-language padding token (tgt_pad), and the learning rate (lr). All of these constructor arguments are saved as class attributes via save_hyperparameters(). The class provides two key methods beyond the inherited forward pass. First, a validation_step method computes the model's predictions on a validation batch by calling the forward pass on all inputs except the labels, and then plots the resulting loss with train=False to track generalization performance. Second, a configure_optimizers method returns the Adam optimizer, parameterized with the specified learning rate, as the optimization algorithm for updating all learnable parameters during training.

0

1

Updated 2026-05-14

Contributors are:

Who are from:

Tags

D2L

Dive into Deep Learning @ D2L