Machine Translation
Machine translation turns a sentence in one language into the same meaning in another, picking each output word from the source words it attends to most.
Why Does This Exist?
Human translators can't keep up with the daily volume of product pages, support tickets, docs and messages that need converting. Rule systems tried to cover each language pair with hand-written grammar rules and dictionaries, and they broke on idioms, different word orders and words with several meanings.
Neural machine translation replaces those rules with a learned map from one sequence to another. It keeps meaning across different lengths and orders, which is why the same idea now powers cross-lingual search and multilingual assistants.
This page covers the encoder-decoder with attention: how source words become states, how the decoder picks what to read, and how output words are chosen one at a time. For turning raw strings into model-ready pieces first, see tokenization in NLP. For scoring whole-sentence meaning instead of translating, see sentence embeddings. For answering from text instead of converting languages, see question answering.
The one idea you need first
Text is split into tokens such as words or subwords, and each token becomes a vector the network can compute with. That split plus the vector is the whole prerequisite. The full treatment is in tokenization in NLP.
Think of It Like This
A bilingual secretary with sticky notes
You dictate a French memo and your secretary writes each phrase on a sticky note, one note per phrase, and spreads them on a desk. Before typing each English word they glance back at the notes and press a finger most firmly on the note that matters for that word.
The notes are the encoder states. The finger pressure is the attention weight: most on one note, some on the rest, always summing to full pressure. The typed English word is the decoder output.
Where it stops: the secretary truly understands both languages and remembers yesterday. The model keeps no meaning outside its vectors, and it forgets everything beyond the weights and the current states.
How It Actually Works
Call the source tokens , where each is one source piece such as a word. An embedding layer turns each piece into a vector , a list of numbers that stands for that piece. The encoder reads and returns one state per position, , where each mixes word with its surrounding context.
At output step the decoder holds a state , a summary of the English words written so far plus what it has read. It scores every source position against , producing raw scores , one number per source word. Attention turns those scores into weights that sum to 1, so reads as the share of focus on source word while writing target word . The context vector is that weighted blend:
In plain words: is the average of the encoder states, pulled toward whichever source words the weights favour. The decoder then combines with , passes the result through a feed-forward layer and a softmax, and outputs , the probability of each candidate next English word given the earlier English words and the full French input . The chosen word is fed back as input for step , until a stop token ends the sentence.
Why the weights sum to 1
Softmax turns raw scores into shares. With scores , exponentiate to , sum to , and divide to get . They sum to up to rounding, so raising one share must lower the others. That fixed budget is what forces the model to choose.
Worked example
Take the French input "Le chat dort" with three encoder states , , . Suppose the decoder scores for the next English word are , so the weights are as computed above.
The context vector is:
First coordinate: . Second: . The blend leans toward because its weight is largest, which is exactly the picture above: the thickest connector lands on the first source word, and the context dot sits closest to that state.
Code
This snippet recomputes the weights and the blend from the worked example, so you can check the arithmetic.
import math
def softmax(xs: list[float]) -> list[float]: m = max(xs) exps = [math.exp(x - m) for x in xs] s = sum(exps) return [e / s for e in exps]
scores = [2.0, 1.0, 0.0]w = softmax(scores)print([round(v, 3) for v in w]) # -> [0.665, 0.245, 0.09]c0 = w[0] * 1.0 + w[2] * 1.0c1 = w[1] * 1.0 + w[2] * 1.0print(round(c0, 3), round(c1, 3)) # -> 0.755 0.335Watch Out For
Trusting short outputs on long inputs
Early encoder-decoder models without attention squeezed every sentence into one fixed vector. Short sentences translated fine, then quality fell apart past roughly twenty words because detail had nowhere to live. The symptom is fluent beginnings that drift or drop clauses near the end. The fix is attention or a Transformer that keeps all states available, so the decoder rereads the source at each step instead of memorising it once.
Reading fluency as correctness
A neural translator can output smooth English that adds facts the French never stated, especially names, numbers and negations. The symptom is a confident sentence with zero awkwardness that back-translation exposes as wrong. The fix is to check entities and negation against the source for anything high stakes, and to keep beam outputs or attention peaks around to see which source words each English word actually came from.
The Quick Version
- Machine translation maps a source sequence to a target sequence of possibly different length, one output word at a time.
- The encoder turns each source token plus context into a state; the decoder writes from those states, not from a single squeezed vector.
- Attention weights sum to 1 and blend the states into a context vector; scores [2, 1, 0] give [0.665, 0.245, 0.090].
- The worked blend of [1.0, 0.0], [0.0, 1.0] and [1.0, 1.0] under those weights is [0.755, 0.335].
- Without attention, long sentences lose detail; with it, each output word rereads the source.
- Fluent output can still invent facts, so verify names, numbers and negation against the source.