NLP coursework demoSeq2seqAttention

Machine Translation, Explained

A demo version of my notebook: English captions enter an encoder, attention decides what to look at, and the decoder produces French one token at a time.

Multi30k

paired caption dataset

LSTM + attention

custom encoder-decoder

BLEU + heatmaps

evaluation and inspection

Live translation trace

notebook-derived example

English source tokens

agroupofmenareloadingcottonontoatruck

Encoder states

French decoder output

Currently explaining un: the darker English tokens receive more attention for this French output step.

Part-by-part walkthrough

How the notebook becomes a translation system

Each step has a simple explanation, a short source snippet, and a talk track you can use when presenting it.

3. Attention

Look back at the useful source words

Attention prevents the decoder from relying on one fixed sentence vector. At each generated word, it scores the encoder outputs, masks padding, normalizes with softmax, and creates a context vector.

How I would explain it

The heatmap below is the story: for each French word, the model decides which English states deserve attention right now.

attention_scores = torch.bmm(
  encoder_outputs,
  weights.unsqueeze(-1)
).squeeze(-1)

masked_scores = attention_scores.masked_fill(mask == 0, float("-inf"))
attention_weights = F.softmax(masked_scores, dim=-1)
context = torch.sum(encoder_outputs * attention_weights.unsqueeze(-1), dim=1)

Attention lab

Click a French token to inspect where the model looks

This heatmap is an interactive reconstruction of the attention logic from the workbook. It makes the mechanism explainable without needing to run the training notebook live.

Example sentence

Attention scoring

Bilinear attention adds a learned projection before scoring, so the alignment can become sharper than a raw dot product.

a
group
of
men
are
loading
cotton
onto
a
truck

Decoding playground

The logits are not the final translation

The decoder produces a probability distribution. The decoding strategy turns that distribution into the next token. Changing the policy changes how deterministic or exploratory the output feels.

Candidate distribution

Next token: un

pool mass 82%
un50%
le21%
des11%
groupe8%
homme5%
camion3%
sur2%
bleue1%

What this demonstrates

I can explain the model from data to diagnostics

The page is deliberately built around explanation: not just what the code does, but why each choice changes translation behavior.

Model shape

Encoder-decoder LSTM, source and target tokenizers, output projection.

Attention

Dot-product versus bilinear scoring, padding masks, softmax, context vectors.

Decoding

Greedy, top-k, and top-p as different ways to sample next-token probabilities.

Evaluation

Early stopping on validation BLEU, checkpoint testing, and heatmap inspection.