Skip to content

Implementing Statistical Machine Translation Using MapReduce

by Michael Nielsen on March 23, 2009

This post is part of my series on the Google Technology Stack. It’s something of a digression, and is optional (but fun, and worth reading!) We’ll return to the main line of development in the next post.

This post is a followup to an earlier post about the beautiful subject of statistical machine translation. In the earlier post I described how to build a statistical model that can be used to automatically translate texts from one language to another. The ideas are beautiful, but require a lot of computing power – the more computing power, the better the translation. In this post I describe a way of implementing statistical machine translation in parallel on a large cluster of machines. The post assumes: (1) that you’re comfortable with my previous post on statistical machine translation; and (2) that you’re comfortable with the MapReduce framework for distributed computing.

Before going any further, a warning: I haven’t implemented the system described here. I’ve no idea if Google does machine translation with a MapReduce implementiation, although Google translate definitely uses statistical machine translation. It’s possible there’s a bottleneck in my implementation that I’m not noticing. Ideally, we should carefully analyse the resources used in my proposed implementation, and, if that reveals no problems, build a working system, and test to see how well it scales in practice. I haven’t done any of that. Still, putting statistical machine translation into the MapReduce framework is a great way of deepening our understanding of both sets of ideas, and I’m pretty confident that many of these ideas could be used in a production machine translation system.

With that said, it should be noted that the simple statistical machine translation model described in the previous post can actually be implemented on a single machine. However, as discussed in the earlier post, it’s easy to tweak that model so it performs far better, at the cost of making it too complex to run on a single machine. The ideas described in this post are easily adapted to many of those tweaked models of machine translation.

One final point before we jump into the meat of the post. Earlier posts in this series have been pretty detailed. In this post I’ve glossed over some details so as not to obscure the forest with too many tress – there’s only so many Map and MapReduce jobs you can read about before your eyes start to glaze over. But the missing details should be easily filled in by the reader, and I’ve tried to indicate where there are details which need to be filled in.

Overview: what our system needs to do

Recall the basic elements of statistical machine translation. It’s split into two main phases. The first phase is the training phase, in which a statistical model of translation is built, using a corpus of texts in both the source and target language (French and English, for the sake of concreteness). The training phase is itself split into three parts: (i) document collection, where we assemble the corpus of texts from which the statistical model will be inferred; (ii) building the language model for the target language; and (iii) building the translation model from the target language to the source language (yes, that’s the right way round, see the earlier post!) The second phase is the translation phase, which uses a heuristic search procedure to find a good translation of a text. It’s this phase which is actually used directly by the end user – the training phase all happens offline, beforehand.

Altogether, that makes four separate procedures we need to implement. We’ll go through them one by one in the remainder of this post. Before doing that, though, there is one idea implicit in my MapReduce post that needs to be made more explicit. This is the idea of a distributed hash of key-value pairs. Given a set of keys and corresponding values (k_1:v_1),(k_2:v_2),\ldots, a crucial part of MapReduce is distributing the key-value pairs evenly over all computers in the cluster.

Here’s a simple way this may be achieved. Suppose there are n computers in the cluster. Number them 0,\ldots,n-1. Let \#(\cdot) be a hash function. An unfortunate fact is that this use of the term “hash” is completely different than the useage of “hash” to denote a set of key-value pairs. In practice, this isn’t as confusing as you might think, but you should note the difference. In any case, the way we set up our distributed hash is to store the key-value pair (k:v) on computer number \#(k) \mod n. Because the hash function distributes key values in a uniform way, the result is to spread the key-value pairs through the cluster in a uniform way.

In a practical implementation, this approach to storing a distributed hash has some drawbacks, which we’ll come back to in later posts about the Google File System and BigTable. But for now, you can think of a distributed hash in this way.

Document collection

There are many possible ways we could be presented with the documents we’ll use to build our statistical model of translation. To make things concrete, and to illustrate the general ideas used to manage the distribution of documents across the cluster, let’s imagine we’re presented with them as a file containing several million URLs, with each URL pointing to an English language text which we’ll use to build a language model. Alternately, the file might contain URLs pointing to pairs of translated texts, in English and in French, which we’ll use to build the translation model. The same basic ideas apply in both cases, so to be specific, I’ll suppose the URLs point to English-only texts.

