



\section{
\label{factored-tutorial.tex}
Tutorial for Using Factored Models}


{\bf Note}: There may be some discrepancies between this description and the actual workings of the training script.







      - Train an unfactored model (Section \ref{factored-tutorial.texntoc1}) 

      - Train a model with POS tags (Section \ref{factored-tutorial.texntoc2}) 

      - Train a model with generation and translation steps (Section \ref{factored-tutorial.texntoc3}) 

      - Train a morphological analysis and generation model (Section \ref{factored-tutorial.texntoc4}) 

      - Train a model with multiple decoding paths (Section \ref{factored-tutorial.texntoc5})\\





To work through this tutorial, you first need to have the data in place. The instructions also assume that you have the training script and the decoder in you executable path.



You can obtain the data as follows:

\begin{compactitem}
\item {\tt wget {\sf http://www.statmt.org/moses/download/factored-corpus.tgz}}

\item {\tt tar xzf factored-corpus.tgz}

\end{compactitem}

For more information on the training script, check the documentation, which is linked to on the right navigation column under "Training".



\subsection{
\label{factored-tutorial.texntoc1}Train an unfactored model}


The corpus package contains language models and parallel corpora with POS and lemma factors. Before playing with factored models, let us start with training a traditional phrase-based model:






\begin{SaveVerbatim}{myverb}
% train-model.perl \
--root-dir unfactored \
--corpus factored-corpus/proj-syndicate \
--f de --e en \
--lm 0:3:factored-corpus/surface.lm \
--external-bin-dir .../tools \
--input-factor-max 4
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

This creates a phrase-based model in the directory {\tt unfactored/model} in about 20 minutes (on a 2.8GHZ machine). For a quicker training run that only takes a few minutes (with much worse results) use the just the first 1000 sentence pairs of the corpus, contained in {\tt factored-corpus/proj-syndicate.1000}.






\begin{SaveVerbatim}{myverb}
% train-model.perl \
--root-dir unfactored \
--corpus factored-corpus/proj-syndicate.1000 \
--f de --e en \
--lm 0:3:factored-corpus/surface.lm \
--external-bin-dir .../tools \
--input-factor-max 4
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

This creates a typical phrase-based model, as specified in the created configuration file {\tt moses.ini}. Here the part of the file that points to the phrase table:






\begin{SaveVerbatim}{myverb}
[feature]
PhraseDictionaryMemory ... path=/.../phrase-table.gz ...
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

You can take a look at the generated phrase table, which starts as usual with rubbish but then occasionally contains some nice entries. The scores ensure that during decoding the good entries are preferred. 






\begin{SaveVerbatim}{myverb}
! ||| ! ||| 1 1 1 1 2.718
" ( ||| " ( ||| 1 0.856401 1 0.779352 2.718
" ) , ein neuer film ||| " a new film ||| 1 0.0038467 1 0.128157 2.718
" ) , ein neuer film �ber ||| " a new film about ||| 1 0.000831718 1 0.0170876 2.71
[...]
frage ||| issue ||| 0.25 0.285714 0.25 0.166667 2.718
frage ||| question ||| 0.75 0.555556 0.75 0.416667 2.718
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

\subsection{
\label{factored-tutorial.texntoc2}Train a model with POS tags}


Take a look at the training data. Each word is not only represented by its surface form (as you would expect in raw text), but also with additional factors.






\begin{SaveVerbatim}{myverb}
% tail -n 1 factored-corpus/proj-syndicate.??
==> factored-corpus/proj-syndicate.de <==
korruption|korruption|nn|nn.fem.cas.sg floriert|florieren|vvfin|vvfin .|.|per|per

==> factored-corpus/proj-syndicate.en <==
corruption|corruption|nn flourishes|flourish|nns .|.|.
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The German factors are

\begin{compactitem}
\item surface form,

\item lemma,

\item part of speech, and

\item part of speech with additional morphological information.

\end{compactitem}

The English factors are

\begin{compactitem}
\item surface form,

\item lemma, and

\item part of speech.

\end{compactitem}

Let us start simple and build a translation model that adds only the target part-of-speech factor on the output side:






\begin{SaveVerbatim}{myverb}
% train-model.perl \
--root-dir pos \
--corpus factored-corpus/proj-syndicate.1000 \
--f de --e en \
--lm 0:3:factored-corpus/surface.lm \
--lm 2:3:factored-corpus/pos.lm \
--translation-factors 0-0,2 \
--external-bin-dir .../tools
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Here, we specify with {\tt -{}-translation-factors 0-0,2} that the input factor for the translation table is the (0) surface form, and the output factor is (0) surface form and (2) part of speech.






\begin{SaveVerbatim}{myverb}
[feature]
PhraseDictionaryMemory ... input-factor=0 output-factor=0,2
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The resulting phrase table looks very similar, but now also contains part-of-speech tags on the English side:






\begin{SaveVerbatim}{myverb}
! ||| !|. ||| 1 1 1 1 2.718
" ( ||| "|" (|( ||| 1 0.856401 1 0.779352 2.718
" ) , ein neuer film ||| "|" a|dt new|jj film|nn ||| 1 0.00403191 1 0.128157 2.718
" ) , ein neuer film �ber ||| "|" a|dt new|jj film|nn about|in ||| 1 0.000871765 1 0.0170876 2.718
[...]
frage ||| issue|nn ||| 0.25 0.285714 0.25 0.166667 2.718
frage ||| question|nn ||| 0.75 0.625 0.75 0.416667 2.718
\end{SaveVerbatim}
\begin{shaded}
\scriptsize
\UseVerbatim{myverb}
\end{shaded}

We also specified two language models. Besides the regular language model based on surface forms, we have a second language model that is trained on POS tags. In the configuration file this is indicated by two lines in the LM section:






\begin{SaveVerbatim}{myverb}
[feature]
KENLM name=LM0 ...
KENLM name=LM1 ...
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Also, two language model weights are specified:






\begin{SaveVerbatim}{myverb}
[weight]
LM0= 0.5
LM1= 0.5
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The part-of-speech language model includes preferences such as that determiner-adjective is likely followed by a noun, and less likely by a determiner:






\begin{SaveVerbatim}{myverb}
-0.192859       dt jj nn
-2.952967       dt jj dt
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

This model can be used just like normal phrase based models:






\begin{SaveVerbatim}{myverb}
% echo 'putin beschreibt menschen .' > in
% moses -f pos/model/moses.ini < in
[...]
BEST TRANSLATION: putin|nnp describes|vbz people|nns .|. [1111]  [total=-6.049]
<<0.000, -4.000, 0.000, -29.403, -11.731, -0.589, -1.303, -0.379, -0.556, 4.000>>
[...]
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

During the decoding process, not only words ({\tt putin}), but also part-of-speech are generated ({\tt nnp}).



Let's take a look what happens, if we input a German sentence that starts with the object:






\begin{SaveVerbatim}{myverb}
% echo 'menschen beschreibt putin .' > in
% moses -f pos/model/moses.ini < in
BEST TRANSLATION: people|nns describes|vbz putin|nnp .|. [1111]  [total=-8.030]
<<0.000, -4.000, 0.000, -31.289, -17.770, -0.589, -1.303, -0.379, -0.556, 4.000>>
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Now, this is not a very good translation. The model's aversion to do reordering trumps our ability to come up with a good translation. If we downweight the reordering model, we get a better translation:






\begin{SaveVerbatim}{myverb}
% moses -f pos/model/moses.ini < in -d 0.2
BEST TRANSLATION: putin|nnp describes|vbz people|nns .|. [1111]  [total=-7.649]
<<-8.000, -4.000, 0.000, -29.403, -11.731, -0.589, -1.303, -0.379, -0.556, 4.000>>
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Note that this better translation is mostly driven by the part-of-speech language model, which prefers the sequence {\tt nnp vbz nns .} (-11.731) over the sequence {\tt nns vbz nnp .} (-17.770). The surface form language model only shows a slight preference (-29.403 vs. -31.289). This is because these words have not been seen next to each other before, so the language model has very little to work with. The part-of-speech language model is aware of the count of the nouns involved and prefers a singular noun before a singular verb ({\tt nnp vbz}) over a plural noun before a singluar verb ({\tt nns vbz}).



To drive this point home, the unfactored model is not able to find the right translation, even with downweighted reordering model:






\begin{SaveVerbatim}{myverb}
% moses -f unfactored/model/moses.ini < in -d 0.2
people describes putin . [1111]  [total=-11.410]
<<0.000, -4.000, 0.000, -31.289, -0.589, -1.303, -0.379, -0.556, 4.000>>
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

\subsection{
\label{factored-tutorial.texntoc3}Train a model with generation and translation steps}


Let us now train a slightly different factored model with the same factors. Instead of mapping from the German input surface form directly to the English output surface form and part of speech, we now break this up into two mapping steps, one translation step that maps surface forms to surface forms, and a second step that generates the part of speech from the surface form on the output side:






\begin{SaveVerbatim}{myverb}
% train-model.perl \
--root-dir pos-decomposed \
--corpus factored-corpus/proj-syndicate.1000 \
--f de --e en \
--lm 0:3:factored-corpus/surface.lm \
--lm 2:3:factored-corpus/pos.lm \
--translation-factors 0-0 \
--generation-factors 0-2 \
--decoding-steps t0,g0 \
--external-bin-dir .../tools
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Now, the translation step is specified only between surface forms ({\tt -{}-translation-factors 0-0}) and a generation step is specified ({\tt -{}-generation-factors 0-2}), mapping (0) surface form to (2) part of speech. We also need to specified in which order the mapping steps are applied ({\tt -{}-decoding-steps t0,g0}).



Besides the phrase table that has the same format as the unfactored phrase table, we now also have a generation table. It is referenced in the configuration file:






\begin{SaveVerbatim}{myverb}
[feature]
Generation ... input-factor=0 output-factor=2

[weight]
GenerationModel0= 0.3 0
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Let us take a look at the generation table:






\begin{SaveVerbatim}{myverb}
% more pos-decomposed/model/generation.0-2
nigerian nnp 1.0000000  0.0008163
proven vbn 1.0000000  0.0021142
issue nn 1.0000000  0.0021591
[...]
control vb 0.1666667  0.0014451
control nn 0.8333333  0.0017992
[...]
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The beginning is not very interesting. As most words, {\tt nigerian}, {\tt proven}, and {\tt issue} occur only with one part of speech, e.g., p({\tt nnp}$|${\tt nigerian}) = 1.0000000. Some words, however, such as {\tt control} occur with multiple part of speech, such as base form verb ({\tt vb}) and single noun ({\tt nn}).



The table also contains the reverse translation probability p({\tt nigerian}$|${\tt nnp}) = 0.0008163. In our example, this may not be a very useful feature. It basically hurts open class words, especially unusual ones. If we do not want this feature, we can also train the generation model as single-featured by the switch {\tt -{}-generation-type single}.



\subsection{
\label{factored-tutorial.texntoc4}Train a morphological analysis and generation model}


Translating surface forms seems to be a somewhat questionable pursuit. It does not seem to make much sense to treat different word forms of the same lemma, such as {\tt mensch} and {\tt menschen} differently. In the worst case, we will have seen only one of the word forms, so we are not able to translate the other. This is what in fact happens in this example:






\begin{SaveVerbatim}{myverb}
% echo 'ein mensch beschreibt putin .' > in
% moses.1430.srilm -f unfactored/model/moses.ini < in
a mensch|UNK|UNK|UNK describes putin . [11111]  [total=-158.818] 
<<0.000, -5.000, -100.000, -127.565, -1.350, -1.871, -0.301, -0.652, 4.000>>
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Factored translation models allow us to create models that do morphological analysis and decomposition during the translation process. Let us now train such a model:






\begin{SaveVerbatim}{myverb}
% train-model.perl \
--root-dir morphgen \
--corpus factored-corpus/proj-syndicate.1000 \
--f de --e en \
--lm 0:3:factored-corpus/surface.lm \
--lm 2:3:factored-corpus/pos.lm \
--translation-factors 1-1+3-2 \
--generation-factors 1-2+1,2-0 \
--decoding-steps t0,g0,t1,g1 \
--external-bin-dir .../tools
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

We have a total of four mapping steps:

\begin{compactitem}
\item a translation step that maps lemmas ({\tt 1-1}),

\item a generation step that sets possible part-of-speech tags for a lemma ({\tt 1-2}),

\item a translation step that maps morphological information to part-of-speech tags ({\tt 3-2}), and

\item a generation step that maps part-of-speech tag and lemma to a surface form ({\tt 1,2-0}).

\end{compactitem}

This enables us now to translate the sentence above:






\begin{SaveVerbatim}{myverb}
% echo 'ein|ein|art|art.indef.z mensch|mensch|nn|nn.masc.nom.sg \
beschreibte|beschreiben|vvfin|vvfin putin|putin|nn|nn.masc.cas.sg \
.|.|per|per' > in
% moses -f morphgen/model/moses.ini < in
BEST TRANSLATION: a|a|dt individual|individual|nn describes|describe|vbz \
putin|putin|nnp .|.|. [11111]  [total=-17.269] 
<<0.000, -5.000, 0.000, -38.631, -13.357, -2.773, -21.024, 0.000, -1.386, \
-1.796, -4.341, -3.189, -4.630, 4.999, -13.478, -14.079, -4.911, -5.774, 4.999>>
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Note that this is only possible, because we have seen an appropriate word form in the output language. The word {\tt individual} occurs as single noun in the parallel corpus, as translation of {\tt einzelnen}. To overcome this limitation, we may train generation models on large monolingual corpora, where we expect to see all possible word forms.



\subsection{
\label{factored-tutorial.texntoc5}Train a model with multiple decoding paths}


Decomposing translation into a process of morphological analysis and generation will make our translation model more robust. However, if we have seen a phrase of surface forms before, it may be better to take advantage of such rich evidence. 



The above model poorly translates sentences, as it does use the source surface form at all, relying on translating the properties of the surface forms.



In practice, we fair better when we allow both ways to translate in parallel. Such a model is trained by the introduction of decoding paths. In our example, one decoding path is the morphological analysis and generation as above, the other path the direct mapping of surface forms to surface forms (and part-of-speech tags, since we are using a part-of-speech tag language model):






\begin{SaveVerbatim}{myverb}
% train-model.perl \
--corpus factored-corpus/proj-syndicate.1000 \
--root-dir morphgen-backoff \
--f de --e en \
--lm 0:3:factored-corpus/surface.lm \
--lm 2:3:factored-corpus/pos.lm \
--translation-factors 1-1+3-2+0-0,2 \
--generation-factors 1-2+1,2-0 \
--decoding-steps t0,g0,t1,g1:t2 \
--external-bin-dir .../tools
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

This command is almost identical to the previous training run, except for the additional translation table {\tt 0-0,2} and its inclusion as a different decoding path {\tt :t2}.



A strategy for translating surface forms which have not been seen in the training corpus is to translate its lemma instead. This is especially useful for translation from morphologically rich languages to simpler languages, such as German to English translation.






\begin{SaveVerbatim}{myverb}
% train-model.perl \
--corpus factored-corpus/proj-syndicate.1000 \
--root-dir lemma-backoff \
--f de --e en \
--lm 0:3:factored-corpus/surface.lm \
--lm 2:3:factored-corpus/pos.lm \
--translation-factors 0-0,2+1-0,2 \
--decoding-steps t0:t1 \
--external-bin-dir .../tools
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}






\begin{center}\textit{\small Subsection last  modified on May 29, 2016, at 10:02 PM}\end{center}
