Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
perceptron-model.H
Go to the documentation of this file.
1 // Copyright 2012, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // -----------------------------------------------------------------------------
30 //
31 //
35 
36 #ifndef RERANKER_PERCEPTRON_MODEL_H_
37 #define RERANKER_PERCEPTRON_MODEL_H_
38 
39 #include <vector>
40 #include <unordered_set>
41 #include <memory>
42 
43 #include "candidate-set.H"
44 #include "dot-product.H"
45 #include "model.H"
46 #include "training-time.H"
47 #include "training-vector-set.H"
48 
49 #define DEFAULT_MAX_EPOCHS_IN_DECLINE 5
50 
51 namespace reranker {
52 
53 using std::shared_ptr;
54 using std::unordered_set;
55 using std::vector;
56 
63 class PerceptronModel : public Model {
64  public:
67 
71  Model("", new DotProduct()),
72  models_(),
73  best_models_(),
77  step_size_(1.0) {
79  }
80 
85  PerceptronModel(const string &name) :
86  Model(name, new DotProduct()),
87  models_(),
88  best_models_(),
92  step_size_(1.0) {
94  }
95 
101  PerceptronModel(const string &name, KernelFunction *kernel_fn) :
102  Model(name, kernel_fn),
103  models_(),
104  best_models_(),
105  best_model_epoch_(-1),
108  step_size_(1.0) {
110  }
111 
120  PerceptronModel(const string &name, KernelFunction *kernel_fn,
121  Symbols *symbols) :
122  Model(name, kernel_fn, symbols),
123  models_(),
124  best_models_(),
125  best_model_epoch_(-1),
128  step_size_(1.0) {
130  }
131 
132 
134  virtual ~PerceptronModel() { }
135 
150  virtual bool NeedToUpdate(Model *model, CandidateSet &example);
151  };
152 
172  virtual void Update(Model *m, CandidateSet &example);
173  };
174 
176  virtual const string &model_spec() const { return model_spec_; }
177 
179  virtual const string &proto_reader_spec() const {
180  return proto_reader_spec_;
181  }
182 
184  virtual const string &proto_writer_spec() const {
185  return proto_writer_spec_;
186  }
187 
190  virtual int best_model_epoch() const { return best_model_epoch_; }
191 
256  virtual void RegisterInitializers(Initializers &initializers);
257 
260  virtual void Init(const Environment *env, const string &arg);
261 
262  // training methods
263 
264  virtual bool NeedToKeepTraining();
265 
278  virtual void Train(CandidateSetIterator &examples,
279  CandidateSetIterator &development_test);
280 
281  virtual void NewEpoch();
282 
283  virtual void EndOfEpoch();
284 
290  virtual void TrainOneEpoch(CandidateSetIterator &examples);
291 
295  virtual void TrainOnExample(CandidateSet &example);
296 
302  virtual bool NeedToUpdate(CandidateSet &example);
303 
309  virtual void Update(CandidateSet &example);
310 
312  virtual double Evaluate(CandidateSetIterator &development_test);
313 
326  virtual void ScoreCandidates(CandidateSet &candidates, bool training);
327 
337  virtual double ScoreCandidate(Candidate &candidate, bool training);
338 
339  // mutators
340 
342  virtual void CompactifyFeatureUids();
343 
347  void set_max_epochs_in_decline(int max_epochs_in_decline) {
348  max_epochs_in_decline_ = max_epochs_in_decline;
349  }
350 
353  virtual const TrainingVectorSet &models() const { return models_; }
354 
355  protected:
358  GetUpdatePredicate("PerceptronModelDefaultUpdatePredicate()");
359  updater_ =
360  GetUpdater("PerceptronModelDefaultUpdater()");
361  }
362 
379  virtual void ComputeFeaturesToUpdate(const CandidateSet &example,
380  unordered_set<int> &
381  gold_features_to_update,
382  unordered_set<int> &
383  best_scoring_features_to_update) const;
384 
389  virtual double ComputeStepSize(const unordered_set<int> &gold_features,
390  const unordered_set<int> &best_scoring_features,
391  const CandidateSet &example) {
392  return step_size_;
393  // For MIRA, simply derive class and override to be
394  /*
395  int feature_count = gold_features.size() + best_scoring_features.size();
396  double loss_diff =
397  example.GetBestScoring().loss() - example.GetGold().loss();
398  double score_diff =
399  example.GetBestScoring().score() - example.GetGold().score();
400  double raw_step = (loss_diff + score_diff) / feature_count;
401  step_size_ = raw_step > mira_clip_ : mira_clip_ : raw_step;
402  return step_size_;
403  */
404  }
405 
406  // data members
422  double step_size_;
423  string model_spec_;
424 
428  static string proto_reader_spec_;
432  static string proto_writer_spec_;
433 };
434 
435 } // namespace reranker
436 
437 #endif
virtual const string & model_spec() const
Returns the spec string for constructing a default instance of this model so it may be properly de-se...
virtual double ScoreCandidate(Candidate &candidate, bool training)
Scores a candidate according to either the raw or averaged version of this perceptron model...
Model is an interface for reranking models.
Definition: model.H:141
const string & name() const
Returns the unique name for this model instance.
Definition: model.H:281
This class defines a dot product kernel function for two vectors.
Definition: dot-product.H:47
An interface specifying iteration over CandidateSet instances, using Java-style semantics (sorry...
TrainingVectorSet best_models_
The best models seen so far during training, according to evaluation on the held-out development test...
Symbols * symbols() const
Returns the symbol table for this model.
Definition: model.H:284
Provides a dot product implementation of the reranker::KernelFunction interface.
TrainingVectorSet models_
The feature vectors representing this model.
PerceptronModel(const string &name, KernelFunction *kernel_fn)
Constructs a new perceptron model with the specified kernel function.
virtual const string & proto_reader_spec() const
Returns the spec string for contructing an instance of a ModelProtoReader capable of de-serializing t...
int num_epochs_in_decline_
The current number of training epochs in which the model has been degrading in development set perfor...
virtual const TrainingVectorSet & models() const
Returns the set of models and statistics used by this PerceptronModel instance.
virtual void RegisterInitializers(Initializers &initializers)
Registers several variables that may be initialized when this object is constructed via Factory::Crea...
virtual void CompactifyFeatureUids()
Renumbers the potentially sparse feature uid’s so that they occupy the interval [0,n-1] densely, for n non-zero features in use by this model.
The default update function for perceptron models.
static string proto_writer_spec_
A string that specifies to construct a PerceptronModelProtoWriter, which is capable of serializing an...
This class implements a perceptron model reranker.
virtual void TrainOnExample(CandidateSet &example)
Trains this model on the specified training example.
virtual void TrainOneEpoch(CandidateSetIterator &examples)
Trains this model for one epoch, i.e., a single pass through the specified set of training examples...
PerceptronModel(const string &name)
Constructs a new perceptron model with a DotProduct kernel function.
void set_max_epochs_in_decline(int max_epochs_in_decline)
Sets the maximum number of training epochs to keep training after the model starts to degrade (i...
virtual double Evaluate(CandidateSetIterator &development_test)
Evaluates this model on the specified set of held-out development test data.
static string proto_reader_spec_
A string that specifies to construct a PerceptronModelProtoReader, which is capable of de-serializing...
virtual int best_model_epoch() const
Returns the epoch of the best models seen so far during training.
A class to construct a PerceptronModel from a ModelMessage instance.
A class to hold a set of candidates, either for training or test.
Definition: candidate-set.H:62
An interface specifying a converter from symbols (strings) to int indices.
Definition: symbol-table.H:57
An interface for an environment in which variables of various types are mapped to their values...
Definition: environment.H:125
A class to represent a candidate in a set of candidates that constitutes a training instance for a re...
Definition: candidate.H:60
A class to hold the several feature vectors needed during training (especially for the perceptron fam...
An inner interface for a predicate that tests whether a Model needs to be updated based on the curren...
Definition: model.H:243
virtual void ScoreCandidates(CandidateSet &candidates, bool training)
Scores the specified set of candidates according to either the raw or averaged version of this percep...
The default update predicate for perceptron and perceptron-style models, which indicates to do a mode...
PerceptronModel()
Constructs a new instance with the empty string for its name and the DotProduct kernel function...
virtual const string & proto_writer_spec() const
Returns the spec string for contructing an instance of a ModelProtoWriter capable of serializing this...
virtual void ComputeFeaturesToUpdate(const CandidateSet &example, unordered_set< int > &gold_features_to_update, unordered_set< int > &best_scoring_features_to_update) const
Computes the features to be updated for the gold candidate and the best-scoring candidate.
shared_ptr< Updater > GetUpdater(const string &spec) const
Definition: model.C:179
virtual ~PerceptronModel()
Destroys this perceptron model and all its data members.
virtual void Update(CandidateSet &example)
Updates the current model based on the specified set of candidates.
An inner interface specifying an update function for a model.
Definition: model.H:268
A class to construct a ModelMessage from a PerceptronModel instance.
double step_size_
The last value computed by the ComputeStepSize method.
virtual void Init(const Environment *env, const string &arg)
Initializes this instance.
virtual void Train(CandidateSetIterator &examples, CandidateSetIterator &development_test)
Trains this model on a collection of training examples, where each training example is a set of candi...
virtual double ComputeStepSize(const unordered_set< int > &gold_features, const unordered_set< int > &best_scoring_features, const CandidateSet &example)
Computes the step size for the next update, and, as a side effect, caches this value in step_size_...
shared_ptr< UpdatePredicate > update_predicate_
The update predicate for this model.
Definition: model.H:567
PerceptronModel(const string &name, KernelFunction *kernel_fn, Symbols *symbols)
Constructs a new perceptron model with the specified kernel function and symbol table.
#define DEFAULT_MAX_EPOCHS_IN_DECLINE
int best_model_epoch_
The epoch of the best models seen so far during training.
shared_ptr< UpdatePredicate > GetUpdatePredicate(const string &spec) const
Definition: model.C:171
int max_epochs_in_decline_
The maximum number of training epochs to keep training after the model starts to degrade (i...
An interface specifying a kernel function for two FeatureVector instances.
virtual bool NeedToKeepTraining()
Returns whether more training epochs are required for this model.
Class to hold a single training instance for a reranker, which is a set of examples, typically the n-best output of some input process, posibly including a gold-standard feature vector.
Reranker model interface.
Provides the reranker::TrainingVectorSet class.
virtual bool NeedToUpdate(CandidateSet &example)
Indicates whether the current model needs to be updated; the implementation here simply returns true ...
shared_ptr< Updater > updater_
The updater for this model.
Definition: model.H:569
A container for all the member initializers for a particular Factory-constructible instance...
Definition: factory.H:203
Provides the reranker::Time class, which holds the three notions of training time: current epoch...