Neural Machine Translation › Encoder-Decoder Translation

Michael BrenndoerferNovember 26, 20263 min read
Language AI HandbookAll chapters
Part 27 of 67Machine Translation and Speech Translation
Chapter 191Neural Machine Translation › Encoder-Decoder Translation
Reading section 3 of 12 · Encoder-Decoder Translation · 3 minSection 3 of 12 · 3 min

Section 3 of 12 · 3 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

An early recurrent design used an LSTM to encode the source and another LSTM to generate the target, as in Sutskever et al. (2014). The Encoder-Decoder Framework gives the general setup.

The encoder reads the source and produces hidden states. A bidirectional RNN runs forward and backward and concatenates:

h→j=RNN⁡fwd(h→j−1,Exxj),h←j=RNN⁡bwd(h←j+1,Exxj)\overrightarrow{h}_j = \operatorname{RNN}_{\text{fwd}}\big(\overrightarrow{h}_{j-1}, E_x x_j\big), \qquad \overleftarrow{h}_j = \operatorname{RNN}_{\text{bwd}}\big(\overleftarrow{h}_{j+1}, E_x x_j\big) hj=[h→j;h←j]h_j = \big[\overrightarrow{h}_j ; \overleftarrow{h}_j\big]

For this bidirectional variant, the forward state depends on the prefix and the backward state depends on the suffix. Concatenating them lets the decoder's source representation incorporate context on both sides of a position.

The decoder is a conditional language model. At step tt it consumes the previous target token and its own previous state:

st=RNN⁡dec(st−1,Eyyt−1)s_t = \operatorname{RNN}_{\text{dec}}\big(s_{t-1}, E_y y_{t-1}\big) P(yt∣y<t,x)=[softmax⁡(Wost+bo)]ytP(y_t \mid y_{<t}, x) = \big[\operatorname{softmax}(W_o s_t + b_o)\big]_{y_t}

During autoregressive inference, the next-token distribution depends on the generated prefix. Decoding proceeds until an end token or a length limit. Each new token supplies part of the context for the next step.

Teacher forcing feeds the reference prefix y<ty_{<t} and scores yty_t. During generation the model uses its own prefix, which can contain earlier errors. Teacher Forcing describes the mismatch.

The fixed-vector bottleneck

In the fixed-vector design, the decoder receives one fixed-size source representation as its initial state. In this simplified notation, s0=hms_0=h_m denotes the final encoder state of a unidirectional encoder. A bidirectional variant can combine the terminal states of both directions.

Increasing the source length leaves the size of this representation fixed. The decoder can use encoded information about a source position only through that initial representation.

In their English-to-French experiment, Sutskever et al. (2014), §§3.3 and 3.5 found that reversing the source improved BLEU. They argued that the reversal shortened some early source-to-target dependencies, while the average distance between corresponding words was unchanged. The reversed-input model translated long sentences well in their length analysis.

Another way to give the decoder access to earlier source information is to retain a representation for each position and let the decoder select among them.

Attention removes the bottleneck

With attention the decoder can form a different weighted sum over the encoder states h1,…,hmh_1, \dots, h_m at each step. We covered the mechanism in Attention Intuition and Bahdanau Attention.

For decoder state st−1s_{t-1} and encoder state hjh_j, compute an energy:

et,j=v⊤tanh⁡(Wsst−1+Whhj)e_{t,j} = v^\top \tanh\big(W_s s_{t-1} + W_h h_j\big)

The energy is a learned score: how relevant is source position jj to the token the decoder is about to produce. The tanh⁡\tanh lets the score depend nonlinearly on both the decoder state and the encoder state, and the vector vv collapses the result to a scalar.

Normalize over source positions:

αt,j=exp⁡(et,j)∑j′=1mexp⁡(et,j′)\alpha_{t,j} = \frac{\exp(e_{t,j})}{\sum_{j'=1}^{m} \exp(e_{t,j'})}

The softmax turns the scores into a distribution over allowed source positions. This row of weights is recomputed at each decoder step.

Form the context vector and feed it into the decoder:

ct=∑j=1mαt,jhj,st=RNN⁡dec(st−1,[Eyyt−1;ct])c_t = \sum_{j=1}^{m} \alpha_{t,j} h_j, \qquad s_t = \operatorname{RNN}_{\text{dec}}\big(s_{t-1}, [E_y y_{t-1} ; c_t]\big)

The decoder can read h3h_3 through the attention sum, without requiring the encoder to carry the same information from position 3 all the way to its final state. Each decoder step still compresses the attended states into one context vector.

Bahdanau et al. (2015) reported that their attentive RNNsearch models held up better on longer English-to-French sentences than their fixed-vector RNNencdec baselines. Their qualitative examples included attention patterns resembling word correspondences, although the training objective used sentence pairs and supplied no word-alignment labels.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy