



\section{
\label{advanced-incremental.tex}
Incremental Training}


{\bf NB: This page requires refactoring}



\subsection{
Contents}






      - Introduction (Section \ref{advanced-incremental.texntoc1}) 

      - Initial Training (Section \ref{advanced-incremental.texntoc2}) 

      - Virtual Phrase Tables Based on Sampling Word-aligned Bitexts (Section \ref{advanced-incremental.texntoc3}) 

      - Construction (Section \ref{advanced-incremental.texntoc4}) 

      - Use within EMS (Section \ref{advanced-incremental.texntoc5}) 

      - How to use memory-mapped dynamic suffix array phrase tables in the moses decoder  (Section \ref{advanced-incremental.texntoc6}) 

      - Updates (Section \ref{advanced-incremental.texntoc7}) 

      - Preprocess New Data (Section \ref{advanced-incremental.texntoc8}) 

      - Prepare New Data (Section \ref{advanced-incremental.texntoc9}) 

      - Update and Compute Alignments (Section \ref{advanced-incremental.texntoc10}) 

      - Phrase Table Features for PhraseDictionaryBitextSampling (Section \ref{advanced-incremental.texntoc11}) 

      - Types of counts (Section \ref{advanced-incremental.texntoc12}) 

      - List of phrase table entry features (Section \ref{advanced-incremental.texntoc13}) 

      - Specification of forward/backward phrase-level conditional probabilities (Section \ref{advanced-incremental.texntoc14}) 

      - Specification of log count features (Section \ref{advanced-incremental.texntoc15}) 

      - Seeding the dynamic forground corpus (Section \ref{advanced-incremental.texntoc16}) 

      - Checking the active feature set (Section \ref{advanced-incremental.texntoc17}) 

      - Suffix Arrays for Hierarchical Models (Section \ref{advanced-incremental.texntoc18}) 

      - Using the EMS (Section \ref{advanced-incremental.texntoc19})\\





\subsection{
\label{advanced-incremental.texntoc1}Introduction}


Translation models for Moses are typically batch trained. That is, before training you have all the data you wish to use, you compute the alignments using GIZA, and from that produce a phrase table which you can use in the decoder. If some time later you wish to utilize some new training data, you must repeat the process from the start, and for large data sets, that can take quite some time.



Incremental training provides a way of avoiding having to retrain the model from scratch every time you wish to use some new training data. Instead of producing a phrase table with precalculated scores for all translations, the entire source and target corpora are stored in memory as a suffix array along with their alignments, and translation scores are calculated on the fly. Now, when you have new data, you simply update the word alignments, and append the new sentences to the corpora along with their alignments. Moses provides a means of doing this via
XML RPC, so you don't even need to restart the decoder to use the new data.



Note that at the moment the incremental phrase table code is not thread safe.



\subsection{
\label{advanced-incremental.texntoc2}Initial Training}


This section describes how to initially train and use a model which
support incremental training.






\begin{compactitem}
\item Setup the EMS as normal, but use a modified version of GIZA++\footnote{\sf http://code.google.com/p/inc-giza-pp/}.

\item Add the line:

\end{compactitem}




\begin{SaveVerbatim}{myverb}
training-options = "-final-alignment-model hmm"
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}






to the TRAINING section of your experiment configuration file.






\begin{compactitem}
\item Train the system using the initial training data as normal.

\end{compactitem}

\subsection{
\label{advanced-incremental.texntoc3}\label{advanced-incremental.texmmsapt}Virtual Phrase Tables Based on Sampling Word-aligned Bitexts}


{\em phrase-based decoding only!}



\subsubsection*{
\label{advanced-incremental.texntoc4}Construction}


1. Compile Moses with the bjam switch -{}-with-mm



2. You need 

\begin{SaveVerbatim}{myverb}
- sentences aligned text files
- the word alignment between these files in symal output format
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

3. Build binary files






\begin{SaveVerbatim}{myverb}
Let 
${L1} be the extension of the language that you are translating from,
${L2} the extension of the language that you want to translate into, and 
${CORPUS} the name of the word-aligned training corpus

