Word-based Machine Translation

Machine Translation (MT) is one of the canonical NLP applications, and one that nowadays most people are familiar with, primarily through online translation services of the major search engine providers. While there is still some way to go before machines can provide fluent and flawless translations, in particular for more distant language pairs like English and Japanese, progress in this field has been remarkable. MT is one of the most widely used NLP application. It is an example of end-to-end NLP, and MT-like architectures can be found in many other applications.

In this chapter we will illustrate the foundations of this progress, and focus on word-based machine translation models. In such models words are the basic unit of translation. Nowadays the field has mostly moved to phrase and syntax-based approaches, but the word-based approach is still important, both from a foundational point of view, and as sub-component in more complex approaches. We focus on word-based MT which is not the state-of-the-art anymore, but a foundation and blueprint for modern mechanisms.

MT as Structured Prediction

Formally we will see MT as the task of translating a source sentence s to a target sentence t. We can tackle the problem using the structured prediction recipe: We define a parametrised model sθ(t,s) that measures how well a target t sentence matches a source sentence s, learn the parameters θ from training data, and then find Define a parametrised model sθ(t,s) measuring how well a target sentence t matches a source sentence s, learn the parameters θ from training data, and find

(1)argmaxtsθ(t,s)

as translation of s. Different statistical MT approaches, in this view, differ primarily in how s is defined, θ are learned, and how the argmax is found.

Noisy Channel Model for MT