We use the URLs as keys in a distributed hash. Assuming that they start on a single machine, we first need to distribute the keys across the cluster, and then initialize the corresponding values to empty. The result is a distributed hash with key-value pairs (URL:empty). We now use that distributed hash as input to a Map job, which applies a mapper to (URL:empty), downloading the webpage for the URL, and storing the resulting document as the value corresponding to the key. The resulting distributed hash has key-value pairs (URL:document). We’ll use that distributed hash in constructing the language model, as described below.

Language model

Recall from the previous post that in the bigram model we compute the probability of a sentence as:

   \mbox{pr}(e_1,\ldots,e_m) = \mbox{pr}(e_m|e_{m-1})   \mbox{pr}(e_{m-1}|e_{m-2}) \ldots \mbox{pr}(e_2|e_1)   \mbox{pr}(e_1).

To compute this probability, we need estimates for the single-xword probabilities \mbox{pr}(e_1), and for the bigram conditional probabilities, \mbox{pr}(e_2|e_1).

To estimate the single-word probabilities \mbox{pr}(e) we use two MapReduce jobs. The first MapReduce job counts the total number of words N in all the documents. The input to this job is the hash with entries (URL:document). The mapper emits a single intermediate pair, (0:#(document)), where the intermediate key 0 is simply an arbitrarily chosen constant, and #(document) is the total number of words in the document. The reducer simply sums over all these intermediate values, giving N as output.

The second MapReduce job is a simple variant on the wordcount example used in the earlier post. It takes as input the hash with entries (URL:document). The mapper parses the document, and each time it comes to a word e emits the intermediate key-value pair (e:1), indicating one occurrence of e. The reducers sum over all those counts of 1, and divide by the total number of words N, giving as output a hash with entries (e:\mbox{pr}(e)), where \mbox{pr}(e) is the relative frequency of e in the document collection.

To estimate \mbox{pr}(e_2|e_1) we also use two MapReduce jobs. The first MapReduce job compues the total number of bigrams, N'. Once again, the input to this job is the hash with entries (URL:document). The mapper emits a single intermediate pair, (0:#'(document)), where the intermediate key 0 is again an arbitrary constant, and #'(document) is the total number of bigrams in the document. The reducer sums over all these values, giving N' as output.

The second MapReduce job computes the frequency of each bigram, and divides by N' to give \mbox{pr}(e_2|e_1). Again, the input to this job is the hash with entries (URL:document). The mapper emits an intermediate key-value pair ((e_2|e_1):1) for each bigram it finds in the document, possibly with some repetitions. Note that the intermediate keys are of the form (e_2|e_1), simply for consistency with our notation elswhere. The reducer sums over the intermediate values and divides by N' to get the relative frequency of occurrence, so the output hash has entries ((e_e|e_1):\mbox{pr}(e_2|e_1)), as was desired.

At this point we’ve constructed distributed hashes with key-value pairs (e:\mbox{pr}(e)) and ((e_2|e_1):\mbox{pr}(e_2|e_1)), effectively encoding our language model. In particular, access to these distributed hashes makes it is easy to compute \mbox{pr}(E) for an entire sentence E.

Exercises

  • In the earlier post about statistical machine translation I mentioned that problems can arise in estimating bigram probabilities, because bigrams not in the corpus have their probability systematically underestimated. How might you address this problem on a distributed cluster?

Translation model

Recall from the previous post that our translation model was determined by the following sets of parameters:

  • The fertility probability \mbox{pr}(n|e), the probability that the English word e has fertility n.
  • The distortion probability \mbox{pr}(t|s,l), which is the probability that an English word at position s corresponds to a French word at position t in a French sentence of length l.
  • The translation probability \mbox{pr}(f|e), one for each French word f and English word e.

To determine these parameters we simply start with guesses, and then use an iterative procedure to gradually get better estimates, stopping when the iterative procedure converges on a relatively stable set of parameters.

To implement this strategy, we start by setting up three hashes to store estimates of the parameters. Let’s start with the fertility probabilities. As our starting guess, we’ll assume:

   \mbox{pr}(n|e) \approx \frac{1}{2^{n+1}}.

I’ve put the approximation sign in because we’ll actually truncate the distribution at some high value for the fertility, say n = 10, so the hash is of a manageable size. Obviously, these probabilities should then be modified slightly, to be properly normalized, but that’s a detail I won’t worrry about.

We’ll set up a hash with keys (n|e) to store our initial guesses for the fertility probabilities. This can be done in many ways. One way is to use a single MapReduce job which takes as input the hash storing the corpus of English-French texts and produces as output a hash with key-value pairs ((0|e):1/2),((1|e):1/4),((2|e):1/8),\ldots, where e ranges over the set of English words in our corpus.

