๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #236

Seq2Seq Model

The Seq2Seq (Sequence-to-Sequence) model is the complete, assembled system โ€” encoder, context vector, and decoder working together โ€” formalized as one end-to-end trainable architecture.

The Full Pipeline

\[ \mathbf{c} = \text{Encoder}(\mathbf{x}_1, \ldots, \mathbf{x}_T) \qquad \hat{\mathbf{y}}_1, \ldots, \hat{\mathbf{y}}_{T'} = \text{Decoder}(\mathbf{c}) \]

\(T\) and \(T'\) can be completely different โ€” this is exactly the length-independence established in Encoder-Decoder Architecture. The decoder generates its output one token at a time, feeding each generated token back in as input for producing the next one, continuing until it produces a special "end of sequence" token.

Diagram โ€” Autoregressive Decoding

context c <start> "J'aime" "les" "chiens" each generated word feeds back in as the next step's input

Generation is autoregressive โ€” each output token depends on every token generated before it, plus the original context vector.

Code โ€” A Simplified Full Decoding Loop

import torch

def generate(encoder, decoder, input_seq, start_token, end_token, max_len=20):
    h, c = encoder(input_seq)
    token = torch.tensor([[start_token]])
    output_tokens = []

    for _ in range(max_len):
        logits, h, c = decoder(token, h, c)
        next_token = logits.argmax(dim=-1)   # greedy decoding: pick the most likely token
        if next_token.item() == end_token:
            break
        output_tokens.append(next_token.item())
        token = next_token   # feed the generated token back in as the NEXT step's input

    return output_tokens

Training vs Inference โ€” A Key Difference

During inference (as shown above), each step's input is the model's own previous prediction. During training, a different strategy โ€” teacher forcing โ€” is typically used instead, feeding the true previous token rather than the model's own (possibly wrong, especially early in training) prediction. This distinction, and the subtlety it introduces, is the entire subject of the next note.

Common Mistakes

  • Confusing the training-time and inference-time decoding procedures โ€” they typically differ (teacher forcing during training, autoregressive generation during inference), and forgetting this distinction leads to a common category of subtle bugs.
  • Forgetting to handle the end-of-sequence token correctly โ€” without a stopping condition, greedy decoding could run indefinitely (hence the max_len safeguard above).

Interview Relevance

Q: "Why is decoding in a Seq2Seq model called 'autoregressive'?" Each generated token becomes part of the input for generating the next token โ€” the model's own previous outputs feed back into itself, one step at a time, rather than the entire output sequence being produced all at once. This mirrors the "auto-regressive" naming from statistics, where a value depends on its own previous values.

Practice Question

What would happen during inference if the decoder never learned to correctly predict an end-of-sequence token for a given input?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Seq2Seq Model โ€“ FAQs

Quick answers about learning Seq2Seq Model in Deep Learning.

This free note from CodingNow 2.0 explains Seq2Seq Model in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Seq2Seq Model, is 100% free with no signup required.
With focused practice, most students grasp Seq2Seq Model in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now