Cs224n lecture 8 machine translation, seq2seq, attention

Screenshot 2019-12-31 at 1.03.35 PM.png

Alignment

Screenshot 2019-12-31 at 1.04.48 PM.png

Alighment Can be Complex! (Appendix 1)

Back in the day we used statistical machine translation (Appendix 2)

Neural Machine Translation

Sequence to Sequence (2014).

Seq2seq can also do many other NLP tasks-

Seq2Seq is an example of a conditional language model. The conditional is because its predictions are conditions on the source sentence x. In other words,

Screenshot 2019-12-31 at 1.16.27 PM.png

Screenshot 2019-12-31 at 1.13.51 PM.png

Training NMT systems

Get a big parallel corpus.

Feed source sentence into encoder RNN

Start out the first hidden state of the decoder RNN as the kit hidden state of the encoder RNN, and feed target sentence into decoder RNN.

For every step of the decoder RNN, produce the probability distribution of the next decoded word. Compute a loss over every word of the decoded output sequence as the negative CE of the next word. Average all of the losses to get the total losses for the example. (If the decoder produces <end> too early in training, keep going).

Seq2Seq is trained as a single system and Backprop operates “end-to-end”, from the final losses to the weights of the encoder RNN.

To deal with variable length sentences, we pad and short sentences to a pre-defined max length, and make sure that we don’t use any hidden states that came from the padding.

Screenshot 2019-12-31 at 1.21.46 PM.png

Decoding with Beam Search

The basic decoding method is greedy decoding, but often we do **beam search **to get better searches.

On each step of the decoder, keep track of the k most probable partial translation (which we call hypotheses)

k is the beam size (in practice around 5 to 10).

A hypothesis $y_1,…,y_t$ has a score which is its log probability. Scores are all negative, higher score is better. We search for high-(normalized) scoring hypotheses, tracking top k on each step. We do not normalize the scores when computing candidates using beam search.

Stopping in beam search: When a hypothesis produces $$, that hypothesis is complete. At this point we place it aside and continue exploring other hypothesis via beam search. We continue beam search until: We reach timestep threshold $T$, or we have at least $n$ completed hypothesis.

Selecting the final output senquence. When we have our final list of candidate scores, as generally shorter hypotheses have higher scores, to fix this, we must normalize the scores by length ($1/t$) before picking the final output.

Screenshot 2019-12-31 at 1.30.48 PM.png

Screenshot 2019-12-31 at 1.31.33 PM.png

BLEU- Bilingual Evaluation Understudy

Comparison of machine translation and human translation.

Look at all the 1,2,3 and 4 grams that appear in the machine translation, how many of those appeared in at least one human translations? (n-gram precision)

Attention

Attention solves the seq2seq informational bottleneck problem

Screenshot 2020-01-22 at 10.56.27 PM.png

Attention: On each step of the decoder, use a direct connection of the encoder to focus on a particular part of the source sequence. The attention score is basically a dot product.

Screenshot 2020-01-22 at 10.57.53 PM.png** **

(1) Given a decoder state, we generate the attention score with each encoder state, which is just the dot product.

Screenshot 2020-01-22 at 10.58.30 PM.png** **

(2) We take the softmax all those hidden states to determine the attention distribution. This is the weighting of importances of encoder states to the current decoder state.

Screenshot 2020-01-22 at 10.59.11 PM.png

(3) We then form the attention output, which is the weighted interpolation of the source encoder hidden states weighted by the attention distribution.

Screenshot 2020-01-22 at 11.02.15 PM.png

(4) Concatenate attention output with decoder hidden state and use model to predict the next word. The dependency of the next word on the attention part of the full concatenated state is learned from training data.

(Optional)

Screenshot 2020-01-22 at 11.04.56 PM.png

(5- optional) Sometimes we take the attention output encoding from the previous step and also feed it into the decoder along with the usual decoder input.

Attention is soft alignment.

Attention in equations:

Screenshot 2020-01-22 at 11.08.33 PM.png

Generalized Attention

Given a set of vector values and a vector query, attention is a technique to compuite a weighted sum of the values dependent on the query. We say the query attends to the values.

Interpretation of Attention

Ex) In seq2seq, the decoder hidden state is the query and they attend to all the encoder hidden states, which are the values.

Variants of Attention

In all attention variants we have some values $h_1,…,h_N \in R^{d_1}$ and a query $s \in R^{d_2}$

  1. Compute the attention scores $e \in R^N$
  2. Take softmax to get the attention distribution $\alpha$
  3. Use the attention distribution to take the weighted sum of values

The variants of attention are as follows:

Benefits of Attention:

** **

Essential Tips to Training RNN Models

  1. Use an LSTM or GRU

2. Initlaize recurrent matrices to be orthogonal

3. Intialize other matrices with a sensible (small!) scale

4. Initalize forget gate bias to 1: default to remembering

5. Use Adam or AdaDelta or AdaGrad

6. Clip the norm of the gradient: 1-5 seems to be a reasonable threshold when used together with Adam or AdaDelta

7. Either only dropout vertically or look into using Bayesian Dropout (Gal and Gahramani - not natively in PyTorch yet unf)

8. Be patient!

Experimental Strategy:

Work incrementally!

Start with a very simple model and get it to work

Add bells and whistles one-by-one and get the model working with each of them (or abandom them)

1. Initially run on a tiny amount of data

2. Then run your model on a large dataset

Research ideas:

Learning Temporal Events In text - what came before, what came after

Look at the ‘residual encodings’ in NMT systems - the encoding remaining in the decoder hidden state after <EOS> is produced. These encodings must capture something inherent in the language pair.

Appendix 1:

Note: Calculating softmaxes at the end ismore expensive than doing the RNN computation - because the vocab size is huge!

** **

Screenshot 2019-12-31 at 1.05.30 PM.pngScreenshot 2019-12-31 at 1.06.05 PM.png** **

Screenshot 2019-12-31 at 1.06.28 PM.png

Appendix 2: Statistical Machine Translation

Essentially, form many candidate output sentences based on different possible the direct translations of words and different alignments. The number was too much to put in memory so candidates with low probability are pruned out.

SMT had lots and lots of subcomponents for feature engineering, using lots of tables etc.

** **

Screenshot 2019-12-31 at 1.09.08 PM.png

Appendix 3: How to deal with Unknown tokens in vocabualary:

Appendix 3: BLEU Metric


BLEU is a weighted geometric mean, with a brevity penalty factor added, on n-grams. Note that it’s precision oriented, so p1 etc is the proportion of 1-grams in the machine translation also in the reference translation, and so on. Note that in BLEU, you are are only allowed to use each 1-gram in the regerence translation once to match the machine translation for a given 1-gram level, and similary for all levels of n. n=1,2,3,4 us ually used.

exp( 0.5 * log p1 + 
0.25 * log p2 +
0.125 * log p3 +
0.125 * log p4 -
max(words-in-refernece / words-in-machine - 1, 0) )