Lensa ML
Lensa ML

Seq2Seq & Beam Search

Step 1 of 6

Encoder-Decoder Idea

Compress input → fixed vector → generate output

Input sequence length4
EncoderDecodertheh1cath2ish3smallh4contexts1les2chats3ests4petit4 tokens → 1 vector
INPUT LEN
4
OUTPUT LEN
4
BOTTLENECK
1 vector

4 input tokens → 1 context vector → 4 output tokens. No matter how long the input, the encoder must pack everything into one vector. The decoder never sees the original tokens — only this summary.

Step 1 of 6: Encoder-Decoder Idea

Compress input → fixed vector → generate output

Input sequence length4
EncoderDecodertheh1cath2ish3smallh4contexts1les2chats3ests4petit4 tokens → 1 vector
INPUT LEN
4
OUTPUT LEN
4
BOTTLENECK
1 vector

4 input tokens → 1 context vector → 4 output tokens. No matter how long the input, the encoder must pack everything into one vector. The decoder never sees the original tokens — only this summary.

Step 2 of 6: Encoding

The encoder RNN processes input tokens one at a time

Time step0
Encoder RNNIh1loveh2deeph3learningh4Hidden state evolutiond1d2d3d4d5d6h1h2h3h40.380.600.250.74-0.300.56h1 = tanh( W_h · h0 + W_x · embed("I") )
TIME STEP
1 / 4
TOKEN
I
‖h‖
1.233
Δ FROM PREV
1.233

Processing "I": h1 is computed from the zero-initialized state + the input embedding. The heatmap shows the 6D hidden vector — each cell's color and value reveals what the RNN "remembers." Green = positive, purple = negative.

Step 3 of 6: Decoding

The decoder generates output tokens autoregressively

Decoding step0
contextDecoder<SOS>s1J'J's2aimes3les4deeps5learnings6softmax → P(token | s1)J'62%Je21%Le8%Il5%Un4%
STEP
1 / 6
INPUT
<SOS>
OUTPUT
J'
CONFIDENCE
62%

Decoding begins: <SOS> + context vector → s1 → softmax picks "J'" (62% confidence). The curved arrow shows this output feeding back as the next step's input — this is autoregressive decoding.

Step 4 of 6: Greedy vs Beam Search

Exploring multiple hypotheses for better translations

Beam width1
t=0t=1t=2SOSJ'-0.95Un-1.30Je-1.30Le-2.11aime-2.00suis-1.48veux-2.36Best: SOS → J' suis (-1.48)keptpruned
BEAM WIDTH
1
EXPANDED
3
PRUNED
2
BEST SCORE
-1.48

Greedy decoding (beam=1): only the single best token survives at each step. 3 candidates pruned at t=1, 2 at t=2. Fast but myopic — the locally best token may not lead to the globally best sequence.

Step 5 of 6: Beam Scores

Ranking candidate translations by log-probability

Length penalty α0.00
← from beam treecontinued decoding →#1J'-0.31aime-0.42le-0.38deep-0.22learning-0.35-1.68raw: -1.68 / 5#2J'-0.31suis-0.65deep-0.48learning-0.40-1.84raw: -1.84 / 4#3J'-0.31veux-0.85le-0.30deep-0.25learning-0.38-2.09raw: -2.09 / 5#4Je-0.90suis-0.50profond-1.20-2.60raw: -2.60 / 3#5Le-0.72aime-0.55le-0.38apprentissage-0.95-2.60raw: -2.60 / 4raw score
α
0.00
BEST
-1.68
BEST HYP
#1
LEN RANGE
3–5

No length penalty (α=0): raw log-prob sum determines rank. The amber-bordered tokens (first two columns) are the beam paths from step 4 — each hypothesis extends a surviving beam. Shorter sequences win unfairly here. Try increasing α.

Step 6 of 6: Limitations & Attention

The information bottleneck motivates the attention mechanism

Input sequence length4
Input lengthBLEU scoreSeq2Seq+ Attention
INPUT LEN
4
SEQ2SEQ
38.3
+ ATTENTION
41.7
GAP
+3.4

Short inputs: Seq2Seq works well! The fixed-size context vector can hold enough information. The attention advantage is small here.