At this point, you’ve probably noticed that I’m assuming you’re getting comfortable enough with Map and MapReduce that I can just describing what the jobs do, and leave it to you to fill in the details of how they work. This will continue.

Next, we set up a distributed hash for the distortion probabilities \mbox{pr}(t|s,l). To do this, we first set up an empty hash (i.e., one with empty values) with keys (t|s,l), where t \leq l \leq 40, and s \leq 50, say. As before, these truncations are rather arbitrary, but their purpose is to limit the size of the hash. Now run a Map job with input the empty hash, ((t|s,l):\mbox{empty}), and where the mapper simply outputs the value 1/l, giving us a hash ((t|s,l):1/l), i.e., a uniform distribution for target positions.

In the last paragraph you might wonder why we don’t start by initializing the values in the hash as 1/l, rather than using a Map job to do it. The model I have in mind is that the setup of the initial empty hash is co-ordinated from a single centralized server, with the values automatically being set to a default of empty. We could instead set the values on the centralized server as well, before distribution, but the problem is that this might cause the central server to become a bottleneck. That probably won’t happen here – computing and distributing 1/l is pretty easy – but it could easily happen if the computation or values were more complex. As a matter of general principle, it’s better to compute the values using a Map job which can be distributed across the cluster.

The third and final distributed hash we need to set up has keys (f|e) and values the translation probabilities \mbox{pr}(f|e). As our initial guess for these probabilities, we’ll use our corpus of starting documents, and set \mbox{pr}(f|e) to be the fraction of sentences with f in them, given that e occurs. We do this using two MapReduce jobs. The first MapReduce job constructs a hash with entries (e: \mbox{number of sentences with } e \mbox{ in them}), over all words e in the corpus. We use this hash and a second MapReduce job to construct a hash with keys (f|e) and values the fraction of sentences with f in them, given that e is in the starting text.

We’ve now set up the three hashes describing our starting guess for the parameters of our translation model. Next, we need to understand how to iteratively update those guesses, hopefully improving their quality.

The first step in the iterative update is to use Map to process our translation corpus, producing as output a hash with keys (E,F), where E and F are corresponding English and French sentences in the corpus. To compute the corresponding value, we consider all possible alignments between the sentence pair, and compute probabilities for those alignments, using our initial estimate of the parameters above. The alignment a with the highest probability is the corresponding value, so we are producing a hash with pairs ((E,F):a).

The second step in the iterative update is to use the sentence-pair hash ((E,F):a) and three MapReduce jobs to compute the updated estimates for the fertility, distortion and translation probabilities, \mbox{pr}(n|e), \mbox{pr}(t|s,l) and \mbox{pr}(f|e). This completes the iterative update.

We now repeat this iterative update process many times. I’ll leave it as an exercise to figure out how you could use MapReduce to determine how much the parameters of the model are changing, per iteration, and what would be a reasonable criterion for terminating the iterations.

The translation step: heuristic search

The translation step is the final stage. It’s the stage at which we take our translation model, and apply it to translate a French document into English, sentence by sentence. Recall from the previous post that to translate the French sentence F we search for the English sentence E and alignment a which maximizes the product

   \mbox{pr}(F,a|E) \mbox{pr}(E).

The idea of the heuristic search is to consider partial sentences and partial alignments, maintaining a stack of particularly promising candidates. We’ll set the stack size to be S, let’s say S = 10^6, so we’ll never consider more than a million possible candidate alignments and sentences.

The procedure involves three steps: (1) Construct all possible single-word extensions of existing partial sentences and partial alignments, and compute the corresponding product \mbox{pr}(F,a|E) \mbox{pr}(E); (2) Choose the S most likely of those, and discard the rest; and (3) Terminate when we have a complete alignment which is substantially more likely than any of the remaining partial alignments, otherwise repeat steps (1) and (2).

Step (1) can easily be done using a Map job. Step (2) can be done using multiple MapReduce steps. This is essentially a binary search to find the top S values for the product \mbox{pr}(F,a|E) \mbox{pr}(E). Step (3) can be done using the same ideas as step (2).

About this post: This is one in a series of posts about the Google Technology Stack – PageRank, MapReduce, and so on. The posts are summarized here, and there is FriendFeed room for the series here. Subscribe to my blog to follow future posts in the series.