Cambridge SMT System
Optimize.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 <main.custom_assert.hpp>
13 #include <main.logger.hpp>
14 #include "Optimize.h"
15 #include <ctime>
16 #include <boost/random/uniform_on_sphere.hpp>
17 #include <boost/random/variate_generator.hpp>
18 
19 typedef unsigned int Sid; // Sentence ID
20 
21 typedef std::vector<Sid> SentenceList;
22 
24 
25 DEFINE_int32 (threads, 1, "Number of threads used for line search");
26 
27 DEFINE_int64 (seed, 0, "Random direction seed");
28 
29 unsigned int GetSeed() {
30  static unsigned int seed = 0;
31  if (seed != 0) {
32  return seed;
33  }
34  if (FLAGS_seed == 0) {
35  seed = static_cast<unsigned int> (std::time (0) );
36  } else {
37  seed = (unsigned int) FLAGS_seed;
38  }
39  tracer << "Seed used for random directions: " << seed << endl;
40  return seed;
41 }
42 
43 RandDirGenerator::RandDirGenerator (unsigned int dim) : dim (dim) {
44  rand_gen.seed (GetSeed() );
45 }
46 
48  boost::uniform_on_sphere<double> unif_sphere (dim);
49  boost::random::variate_generator<boost::random::mt19937&, boost::random::uniform_on_sphere<double> >
50  sphereGen (rand_gen, unif_sphere);
51  return sphereGen();
52 }
53 
54 PARAMS ComputeFinalPoint (const PARAMS& lambda, const PARAMS& direction,
55  const double gamma) {
56  PARAMS vv (lambda.size() );
57  for (unsigned int k = 0; k < lambda.size(); ++k) {
58  vv[k] = lambda[k] + gamma * direction[k];
59  }
60  return vv;
61 }
62 /*
63  * Scale a PARAMS by the a single parameter value. Designed to combat numerical instability.
64  */
65 
66 PARAMS VectorScale (const PARAMS& vw, const unsigned int k) {
67  const double normalization = fabs (vw[k]);
68  PARAMS vv = vw;
69  if (abs (normalization) > kDoubleDelta) {
70  for (unsigned int i = 0; i < vw.size(); ++i) {
71  vv[i] = vv[i] / normalization;
72  }
73  }
74  return vv;
75 }
76 /*
77  template<class T>
78  OptimizationResult OptimizeRandom(const PARAMS& startPoint, int noOfDirctions, const Algorithm<T>& algo) {
79 
80  }
81  */
82 
84  hasDirection (0) {
85 }
86 
87 void Directions::Resize (unsigned int dim, TuneSet& lats) {
88  this->dim = dim;
89  current.resize (dim);
90  if (hasDirection != 0) {
91  delete hasDirection;
92  }
93  hasDirection = new bool[dim];
94  for (unsigned int i = 0; i < dim; ++i) {
95  current[i] = 0.0;
96  hasDirection[i] = false;
97  }
98  feature_filter.clear();
99  unordered_set<unsigned int> total_features;
100  unordered_map<unsigned int, std::vector<Sid> > latticesByFeature;
101  for (std::vector<Sid>::const_iterator sit = lats.ids.begin();
102  sit != lats.ids.end(); ++sit) {
103  //sparse_hash_set<unsigned int> features;
104  TupleArcFst* fst = lats.GetVectorLattice (*sit, opts.useCache);
105  for (fst::StateIterator<TupleArcFst> si (*fst); !si.Done(); si.Next() ) {
106  TupleArcFst::StateId state_id = si.Value();
107  for (fst::MutableArcIterator<TupleArcFst> ai (fst, si.Value() );
108  !ai.Done(); ai.Next() ) {
109  const TupleW w = ai.Value().weight;
110  for (fst::SparseTupleWeightIterator<FeatureWeight, int> it (w);
111  !it.Done(); it.Next() ) {
112  latticesByFeature[it.Value().first - 1].push_back (*sit);
113  }
114  }
115  }
116  if (!opts.useCache) {
117  delete fst;
118  }
119  }
120  for (unordered_map<unsigned int, std::vector<Sid> >::const_iterator it =
121  latticesByFeature.begin(); it != latticesByFeature.end(); ++it) {
122  if (it->second.size() >= opts.latticeCutoff) {
123  hasDirection[it->first] = true;
124  total_features.insert (it->first);
125  for (std::vector<Sid>::const_iterator sit = it->second.begin();
126  sit != it->second.end(); ++sit) {
127  feature_filter[*sit].insert (it->first);
128  }
129  }
130  }
131  tracer << "lattices contain " << total_features.size() << " features"
132  << endl;
133 }
134 
135 const PARAMS& Directions::Get (unsigned int axis) {
136  current[prev] = 0;
137  prev = axis;
138  if (directions.count (axis) > 0) {
139  return directions[axis];
140  }
141  current[axis] = 1;
142  return current;
143 }
144 
145 void Directions::Set (unsigned int axis, PARAMS direction) {
146  directions[axis] = direction;
147 }
148 
149 void Directions::Set (std::vector<PARAMS> batch, TuneSet& lats) {
150  directions.clear();
151  for (unsigned int i; i < batch.size(); ++i) {
152  directions[i] = batch[i];
153  }
154  dim = batch.size();
155  Resize (dim, lats);
156 }
157 
158 unsigned int Directions::Size() {
159  return dim;
160 }
161 
162 bool Directions::ContainsAxis (Sid sid, unsigned int axis) {
163  if (directions.count (axis) > 0) {
164  return true;
165  }
166  const unordered_set<unsigned int>& features = feature_filter[sid];
167  unordered_set<unsigned int>::const_iterator it = features.find (axis);
168  bool containsAxis = it != features.end();
169  return containsAxis;
170 }
171 
172 const std::vector<Sid> Directions::FilteredLattices (unsigned int axis,
173  const std::vector<Sid>& lats) {
174  std::vector<Sid> filtered;
175  for (std::vector<Sid>::const_iterator sit = lats.begin();
176  sit != lats.end(); ++sit) {
177  if (ContainsAxis (*sit, axis) && !opts.noSkip) {
178  if (opts.verbose) {
179  tracer << "Skipping axis: " << axis << " for sentence "
180  << *sit << '\n';
181  }
182  filtered.push_back (*sit);
183  }
184  }
185  return filtered;
186 }
187 
188 bool Directions::ContainsAxis (unsigned int axis) {
189  if (directions.count (axis) > 0) {
190  return true;
191  }
192  return hasDirection[axis];
193 }
194 
196  return FLAGS_threads;
197 }
MertOpt opts
Definition: MertCommon.cpp:14
void Resize(unsigned int, TuneSet &lats)
Definition: Optimize.cpp:87
const PARAMS & Get(const unsigned int)
Definition: Optimize.cpp:135
bool noSkip
Definition: MertCommon.h:38
DEFINE_int32(threads, 1,"Number of threads used for line search")
PARAMS GenerateDirection()
Definition: Optimize.cpp:47
bool useCache
Definition: MertCommon.h:37
RandDirGenerator(unsigned int dim=0)
Definition: Optimize.cpp:43
Definition: fstio.hpp:27
PARAMS VectorScale(const PARAMS &vw, const unsigned int k)
Definition: Optimize.cpp:66
#define tracer
Definition: data.lmbr.hpp:18
TupleArcFst * GetVectorLattice(const Sid s, const bool use_cache) const
Definition: TuneSet.cpp:87
unsigned int Sid
Definition: MertCommon.h:45
bool verbose
Definition: MertCommon.h:33
void Set(const unsigned int, PARAMS)
Definition: Optimize.cpp:145
Implements Tropical Sparse tuple weight semiring, extending from openfst SparsePowerWeight class...
MERT mert
Definition: Optimize.cpp:23
Static variables for logger. Include only once from main file.
std::vector< Sid > SentenceList
Definition: Optimize.cpp:21
SentenceList ids
Definition: TuneSet.h:34
DEFINE_int64(seed, 0,"Random direction seed")
bool ContainsAxis(unsigned int)
Definition: Optimize.cpp:188
int32 latticeCutoff
Definition: MertCommon.h:30
const double kDoubleDelta
Definition: MertCommon.h:52
const std::vector< Sid > FilteredLattices(unsigned int, const std::vector< Sid > &)
Definition: Optimize.cpp:172
PARAMS ComputeFinalPoint(const PARAMS &lambda, const PARAMS &direction, const double gamma)
Definition: Optimize.cpp:54
fst::VectorFst< TupleArc > TupleArcFst
unsigned int Sid
Definition: Optimize.cpp:19
unsigned int Size()
Definition: Optimize.cpp:158
unsigned int GetSeed()
Definition: Optimize.cpp:29
hifst-specific classes and methods included in this namespace.
Definition: Optimize.h:31
int GetNoOfThreads()
Definition: Optimize.cpp:195
Static variable for custom_assert. Include only once from main file.