23 #include <fst/fstlib.h> 24 #include <boost/tuple/tuple_comparison.hpp> 31 "vector of minimum changes accepted during line optimization");
33 "vector of maximum changes accepted during line optimization");
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");
40 DEFINE_double (gamma_threshold, 0.000005,
"gamma parameter update threshold");
41 DEFINE_double (bleu_threshold, 0.000001,
"BLEU parameter update threshold");
44 "print precision for vector weight components");
46 "The number of lattices necessary for tuning a feature parameter");
47 DEFINE_int32 (random_directions, 10,
"The number of random directions");
49 DEFINE_bool (verbose,
false,
"enable verbose output");
51 "load all lattices into memory prior to MERT");
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");
57 "do not skip lattices if they do not contain features");
59 "print an old style log with feature vectors displayed");
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");
64 static const char* CMD_LINE_TOKEN_AXES =
"axes";
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);
74 tracer << directions.Size() <<
" optimization directions(s)\n";
77 template<
class Algo,
class ErrorSurface>
81 directions = FLAGS_random_directions;
82 useAxes = FLAGS_random_axes;
85 std::map<boost::tuple<std::string, std::string, std::string> ,
Optimizer *>
87 std::map<boost::tuple<std::string, std::string, std::string> ,
Optimizer *>
89 optimizers[boost::make_tuple (
"bleu",
"lmert",
92 optimizers[boost::make_tuple (
"bleu",
"tgmert",
95 optimizers[boost::make_tuple (
"bleu",
"debug",
98 optimizers[boost::make_tuple (
"bleu",
"lmert",
101 optimizers[boost::make_tuple (
"bleu",
"tgmert",
104 optimizers[boost::make_tuple (
"bleu",
"debug",
110 std::map<boost::tuple<std::string, std::string, std::string> ,
Optimizer *>
115 const std::string& filename,
const bool normalize) {
116 std::ofstream ofs (filename.c_str() );
118 cerr <<
"ERROR: unable to write file " << filename <<
'\n';
125 tracer <<
"writing final parameters to file: " << filename <<
'\n';
132 int main (
int argc,
char** argv) {
133 tracer << argv[0] <<
" starting\n";
134 std::string usage =
"latmert\n\n Usage:";
136 InitFst (usage.c_str(), &argc, &argv,
true);
137 if (FLAGS_lambda.empty() ) {
138 cerr <<
"ERROR: mandatory parameter not specified: 'lambda'\n";
141 if (FLAGS_direction.empty() ) {
142 cerr <<
"ERROR: mandatory parameter not specified: 'direction'\n";
145 if (FLAGS_algorithm.empty() ) {
146 cerr <<
"ERROR: mandatory parameter not specified: 'algorithm'\n";
164 unsigned int dim = lambda.size();
167 if (!FLAGS_params_min.empty() ) {
168 params_min = fst::ParseParamString<double, std::vector<double> > (
170 if (params_min.size() != dim) {
171 cerr <<
"Params min dimensionality does not match param vector " 176 if (!FLAGS_params_max.empty() ) {
177 params_max = fst::ParseParamString<double, std::vector<double> > (
179 if (params_max.size() != dim) {
180 cerr <<
"Params max dimensionality does not match param vector " 185 tracer <<
"dimensions = " << dim <<
'\n';
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';
192 boost::tuple<std::string, std::string, std::string> key = boost::make_tuple (
193 FLAGS_error_function,
194 FLAGS_algorithm, FLAGS_search);
196 if (optimizer == 0) {
197 tracer <<
"Unrecognized Optimizer\t" << FLAGS_error_function <<
"\t" <<
198 FLAGS_algorithm <<
"\t" << FLAGS_search << endl;
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";
210 tracer <<
" start: " << lambda <<
" " << startScore << endl;
211 tracer <<
" final: " << tuned.first <<
" " << tuned.second << endl;
213 tracer <<
" start: " << startScore << endl;
214 tracer <<
" final: " << tuned.second << endl;
216 if (!FLAGS_write_parameters.empty() ) {
218 FLAGS_normalize_parameters);
virtual void LoadRefData(std::vector< std::string >)=0
std::map< boost::tuple< std::string, std::string, std::string >, Optimizer * > optimizers
DEFINE_int32(print_precision, 6,"print precision for vector weight components")
std::map< boost::tuple< std::string, std::string, std::string >, Optimizer * > CreateOptimizers()
void Initialize(const PARAMS ¶ms_min, const PARAMS ¶ms_max)
virtual void InitTuneSet(bool)=0
std::vector< std::string > InitRefDataFilenames(int, char **)
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)
DEFINE_string(params_min,"","vector of minimum changes accepted during line optimization")
std::string ReadWeight(const std::string &)
void WriteTunedParameters(const PARAMS &best, const std::string &filename, const bool normalize)
DEFINE_bool(verbose, false,"enable verbose output")
std::vector< PARAMS > InitializeVectors(const std::string &)
static const std::string & Type()