Cambridge SMT System
latmert.main.cpp
Go to the documentation of this file.
1 //Copyright (c) 2012, University of Cambridge
2 //All rights reserved.
3 //
4 //Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met://
5 //
6 // * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8 // * Neither the name of the University of Cambridge nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9 //
10 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11 
12 #include "LatMertMain.h"
13 #include "DebugMert.h"
14 #include "TGMert.h"
15 #include "ParamsConfig.h"
16 #include "ErrorSurface.h"
17 #include "RefsData.h"
18 #include "CommonFlags.h"
19 #include <iostream>
20 #include <fstream>
21 #include <iomanip>
22 #include <vector>
23 #include <fst/fstlib.h>
24 #include <boost/tuple/tuple_comparison.hpp>
25 
27 // Parameter Flag Definitions //
29 
30 DEFINE_string (params_min, "",
31  "vector of minimum changes accepted during line optimization");
32 DEFINE_string (params_max, "",
33  "vector of maximum changes accepted during line optimization");
34 DEFINE_string (direction, "", "direction(s)");
35 DEFINE_string (write_surface, "", "path to write error surface");
36 DEFINE_string (write_parameters, "", "path to write tuned parameters");
37 DEFINE_string (algorithm, "", "Line search algorithm to use");
38 DEFINE_string (search, "powell", "Algorithm to pick search direction");
39 
40 DEFINE_double (gamma_threshold, 0.000005, "gamma parameter update threshold");
41 DEFINE_double (bleu_threshold, 0.000001, "BLEU parameter update threshold");
42 
43 DEFINE_int32 (print_precision, 6,
44  "print precision for vector weight components");
45 DEFINE_int32 (lattice_cutoff, 1,
46  "The number of lattices necessary for tuning a feature parameter");
47 DEFINE_int32 (random_directions, 10, "The number of random directions");
48 
49 DEFINE_bool (verbose, false, "enable verbose output");
50 DEFINE_bool (cache_lattices, false,
51  "load all lattices into memory prior to MERT");
52 DEFINE_bool (normalize_parameters, true,
53  "normalize final tuned parameters with respect to the first column");
54 DEFINE_bool (prune_stats, false, "print number of arcs pruned");
55 DEFINE_bool (ignore_gsf, false, "ignore grammar scale factor");
56 DEFINE_bool (no_skip, false,
57  "do not skip lattices if they do not contain features");
58 DEFINE_bool (full_log, false,
59  "print an old style log with feature vectors displayed");
60 DEFINE_bool (point_test, false,
61  "check that each optimised parameter gives the same error as hypotheses found by shortest path");
62 DEFINE_bool (random_axes, false, "use axes along with random directions");
63 
64 static const char* CMD_LINE_TOKEN_AXES = "axes";
65 
66 template<class Algo, class ErrorSurface>
68  std::string pattern = FLAGS_direction.data();
69  if (pattern.find (CMD_LINE_TOKEN_AXES) == 0) {
70  directions.Resize (dim, this->lats);
71  } else {
72  directions.Set (InitializeVectors (pattern), this->lats);
73  }
74  tracer << directions.Size() << " optimization directions(s)\n";
75 }
76 
77 template<class Algo, class ErrorSurface>
79  dirGen = RandDirGenerator (dim);
80  this->dim = dim;
81  directions = FLAGS_random_directions;
82  useAxes = FLAGS_random_axes;
83 }
84 
85 std::map<boost::tuple<std::string, std::string, std::string> , Optimizer *>
87  std::map<boost::tuple<std::string, std::string, std::string> , Optimizer *>
88  optimizers;
89  optimizers[boost::make_tuple ("bleu", "lmert",
90  "powell")] = new PowellOptimizer <
92  optimizers[boost::make_tuple ("bleu", "tgmert",
93  "powell")] = new PowellOptimizer <
95  optimizers[boost::make_tuple ("bleu", "debug",
96  "powell")] = new PowellOptimizer <
98  optimizers[boost::make_tuple ("bleu", "lmert",
99  "random")] = new RandomOptimizer <
100  LMertAlgorithm, ErrorSurface<IntegerEncRefs> > ();
101  optimizers[boost::make_tuple ("bleu", "tgmert",
102  "random")] = new RandomOptimizer <
103  TGMertAlgorithm<TupleArc>, ErrorSurface<IntegerEncRefs> > ();
104  optimizers[boost::make_tuple ("bleu", "debug",
105  "random")] = new RandomOptimizer <
106  DebugMertAlgorithm, ErrorSurface<IntegerEncRefs> > ();
107  return optimizers;
108 }
109 
110 std::map<boost::tuple<std::string, std::string, std::string> , Optimizer *>
113 
114 void WriteTunedParameters (const PARAMS& best,
115  const std::string& filename, const bool normalize) {
116  std::ofstream ofs (filename.c_str() );
117  if (!ofs.good() ) {
118  cerr << "ERROR: unable to write file " << filename << '\n';
119  exit (1);
120  }
121  PARAMS final = best;
122  if (opts.scaleParams) {
123  final = VectorScale (final);
124  }
125  tracer << "writing final parameters to file: " << filename << '\n';
126  ofs << std::fixed << std::setprecision (opts.printPrecision) << final << '\n';
127  ofs.close();
128  tracer << " " << std::fixed << std::setprecision (opts.printPrecision) << final
129  << '\n';
130 }
131 
132 int main (int argc, char** argv) {
133  tracer << argv[0] << " starting\n";
134  std::string usage = "latmert\n\n Usage:";
135  usage += argv[0];
136  InitFst (usage.c_str(), &argc, &argv, true);
137  if (FLAGS_lambda.empty() ) {
138  cerr << "ERROR: mandatory parameter not specified: 'lambda'\n";
139  return 1;
140  }
141  if (FLAGS_direction.empty() ) {
142  cerr << "ERROR: mandatory parameter not specified: 'direction'\n";
143  return 1;
144  }
145  if (FLAGS_algorithm.empty() ) {
146  cerr << "ERROR: mandatory parameter not specified: 'algorithm'\n";
147  return 1;
148  }
149  //tracer << "M=" << PARAMS::VECTOR_DIMENSIONS << " dimensions" << '\n';
150  // Set options
151  opts.bleuThreshold = FLAGS_bleu_threshold;
152  opts.gammaThreshold = FLAGS_gamma_threshold;
153  opts.verbose = FLAGS_verbose;
154  opts.printPrecision = FLAGS_print_precision;
155  opts.latticeCutoff = FLAGS_lattice_cutoff;
156  opts.writeSurface = FLAGS_write_surface;
157  opts.scaleParams = FLAGS_normalize_parameters;
158  opts.ignoreGsf = FLAGS_ignore_gsf;
159  opts.useCache = FLAGS_cache_lattices;
160  opts.noSkip = FLAGS_no_skip;
161  opts.fullLog = FLAGS_full_log;
162  opts.pointTest = FLAGS_point_test;
163  PARAMS lambda = InitializeVectors (FLAGS_lambda.data() ) [0];
164  unsigned int dim = lambda.size();
165  PARAMS params_min;
166  PARAMS params_max;
167  if (!FLAGS_params_min.empty() ) {
168  params_min = fst::ParseParamString<double, std::vector<double> > (
169  ReadWeight (FLAGS_params_min.data() ) );
170  if (params_min.size() != dim) {
171  cerr << "Params min dimensionality does not match param vector "
172  << endl;
173  exit (1);
174  }
175  }
176  if (!FLAGS_params_max.empty() ) {
177  params_max = fst::ParseParamString<double, std::vector<double> > (
178  ReadWeight (FLAGS_params_max.data() ) );
179  if (params_max.size() != dim) {
180  cerr << "Params max dimensionality does not match param vector "
181  << endl;
182  exit (1);
183  }
184  }
185  tracer << "dimensions = " << dim << '\n';
186  mert.limits.Initialize (params_min, params_max);
187  tracer << "params min = " << params_min << '\n';
188  tracer << "params max = " << params_max << '\n';
189  tracer << "gamma threshold = " << FLAGS_gamma_threshold << '\n';
190  tracer << "bleu threshold = " << FLAGS_bleu_threshold << '\n';
191  tracer << "weight type = " << TupleW::Type() << '\n';
192  boost::tuple<std::string, std::string, std::string> key = boost::make_tuple (
193  FLAGS_error_function,
194  FLAGS_algorithm, FLAGS_search);
195  Optimizer* optimizer = optimizers[key];
196  if (optimizer == 0) {
197  tracer << "Unrecognized Optimizer\t" << FLAGS_error_function << "\t" <<
198  FLAGS_algorithm << "\t" << FLAGS_search << endl;
199  exit (1);
200  }
201  optimizer->InitTuneSet (FLAGS_cache_lattices);
202  std::vector<std::string> refFilenames = InitRefDataFilenames (argc, argv);
203  optimizer->LoadRefData (refFilenames);
204  optimizer->Init (dim);
205  std::string startScore = optimizer->ComputeError (lambda);
206  tracer << "starting point bleu=" << startScore << endl;
207  pair<PARAMS, double> tuned = (*optimizer) (lambda);
208  tracer << "optimization result:\n";
209  if (opts.fullLog) {
210  tracer << " start: " << lambda << " " << startScore << endl;
211  tracer << " final: " << tuned.first << " " << tuned.second << endl;
212  } else {
213  tracer << " start: " << startScore << endl;
214  tracer << " final: " << tuned.second << endl;
215  }
216  if (!FLAGS_write_parameters.empty() ) {
217  WriteTunedParameters (tuned.first, FLAGS_write_parameters,
218  FLAGS_normalize_parameters);
219  }
220  return 0;
221 }
MertOpt opts
Definition: MertCommon.cpp:14
virtual void LoadRefData(std::vector< std::string >)=0
std::map< boost::tuple< std::string, std::string, std::string >, Optimizer * > optimizers
bool noSkip
Definition: MertCommon.h:38
bool useCache
Definition: MertCommon.h:37
DEFINE_int32(print_precision, 6,"print precision for vector weight components")
#define tracer
Definition: data.lmbr.hpp:18
std::map< boost::tuple< std::string, std::string, std::string >, Optimizer * > CreateOptimizers()
virtual void Init(int)=0
void Initialize(const PARAMS &params_min, const PARAMS &params_max)
bool verbose
Definition: MertCommon.h:33
bool pointTest
Definition: MertCommon.h:40
virtual void Init(int)
ParamsConfig limits
Definition: Optimize.h:32
virtual void InitTuneSet(bool)=0
std::vector< std::string > InitRefDataFilenames(int, char **)
Definition: MertCommon.cpp:192
MERT mert
Definition: Optimize.cpp:23
double gammaThreshold
Definition: MertCommon.h:31
bool scaleParams
Definition: MertCommon.h:35
DEFINE_double(gamma_threshold, 0.000005,"gamma parameter update threshold")
virtual std::string ComputeError(const PARAMS &lambda)=0
int main(int argc, char **argv)
PARAMS VectorScale(const PARAMS &, const unsigned int k=0)
Definition: Optimize.cpp:66
int32 latticeCutoff
Definition: MertCommon.h:30
virtual void Init(int)
DEFINE_string(params_min,"","vector of minimum changes accepted during line optimization")
double bleuThreshold
Definition: MertCommon.h:32
bool fullLog
Definition: MertCommon.h:39
std::string ReadWeight(const std::string &)
Definition: MertCommon.cpp:140
void WriteTunedParameters(const PARAMS &best, const std::string &filename, const bool normalize)
int32 printPrecision
Definition: MertCommon.h:29
std::string writeSurface
Definition: MertCommon.h:34
bool ignoreGsf
Definition: MertCommon.h:36
DEFINE_bool(verbose, false,"enable verbose output")
std::vector< PARAMS > InitializeVectors(const std::string &)
Definition: MertCommon.cpp:126