% zcat ${CORPUS}.${L1}.gz  | mtt-build -i -o /some/path/${CORPUS}.${L1}
% zcat ${CORPUS}.${L2}.gz  | mtt-build -i -o /some/path/${CORPUS}.${L2}
% zcat ${CORPUS}.${L1}-${L2}.symal.gz | symal2mam /some/path/${CORPUS}.${L1}-${L2}.mam
% mmlex-build /some/path/${CORPUS} ${L1} ${L2} -o /some/path/${CORPUS}.${L1}-${L2}.lex 
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

4. Define line in moses.ini (all needs to be on one line, the continuation marks are for typesetting in the pdf of this manual only):



{\bf for static systems:}

\begin{SaveVerbatim}{myverb}
PhraseDictionaryBitextSampling name=PT0 output-factor=0 \
path=/some/path/${CORPUS} L1=${L1} L2=${L2} 
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

{\bf for post-editing, e.g.:}

\begin{SaveVerbatim}{myverb}
PhraseDictionaryBitextSampling name=PT0 output-factor=0 \
path=/some/path/${CORPUS} L1=${L1} L2=${L2} smooth=0 prov=1
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

({\bf Note:} the best configuration of phrase table features is still under investigation.)



Phrase table features are explained below



\subsubsection*{
\label{advanced-incremental.texntoc5}Use within EMS}


Add the following lines to your config file to use the sampling phrase table within experiment.perl:






\begin{SaveVerbatim}{myverb}
### build memory mapped suffix array phrase table
#
mmsapt = "pfwd=g pbwd=g smooth=0.01 rare=0 prov=0 sample=1000 workers=1"
binarize-all = $moses-script-dir/training/binarize-model.perl
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

OR (for use with interactive post-editing)






\begin{SaveVerbatim}{myverb}
### build memory mapped suffix array phrase table
#
mmsapt = "pfwd=g pbwd=g smooth=0 rare=1 prov=1 sample=1000 workers=1"
binarize-all = $moses-script-dir/training/binarize-model.perl
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

DEPRECATED:



<del> * Modify the moses.ini file found in {\tt $<$experiment-dir$>$ /evaluation/filtered.$<$evaluation-set$>$.$<$run-number$>$} to have a ttable-file entry as follows:</del>



<del>PhraseDictionaryDynSuffixArray source=$<$path-to-source-corpus$>$ target=$<$path-to-target-corpus$>$ alignment=$<$path-to-alignments$>$</del>



<del>The source and target corpus paths should be to the tokenized, cleaned, and truecased versions found in {\tt $<$experiment-dir$>$}{\tt /training}{\tt /corpus.$<$run$>$.$<$lang$>$}, and the alignment path should be to {\tt $<$experiment-dir$>$}{\tt /model/aligned.$<$run$>$.grow-diag-final-and}.</del>






\subsubsection*{
\label{advanced-incremental.texntoc6}How to use memory-mapped dynamic suffix array phrase tables in the moses decoder }


(phrase-based decoding only)
See Section Phrase Table Features for PhraseDictionaryBitextSampling (Section \ref{advanced-incremental.texPhraseDictionaryBitextSampling}) below.



\subsection{
\label{advanced-incremental.texntoc7}Updates}


\paragraph*{
\label{advanced-incremental.texntoc8}Preprocess New Data}



First, tokenise, clean, and truecase both target and source sentences (in that order) in the same manner as for the original corpus. You can see how this was done by looking at the {\tt $<$experiment-dir$>$/steps/$<$run$>$/CORPUS\_$\{$tokenize,clean,truecase$\}$.$<$run$>$} scripts.



\paragraph*{
\label{advanced-incremental.texntoc9}Prepare New Data}



The preprocessed data now needs to be prepared for use by GIZA. This involves updating the vocab files for the corpus, converting the sentences into GIZA's {\tt snt} format, and updating the cooccurrence file.



{\tt plain2snt}






\begin{SaveVerbatim}{myverb}
$ $INC_GIZA_PP/GIZA++-v2/plain2snt.out <new-source-sentences> <new-target-sentences> \
-txt1-vocab <previous-source-vocab> -txt2-vocab <previous-target-vocab>
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}






