16 #include <boost/random/uniform_on_sphere.hpp> 17 #include <boost/random/variate_generator.hpp> 19 typedef unsigned int Sid;
25 DEFINE_int32 (threads, 1,
"Number of threads used for line search");
30 static unsigned int seed = 0;
34 if (FLAGS_seed == 0) {
35 seed =
static_cast<unsigned int> (std::time (0) );
37 seed = (
unsigned int) FLAGS_seed;
39 tracer <<
"Seed used for random directions: " << seed << endl;
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);
56 PARAMS vv (lambda.size() );
57 for (
unsigned int k = 0; k < lambda.size(); ++k) {
58 vv[k] = lambda[k] + gamma * direction[k];
67 const double normalization = fabs (vw[k]);
70 for (
unsigned int i = 0; i < vw.size(); ++i) {
71 vv[i] = vv[i] / normalization;
90 if (hasDirection != 0) {
93 hasDirection =
new bool[dim];
94 for (
unsigned int i = 0; i < dim; ++i) {
96 hasDirection[i] =
false;
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) {
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);
120 for (unordered_map<
unsigned int, std::vector<Sid> >::const_iterator it =
121 latticesByFeature.begin(); it != latticesByFeature.end(); ++it) {
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);
131 tracer <<
"lattices contain " << total_features.size() <<
" features" 138 if (directions.count (axis) > 0) {
139 return directions[axis];
146 directions[axis] = direction;
151 for (
unsigned int i; i < batch.size(); ++i) {
152 directions[i] = batch[i];
163 if (directions.count (axis) > 0) {
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();
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) {
179 tracer <<
"Skipping axis: " << axis <<
" for sentence " 182 filtered.push_back (*sit);
189 if (directions.count (axis) > 0) {
192 return hasDirection[axis];
196 return FLAGS_threads;
void Resize(unsigned int, TuneSet &lats)
const PARAMS & Get(const unsigned int)
DEFINE_int32(threads, 1,"Number of threads used for line search")
PARAMS GenerateDirection()
RandDirGenerator(unsigned int dim=0)
PARAMS VectorScale(const PARAMS &vw, const unsigned int k)
TupleArcFst * GetVectorLattice(const Sid s, const bool use_cache) const
void Set(const unsigned int, PARAMS)
Implements Tropical Sparse tuple weight semiring, extending from openfst SparsePowerWeight class...
Static variables for logger. Include only once from main file.
std::vector< Sid > SentenceList
DEFINE_int64(seed, 0,"Random direction seed")
bool ContainsAxis(unsigned int)
const double kDoubleDelta
const std::vector< Sid > FilteredLattices(unsigned int, const std::vector< Sid > &)
PARAMS ComputeFinalPoint(const PARAMS &lambda, const PARAMS &direction, const double gamma)
hifst-specific classes and methods included in this namespace.
Static variable for custom_assert. Include only once from main file.