Many Word-based MT systems, as well as those based on more advanced representations, rely on a Noisy Channel model as choice for the scoring function sθ. In this approach to MT we effectively model the translation process in reverse. That is, we assume that a probabilistic process (the speaker's brain) first generates the target sentence t according to the distribution p(t). Then the target sentence t is transmitted through a noisy channel p(s|t) that translates t into s.

Hence translation is seen as adding noise to a clean t. This generative story defines a joint distribution over target and source sentences p(s,t)=p(t)p(s|t). We can in turn operate this distribution in the direction we actually care about: to infer a target sentence t given a source sentence s we find the maximum a posteriori sentence The distribution over target words and the channel noise model define a joint distribution p(s,t)=p(t)p(s|t). To use it for translation we operate it backwards:

(2)t=argmaxtp(t|s)=argmaxtp(t)p(s|t).

For the structured prediction recipe this means setting

sθ(t,s)=p(t)p(s|t).

In the noisy channel approach for MT the distribution p(t) that generates the target sentence is usually referred to as language model, and the noisy channel is called the translation model. As we have discussed language models earlier, in this chapter we focus on the translation model p(s|t).

A Naive Baseline Translation Model

The most straightforward translation model translates words one-by-one, in the order of appearance:

pθNaive(s|t)=ilength(s)θsi,ti
where θs,t is the probability of translating t as s. θ is often referred to as translation table.

For many language pairs one can acquire training sets Dtrain=((si,ti))i=1n of paired source and target sentences. For example, for French and English the Aligned Hansards of the Parliament of Canada can be used. Given such a training set Dtrain we can learn the parameters θ using the Maximum Likelhood estimator. In the case of our Naive model this amounts to setting Using parallel data of paired source and target sentences, we can train the model using the Maximum Likelihood Estimator:

θs,t=#Dtrain(s,t)#Dtrain(t)

Here #Dtrain(s,t) is the number of times we see target word t translated as source word s, and #Dtrain(t) the number of times we the target word t in total.

Training the Naive Model

Let us prepare some toy data to show how train this naive model.

4

Notice how we transformed raw strings into Document objects via segment, and how we then fill the training set with Sentence objects by extracting the documents head sentence from each document. This dataset can be used to train the naive model as follows. We can train the naive model as follows.

Let us train on the toy dataset:

(das,is)(mein,is)(ist,is)0.00.20.40.00.50.250.250.50

Decoding with the Naive Model

Decoding in MT is the task of finding the solution to equation 1. That is, we need to find that target sentence with maximum a posteriori probability, which is equivalent to finding the target sentence with maximum likelihood as per equation 2. The phrase "decoding" relates to the noisy channel analogy. Somebody generated a message, the channel encodes (translates) this message and the receiver needs to find out what the original message was.

In the naive model decoding is trivial if we assume a unigram language model. We need to choose, for each source word, the target word with maximal product of translation and language model probability. For more complex models this is not sufficient, and we discuss a more powerful decoding method later.

my house the small

The naive model is broken in several ways. Most severely, it ignores the fact that word order can differ and still yield (roughly) the same meaning.

IBM Model 2

The IBM Model 2 is one of the most influential translation models, even though these days it is only indirectly used in actual MT systems, for example to initialize translation and alignment models. As IBM Model 2 can be understood as generalization of IBM Model 1, we omit the latter for now and briefly illustrate it afterward our introduction of Model 2. Notice that parts of these exposition are based on the excellent lecture notes on IBM Model 1 and 2 of Mike Collins. IBM Model 2 is one of the most influential translation models, even though these days it is only indirectly used in actual MT systems, for example to initialize translation and alignment models.

Alignment

The core difference of Model 2 to our naive baseline model is the introduction of latent auxiliary variables: the word to word alignment a between words. In particular, we introduce a variable ai[0length(t)] for each source sentence index i[1length(s)]. The word alignment ai=j means that the source word at token i is aligned with the target word at index j.

Notice that ai can be 0. This corresponds to a imaginary NULL token t0 in the target sentence and allows source words to be omitted in an alignment.

Below you see a simple example of an alignment.

NULLthehouseissmallkleinistdasHaus
NULL小さいいですThehouseissmall

IBM Model 2 defines a conditional distribution p(s,a|t) over both the source sentence s and its alignment a to the target sentence t. Such a model can be used as translation model p(s|t), as defined above, by marginalizing out the alignment Model 2 defines a distribution p(s,a|t). A translation model can be derived by marginalizing out the alignments

p(s|t)=ap(s,a|t).

Model Parametrization

IBM Model 2 defines its conditional distribution over source and alignments using two sets of parameters θ=(α,β). Here α(s|t) is a parameter defining the probability of translation target word t into source word s, and β(j|i,lt,ls) a parameter that defines the probability of aligning the source word at token i with the target word at token j, conditioned on the length lt of the target sentence, and the length ls of the source sentence.

With the above parameters, IBM Model 2 defines a conditional distribution over source sentences and alignments, conditioned on a target sentence and a desired source sentence length ls: Model 2 defines a conditional distribution over source sentences and alignments, conditioned on a target sentence and a desired source sentence length

(3)pθIBM2(s1sls,a1als|t1tlt,ls)=ilsα(si|tai)β(ai|i,lt,ls)

Training IBM Model 2 with the EM Algorithm

Training IBM Model 2 is less straightforward than training our naive baseline. The main reason is the lack of gold alignments in the training data. That is, while we can quite easily find, or heuristically construct, sentence-aligned corpora like our toy dataset, we generally do not have word aligned sentences.

To overcome this problem, IBM Model can be trained using the Expectation Maximization (EM) Algorithm, a general recipe when learning with partially observed data—in our case the data is partially observed because we observe the source and target sentences, but not their alignments. The EM algorithm maximizes a lower bound of the log-likelihood of the data. The log-likelihood of the data is The Expectation Maximization (EM) Algorithm is a standard training algorithm for partially observed data. It maximizes a lower bound of the likelihood:

(ti,si)DtrainlogpθIBM2(si|ti)=(ti,si)DtrainlogapθIBM2(si,a|ti)

EM can be be seen as block coordinate descent on this bound.

The EM algorithm is an iterative method that iterates between two steps, the E-step (Expectation) and the M-Step (Maximization), until convergence. For the case of IBM Model 2 the E and M steps are instantiated as follows: EM for Model 2 iterates between:

  • E-Step: given a current set of parameters θ, calculate the expectations π of the latent alignment variables under the model pθIBM2 — this amounts to estimating a soft alignment for each sentence.
  • M-Step: Given training set of soft alignments π, find new parameters θ that maximize the log likelihood of this (weighted) training set. This amounts to soft counting.

E-Step

The E-Step calculates the distribution

π(a|s,t)=pθIBM2(a|s,t)

for the current parameters θ. For Model 2 this distribution has a very simple form:

π(a|s,t)=ilsπ(ai|s,t,i)=ilsα(si|tai)β(ai|i,lt,ls)jltα(si|tj)β(j|i,lt,ls)

Importantly, the distribution over alignments factorizes in a per-source-token fashion, and hence we only need to calculate, for each source token i and each possible alignment ai, the probability (or expectation) π(ai|s,t,i).

Before we look at the implementation of this algorithm we will set up the training data to be compatible with our formulation. This involves introducing a 'NULL' token to each target sentence to allow source tokens to remain unaligned. We also gather a few statistics that will be useful in our implementation later on. First we create some toy data:

We can now implement the E-Step.

Let us run this code:

NULLthehouseissmallkleinistdasHaus

You can play around with the initialization of β to see how the alignments react to changes of the word-to-word translation probabilities.

M-Step

The M-Step optimizes a weighted or expected version of the log-likelihood of the data, using the distribution π from the last E-Step:

θ=argmaxθ(t,s)Dtrainaπ(a|t,s)logpθIBM2(s,a|t)

The summing over hidden alignments seems daunting, but because π factorizes as we discussed above, we again have a simple closed-form solution:

α(s|t)=(t,s)ilsjltπ(j|i)δ(s,si)δ(t,tj)(t,s)jltδ(t,tj)

where δ(x,y) is 1 if x=y and 0 otherwise. The updates for β are similar.

Let us implement the M-Step now. In this step we estimate parameters θ from a given set of (soft) alignments a.

Let us run one M-Step on the alignments we estimated earlier.

(Haus,is)(ein,is)(groß,is)(mein,is)(das,is)(klein,is)(lang,is)(Gebäude,is)(Mann,is)(ist,is)0.00.10.20.00.20.100.050.100.050.150.100.050.100.050.25

Notice that the algorithm already figured out that "is" is most likely translated to "ist". This is because it is (softly) aligned with "is" in every sentence, whereas other German words only appear in a subset of the sentences.

Initialization (IBM Model 1)

We could already iteratively call eStep and mStepuntil convergence. However, a crucial question is how to initialize the model parameters for the first call to 'eStep'. So far we used a uniform initialization, but given that the EM algorithm's results usually depend significantly on initialization, using a more informed starting point can be useful. Due to non-convexity of the EM bound, good initialization is crucial.

A common way to initialize EM for IBM Model 2 training is to first train the so called IBM Model 1 using EM. This model really is an instantiation of Model 2 with a specific and fixed alignment parameter set β. Instead of estimating β it is set to assign uniform probability to all target tokens with respect to a given length: We can train IBM Model 1, which fixes the distortion parameters β to be uniform:

β(ai|i,lt,ls)=1lt+1

After training the parameters θ of Model 1 can be used to initialize EM for Model 2.

Training Model 1 using EM could have the same initialization problem. Fortunately it turns out that with β fixed in this way it can be shown, under mild conditions, that EM will converge to a global optimum, making IBM Model 1 robust to choices of initialization. EM for IBM Model 1 converges to a global optimum.

Let us train IBM Model 1 now. This amounts to using our previous eStep and mStep methods, initializing β as above and not updating it during mStep.

You can see below that the alignments converge relatively quickly.

20.0040.0060.0080.00X0.0099.000.020.040.060.080.100.120.140.160.18Y0.000.20Label
Exercise 1
Can you think of other reasonable measures for convergence? Hint: consider the formal derivation of EM.

Let us have a look at the translation table.

(mein,house)(das,house)(Haus,house)(ist,house)(klein,house)0.00.20.40.00.50.000.000.500.000.50

We can also inspect the alignments generated during EM.

NULLthehouseissmallkleinistdasHaus

Training IBM Model 2

Now that we have a reasonable initial model we can use it to initialize EM for IBM Model 2. Here is the EM code in full.

Initializing with the IBM Model 1 result gives us:

(mein,house)(das,house)(Haus,house)(ist,house)(klein,house)0.00.51.00.01.00.000.001.000.000.00

For alignments we get:

NULLthehouseissmallkleinistdasHaus

Let us look at the distortion probabilities for a given source position and source and target lengths.

012340.00.51.00.01.00.000.000.000.001.00

Decoding for IBM Model 2

Decoding IBM Model 2 requires us to solve the argmax problem in equation 2, this time using the conditional probability from equation 3 with the hidden alignments marginalized out: Decoding IBM Model 2 ideally means:

(4)argmaxtpθIBM2(s|t)=argmaxtapθIBM2(s,a|t)

This nested argmax and sum is generally computationally very hard (see Park and Darwiche), and often replaced with the simpler problem of finding a combination of best target sequence and corresponding alignment. This nested argmax/sum is generally computationally very hard. It's easier to argmax over target and alignments:

(5)argmaxt,apθIBM2(s,a|t)

As it turns out for IBM Model 2 the sum can be efficiently calculated, and Wang and Waibel show a stack based decoder that does take this into account.

However, both for simplicity of exposition and because for most real-world models this marginalization is not possible, we present a decoder that searches over both target and alignment. To simplify the algorithm further we assume that target and source sentences have to have them same length. Of course this is a major restriction, and it is not necessary, but makes the algorithm easier to explain while maintaining the core mechanism. Here we only show only the Scala code and refer the reader to our slides for an illustration of how stack and beam based decoders work. A simplified same-length decoder in Scala, using a beam:

Let us test this decoder on a simple sentence, using a uniform language model.

NULL _ _ _ _ 4 0.0
NULL a _ _ ein _ 3 -2.3978952727983707
NULL man groß _ _ _ 3 -Infinity
NULL a man _ _ ein Mann 2 -4.795790545596741
NULL a my groß _ ein _ 2 -Infinity
NULL a man is _ ist ein Mann 1 -7.886832998955057
NULL a man man _ ist ein Mann 1 -Infinity
NULL a man is big groß ist ein Mann 0 -10.284728271753428
NULL a man is tall groß ist ein Mann 0 -10.284728271753428

There are currently two contenders for the most likely translation. This is because the translation model is uncertain about the translation of "groß" which can be "tall" in the context of the height of humans, and "big" in most other settings. To avoid this uncertainty we can use a language model to capture the fact that "man is big" is a little less likely than "man is tall". We can reduce ambiguity by using a better language model

NULL _ _ _ _ 4 0.0
NULL a _ _ ein _ 3 -2.4849066497880004
NULL man groß _ _ _ 3 -Infinity
NULL a man _ _ ein Mann 2 -3.1780538303479458
NULL a tall _ ist ein _ 2 -Infinity
NULL a man is _ ist ein Mann 1 -4.564348191467836
NULL a man NULL _ ist ein Mann 1 -6.962243464266207
NULL a man is tall groß ist ein Mann 0 -5.2574953720277815
NULL a man is big groß ist ein Mann 0 -7.655390644826152

Note that "a man is tall" is also more likely in the Google N-grams corpus.

Summary

There are a few high level messages to take away from this chapter.

  • MT is an instance structured prediction recipe
  • The noisy channel is one modeling framework
  • word-based MT is foundation and blue print for more complex models
  • Training with EM
  • NLP Tricks:
    • introducing latent alignment variables to simplify problem
    • decoding with Beams

Background Material