The previous vocabulary files for the original corpus can be found in {\tt $<$experiment-dir$>$/}{\tt training}{\tt /prepared.$<$run$>$/{$<$source-lang$>$,$<$target-lang$>$}.vcb}. Running this command with the files containing your new tokenized, cleaned, and truecased source and target as {\tt txt1} and {\tt txt2} will produce new a new vocab file for each language and a couple of {\tt .snt} files. Any further references to vocabs in commands or config files should reference the new vocabulary files just produced.








Note: if this command fails with the error message {\tt plain2snt.cpp:28: int loadVocab(): Assertion `iid1.size()-1 == ID' failed.}, then change line 15 in {\tt plain2snt.cpp} to {\tt vector}{\tt $<$string$>$ iid1(1),iid2(1);} and recompile.



{\tt snt2cooc}






\begin{SaveVerbatim}{myverb}
$ $INC_GIZA_PP/bin/snt2cooc.out <new-source-vcb> <new-target-vcb> <new-source_target.snt> \
<previous-source-target.cooc > new.source-target.cooc
$ $INC_GIZA_PP/bin/snt2cooc.out <new-target-vcb> <new-source-vcb> <new-target_source.snt> \
<previous-target-source.cooc > new.target-source.cooc
\end{SaveVerbatim}
\begin{shaded}
\scriptsize
\UseVerbatim{myverb}
\end{shaded}






This commands is run once in the source-target direction, and once in the target-source direction. The previous cooccurrence files can be found in {\tt $<$experiment-dir$>$/}{\tt training/}{\tt giza.$<$run$>$/}{\tt $<$target-lang$>$-$<$source-lang$>$.cooc} and {\tt $<$experiment-dir$>$/}{\tt training/}{\tt giza-inverse.$<$run$>$/}{\tt $<$source-lang$>$-$<$target-lang$>$.cooc}.



\paragraph*{
\label{advanced-incremental.texntoc10}Update and Compute Alignments}



GIZA++ can now be run to update and compute the alignments for the new data. This should be run in the source to target, and target to source directions. A sample GIZA++ config file is given below for the source to target direction; for the target to source direction, simply swap mentions of target and source.






\begin{SaveVerbatim}{myverb}
S: <path-to-src-vocab>
T: <path-to-tgt-vocab>
C: <path-to-src-to-tgt-snt>
O: <prefix-of-output-files>
coocurrencefile: <path-to-src-tgt-cooc-file>
model1iterations: 1
model1dumpfrequency: 1
hmmiterations: 1
hmmdumpfrequency: 1
model2iterations: 0
model3iterations: 0
model4iterations: 0
model5iterations: 0
emAlignmentDependencies: 1
step_k: 1
oldTrPrbs: <path-to-original-thmm> 
oldAlPrbs: <path-to-original-hhmm>
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

To run GIZA++ with these config files, just issue the command






\begin{SaveVerbatim}{myverb}
GIZA++ <path-to-config-file>
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

With the alignments updated, we can get the alignments for the new data by running the command:






\begin{SaveVerbatim}{myverb}
giza2bal.pl -d <path-to-updated-tgt-to-src-ahmm> -i <path-to-updated-src-to-tgt-ahmm> \
| symal -alignment="grow" -diagonal="yes" -final="yes" -both="yes" > new-alignment-file
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}




\begin{compactitem}
\item Update Model

\end{compactitem}






Now that alignments have been computed for the new sentences, you can use them in the decoder. Updating a running Moses instance is done via XML RPC, however to make the changes permanent, you must append the tokenized, cleaned, and truecased source and target sentences to the original corpora, and the new alignments to the alignment file.



\subsection{
\label{advanced-incremental.texntoc11}\label{advanced-incremental.texPhraseDictionaryBitextSampling}Phrase Table Features for PhraseDictionaryBitextSampling}


{\bf This is still work in progress. Feature sets and names may change at any time without notice. It is best not to rely on defaults but to always specify for each feature explicitly whether or not it is to be used.}



Some of the features below are described in the following publication: Ulrich Germann. 20014. "Dynamic Phrase Tables for Statistical Machine Translation in an Interactive Post-editing Scenario". {\em AMTA 2014 Workshop on Interactive and Adaptive Machine Translation}. Vancouver, BC, Canada.



\subsubsection*{
\label{advanced-incremental.texntoc12}Types of counts}


The sampling phrase table offers a number of fixed and configurable phrase table features. For the descriptions below, it is necessary to distinguish different kinds of counts.






\begin{compactitem}
\item {\bf raw} [r] counts: raw monolingual phrase occurrence counts

\item {\bf sample size} [s]: number of samples considered

\item {\bf good} [g]: number of samples with a coherent translations (i.e., at least one target phrase could be extracted)

\item {\bf joint} [j]: joint phrase occurrences

\end{compactitem}

\subsubsection*{
\label{advanced-incremental.texntoc13}List of phrase table entry features}


Phrase pair features are specified as follows:






\begin{compactitem}
\item lexical forward and backward probabilities (currently always included)

\item {\tt pfwd=}{\em spec} log of lower bound on forward phrase-level conditional probability; details below

\item {\tt pbwd=}{\em spec} log of lower bound on backward phrase-level conditional probability; details below

\item {\tt logcnt=}{\em spec} logs of plain counts

\item {\tt coh=}$\{$1$|$0$\}$ log of coherence (include / don't include)

\item {\tt rare=}{\em param}: global rarity penalty {\em param}/({\em j + param}), where {\em param} determines the steepness of the asymptotic penalty, which slowly decreases towards zero as the number of joint phrase occurrences {\em j} increases.

\item {\tt prov=}{\em param}: foreground/background-specific provenance reward {\em j}/({\em j + param}) that asymtotically grows to 1 for the specific corpus as the number of joint phrase occurrences {\em j} increases.

\item {\tt unal=}{\em spec}: number of unaligned words in the phrase pair; detailed documentation pending.

\item {\tt pcnt=}{\em spec}: phrase penalty ??? (introduced by M. Denkowski)

\item {\tt wcnt=}{\em spec}: word penalty ??? (introduced by M. Denkowski)

\item {\tt lenrat=}$\{$1$|$0$\}$: use / don't use the phrase length ratio feature described here\footnote{\sf https://ufal.mff.cuni.cz/pbml/104/art-germann.pdf}

\end{compactitem}

\paragraph*{
\label{advanced-incremental.texntoc14}Specification of forward/backward phrase-level conditional probabilities}


The specficication for {\tt pfwd} and {\tt pbwd} consists of one or more of the letters 'r','s', and 'g' plus optionally the '+' sign. The letter (r/s/g) determines the denominator (see types of counts above); the plus sign indicates that these features are to be computed separately for (static) background corpus and (dynamic) foreground corpus. For example, {\tt pfwd=g+} will compute the lower bound on the probability given {\em j} joint occurrences of the phrase pair in question in {\em g} samples, computed separately for the two corpora. The confidence level for the lower bound is specified by the parameter {\tt smooth=}{\em value}, where {\em value} is a value between 0 and 1 indicating the risk of overestimating the true probability given the evidence that we are willing to take. {\tt smooth=0} causes the maximum likelihood estimate to be used. 



\paragraph*{
\label{advanced-incremental.texntoc15}Specification of log count features}

\begin{compactitem}
\item {\tt r1} include raw counts for L1 phrase

\item {\tt r2} include raw counts for L2 phrase

\item {\tt s1} include sample size for L1 phrase

\item {\tt g1} include number of samples used ('good samples')

\item {\tt j} include joint phrase counts

\end{compactitem}

as with {\tt pfwd}/{\tt pbwd}, a '+' at the end indicates that the features are to be provided per corpus, not pooled. E.g., {\tt logcnt=g1jr2}: provide log of the number of samples actually used for phrase extraction, joint counts, and raw L2 phrase counts. 



\paragraph*{
\label{advanced-incremental.texntoc16}Seeding the dynamic forground corpus}


{\tt extra=}{\em path} allows you to specify a set of files {\em path.L1}, {\em path.L2}, and {\em path.}{\tt symal} to seed the dynamic foreground corpus with a word-aligned corpus in text format. {\em path.L1} and {\em path.L2} must be one sentence per line, cased as required for translation. {\em path.}{\tt symal} should contain the word alignment info in symal output format.



\subsubsection*{
\label{advanced-incremental.texntoc17}Checking the active feature set}


the program ptable-describe-features can be used to list the features used in the order they are provided by the phrase table:

\begin{SaveVerbatim}{myverb}
cat moses.ini | ptable-describe-features 
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

\subsection{
\label{advanced-incremental.texntoc18}Suffix Arrays for Hierarchical Models}


The phrase-based model uses a suffix array implementation which comes with Moses.



If you want to use suffix arrays for hierarchical models, use Adam Lopez's implementation. The source code for this is currently available in cdec\footnote{\sf https://github.com/redpony/cdec/}.     You have to compile cdec so please follow its instructions. 



You also need to install pycdec 

\begin{SaveVerbatim}{myverb}
cd python
python setup.py install 
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Note: the suffix array code requires Python 2.7 or above. If you have Linux installations which are a few years old, check this first.



Adam Lopez's implementation writes the suffix array to binary files, given the parallel training data and word alignment. The Moses toolkit has a wrapper script which simplifies this process:

\begin{SaveVerbatim}{myverb}
./scripts/training/wrappers/adam-suffix-array/suffix-array-create.sh \
[path to cdec/python/pkg] \
[source corpus] \
[target corpus] \
[word alignment] \
[output suffix array directory] \
[output glue rules]
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

WARNING - This requires a lot of memory (approximately 10GB for a parallel corpus of 15 million sentence pairs)



Once the suffix array has been created, run another Moses wrapper script to extract the translation rules required for a particular set of input sentences.

\begin{SaveVerbatim}{myverb}
./scripts/training/wrappers/adam-suffix-array/suffix-array-extract.sh \
[suffix array directory from previous command] \
[input sentences] \   
[output rules directory] \
[number of jobs]
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

This command creates one file for each input sentences with just the rules required to decode that sentences. eg.

\begin{SaveVerbatim}{myverb}
# ls filtered.5/
grammar.0.gz	grammar.3.gz	grammar.7.gz
grammar.1.gz	grammar.4.gz	grammar.8.gz
grammar.10.gz	grammar.5.gz	grammar.9.gz ....
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Note - these files are gzipped, and the rules are formatted in the Hiero format, rather than the Moses format. eg.

\begin{SaveVerbatim}{myverb}
# zcat filtered.5/grammar.out.0.gz | head -1
[X] ||| monsieur [X,1] ||| mr [X,1] ||| 0.178069829941 2.04532289505 1.8692317009 0.268405526876 0.160579100251 0.0 0.0 ||| 0-0
\end{SaveVerbatim}
\begin{shaded}
\tiny
\UseVerbatim{myverb}
\end{shaded}

To use these rules in the decoder, put this into the ini file






\begin{SaveVerbatim}{myverb}
PhraseDictionaryALSuffixArray name=TranslationModel0 table-limit=20 \
num-features=7 path=[path-to-filtered-dir] input-factor=0 output-factor=0
PhraseDictionaryMemory name=TranslationModel1 num-features=1 \
path=[path-to-glue-grammar] input-factor=0 output-factor=0
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

\subsubsection*{
\label{advanced-incremental.texntoc19}Using the EMS}


Adam Lopez's suffix array implementation is integrated into the EMS, where all of the above commands are executed for you. Add the following line to your EMS config file:

\begin{SaveVerbatim}{myverb}
[TRAINING]
suffix-array = [pycdec package path]
# e.g.
# suffix-array = /home/github/cdec/python/pkg
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

and the EMS will use the suffix array instead of the usual Moses rule extraction algorithms.



You can also have multiple extractors running at once

\begin{SaveVerbatim}{myverb}
[GENERAL]
sa_extractors = 8
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

WARNING: currently the {\tt pycdec} simply forks itself N times, therefore this will require {\tt N} times more memory. Be careful with the interaction with multiple evaluations in parallel in EMS and large suffix arrays.












\begin{center}\textit{\small Subsection last  modified on October 26, 2015, at 10:20 PM}\end{center}
