Cambridge SMT System
randomlinesearch.hpp
Go to the documentation of this file.
1 #ifndef LMERT_RANDOMLINESEARCH_HPP
2 #define LMERT_RANDOMLINESEARCH_HPP
3 
4 #include <boost/math/constants/constants.hpp>
5 #include <boost/random/mersenne_twister.hpp>
6 #include <boost/random/uniform_on_sphere.hpp>
7 #include <boost/random/variate_generator.hpp>
8 #include <ctime>
9 
10 #include <constants-lmert.hpp>
11 
12 namespace ucam {
13 namespace lmert {
14 
15 template <class Arc>
17 public:
21  ucam::fsttools::PARAMS32 const &lambda ) :
22  lambda_ ( lambda ), fdim_ ( lambda.size() ),
23  gammaMin_ ( rg.get<float> ( HifstConstants::kLmertMinGamma ) ),
24  minBleuGain_ ( rg.get<float> ( HifstConstants::kLmertMinBleuGain ) ),
25  seed_ ( rg.get<int> ( HifstConstants::kLmertRandomSeed ) ),
26  numRandDirs_ ( rg.get<int> ( HifstConstants::kLmertNumRandomDirections ) ) {
27  rand_gen.seed ( seed_ );
28  if ( numRandDirs_ <= 0 )
29  numRandDirs_ = 2 * fdim_;
30  iBleu_ = ts.ComputeBleu ( bs, lambda );
31  FORCELINFO("Lambda: " << ucam::util::printout(lambda_) << "(dim " << fdim_ << ")");
32  FORCELINFO("Initial Bleu: " << iBleu_);
33  optBleu_ = iBleu_;
34  while ( true ) {
35  GenerateDirections();
36  double optGamma = 0.0;
37  int optDirection;
38  ucam::fsttools::Bleu pBleu ( optBleu_ );
39  for ( int i = 0; i < directions_.size(); i++ ) {
40  LineOptimize< Arc > lopt ( rg, ts, bs, lambda_, directions_[i] );
41  bool smallGamma = ( fabs ( lopt.OptimalGamma() ) < gammaMin_ );
42  // bool smallBleuGain = ( lopt.OptimalBleu().m_bleu <= optBleu_.m_bleu + minBleuGain_);
43  bool smallBleuGain = ( lopt.OptimalBleu().m_bleu <= optBleu_.m_bleu + minBleuGain_ || lopt.OptimalBleu().m_brev < optBleu_.m_brev); // added check on short hyps
44  if ( !smallBleuGain && !smallGamma ) { // TODO: add check for unbounded
45  optBleu_ = lopt.OptimalBleu();
46  optGamma = lopt.OptimalGamma();
47  optDirection = i;
48  }
49  LINFO("Direction[" << i << "]: Opt Bleu: " << lopt.OptimalBleu() <<
50  ( smallBleuGain ? " - too small; " : "; " )
51  << "Gamma: " << lopt.OptimalGamma() << ( smallGamma ? " - too small" : "" ));
52  }
53  FORCELINFO("Full random iteration completed. Opt Bleu: " << optBleu_ <<
54  "; Opt Gamma: " << optGamma << ( fabs(optGamma) < gammaMin_ ? " - too small" : "" ) );
55  if ( optBleu_.m_bleu <= pBleu.m_bleu + minBleuGain_ ) {
56  FORCELINFO("Bleu gain less than threshold. Exiting.");
57  optBleu_ = pBleu;
58  break;
59  }
60  UpdateLambda ( directions_[optDirection], optGamma );
61  FORCELINFO("Lambda: " << ucam::util::printout(lambda_) );
62  }
63  FORCELINFO("Initial Bleu: " << iBleu_);
64  FORCELINFO("Final Bleu: " << optBleu_);
65  FORCELINFO("Final Lambda: " << ucam::util::printout(lambda_) );
67  }
68 
69 private:
70 
71  void GenerateDirections() {
72  directions_.clear();
73  for ( int i = 0; i < fdim_; i++ ) {
74  PARAMS32 dir ( fdim_, 0.0 );
75  dir[i] = 1.0;
76  directions_.push_back ( dir );
77  }
78  boost::uniform_on_sphere<float> unif_sphere ( fdim_ );
79  FORCELINFO("Generating " << numRandDirs_ << " random directions");
80  for ( int i = 0; i < numRandDirs_; i++ ) {
81  boost::random::variate_generator<boost::random::mt19937&,
82  boost::random::uniform_on_sphere<float> >
83  sg ( rand_gen, unif_sphere );
84  directions_.push_back ( sg() );
85  }
86  }
87 
88  void UpdateLambda ( PARAMS32 direction, double gamma ) {
89  if ( gamma == 0.0 )
90  return;
91  float rv = fabs(lambda_[0] + gamma * direction[0]);
92  if (rv == 0.0)
93  rv = 1.0;
94  for ( int i = 0; i < fdim_; i++ ) {
95  lambda_[i] = (lambda_[i] + gamma * direction[i])/rv;
96  }
97  }
98 
99  boost::random::mt19937 rand_gen;
100  vector< ucam::fsttools::PARAMS32 > directions_;
101  ucam::fsttools::PARAMS32 lambda_;
102  ucam::fsttools::Bleu optBleu_;
103  ucam::fsttools::Bleu iBleu_;
104  float minBleuGain_;
105  float gammaMin_;
106  int fdim_;
107  int seed_;
108  int numRandDirs_;
109 };
110 
111 
112 }} // end namespaces
113 
114 #endif
#define LINFO(msg)
T get(const std::string &key) const
Returns parsed value associated to key.
Definition: registrypo.hpp:194
#define FORCELINFO(msg)
Bleu ComputeBleu(BleuScorer &bs)
Definition: tuneset.hpp:47
void WriteParamFile(const std::string &filename, std::vector< float > params_)
Write parameter vector to a file, with comma separators.
Definition: params.hpp:111
std::string const kLmertRandomSeed
RandomLineSearch(ucam::util::RegistryPO const &rg, ucam::fsttools::TuneSet< Arc > &ts, ucam::fsttools::BleuScorer &bs, ucam::fsttools::PARAMS32 const &lambda)
std::vector< float > PARAMS32
Definition: bleu.hpp:18
std::string const kLmertMinBleuGain
std::string const kLmertNumRandomDirections
ucam::fsttools::Bleu OptimalBleu()
std::string const kLmertMinGamma
ucam::fsttools::PARAMS32 PARAMS32
Definition: lmert.hpp:23
std::string const kLmertWriteParams
Definition: bleu.hpp:14
std::string printout(Container< T > const &container)