Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mira-style-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 //
34 
35 #ifndef RERANKER_MIRA_STYLE_MODEL_H_
36 #define RERANKER_MIRA_STYLE_MODEL_H_
37 
38 #include <cstdlib>
39 #include <string>
40 
41 #include "kernel-function.H"
42 #include "symbol-table.H"
43 
44 #include "perceptron-model.H"
45 
46 #define DEFAULT_MIRA_CLIP 0.1
47 
48 namespace reranker {
49 
50 using std::string;
51 
58  public:
64  virtual int Compare(const Model &model,
65  const Candidate &c1, const Candidate &c2) {
66  double score_diff = (c1.score() + c1.loss()) - (c2.score() + c2.loss());
67  return score_diff == 0.0 ? 0 : (score_diff < 0.0 ? -1 : 1);
68  }
69 };
70 
79  public:
81  MiraStyleModel(const string &name) : PerceptronModel(name),
82  mira_clip_(DEFAULT_MIRA_CLIP) { }
83  MiraStyleModel(const string &name, KernelFunction *kernel_fn) :
84  PerceptronModel(name, kernel_fn),
85  mira_clip_(DEFAULT_MIRA_CLIP) { }
86  MiraStyleModel(const string &name, KernelFunction *kernel_fn,
87  Symbols *symbols) :
88  PerceptronModel(name, kernel_fn, symbols),
89  mira_clip_(DEFAULT_MIRA_CLIP) { }
90 
91 
112  virtual void RegisterInitializers(Initializers &initializers) {
114  initializers.Add("mira_clip", &mira_clip_);
115  }
116 
119  void set_mira_clip(double mira_clip) { mira_clip_ = mira_clip; }
120 
125  virtual double ComputeStepSize(
126  const unordered_set<int> &gold_features,
127  const unordered_set<int> &best_scoring_features,
128  const CandidateSet &example) {
129  FeatureVector<int,double> vector_diff;
130  vector_diff.AddScaledSubvector(gold_features,
131  example.GetGold().features(), 1.0);
132  vector_diff.AddScaledSubvector(best_scoring_features,
133  example.GetBestScoring().features(), -1.0);
134  double loss_weight = use_weighted_loss() ? example.loss_weight() : 1.0;
135  double loss_diff =
136  loss_weight *
137  (example.GetBestScoring().loss() - example.GetGold().loss());
138  double score_diff =
139  example.GetBestScoring().score() - example.GetGold().score();
140  double raw_step = (loss_diff + score_diff) / vector_diff.Dot(vector_diff);
141  step_size_ = raw_step > mira_clip_ ? mira_clip_ : raw_step;
142  return step_size_;
143  }
144  private:
145  // data members
146  double mira_clip_;
147 };
148 
149 } // namespace reranker
150 
151 #endif
Provides the reranker::PerceptronModel reranker class.
const Candidate & GetGold() const
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
virtual void RegisterInitializers(Initializers &initializers)
Registers one additional variable that may be initialized when this object is constructed via Factory...
double score() const
Returns the reranker’s score for this candidate.
Definition: candidate.H:131
const Candidate & GetBestScoring() const
Provides the reranker::Symbols interface as well as the reranker::StaticSymbolTable implementation...
Symbols * symbols() const
Returns the symbol table for this model.
Definition: model.H:284
MiraStyleModel(const string &name, KernelFunction *kernel_fn)
virtual void RegisterInitializers(Initializers &initializers)
Registers several variables that may be initialized when this object is constructed via Factory::Crea...
void set_mira_clip(double mira_clip)
Sets the maximum value for a step size computed by ComputeStepSize.
This class implements a perceptron model reranker.
An inner interface specifying comparison between two Candidate instances.
Definition: candidate.H:108
virtual int Compare(const Model &model, const Candidate &c1, const Candidate &c2)
Returns 0 if the two candidates’ scores are equal, less than zero if the score of c1 is less than tha...
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_...
double loss_weight() const
Returns the weight of the loss for this candidate set’s reference.
void Add(const string &name, T *member, bool required=false)
Definition: factory.H:225
A class to do “direct loss minimization” by considering the score of a candidate to be its raw score ...
#define DEFAULT_MIRA_CLIP
FeatureVector< K, V > & AddScaledSubvector(const Collection &feature_uids, const FeatureVector< K, V > &feature_vector, V scalar)
Modifies this vector so that it equals this vector plus the scaled specified subvector.
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
virtual bool use_weighted_loss()
Definition: model.H:469
A class to represent a candidate in a set of candidates that constitutes a training instance for a re...
Definition: candidate.H:60
double loss() const
Returns the loss of this candidate.
Definition: candidate.H:129
double step_size_
The last value computed by the ComputeStepSize method.
V Dot(const FeatureVector< K, V > &other) const
Computes the dot product of this feature vector with the specified FeatureVector. ...
Provides the reranker::KernelFunction interface.
A subclass of PerceptronModel that differs only in the way that the ComputeStepSize method is impleme...
An interface specifying a kernel function for two FeatureVector instances.
MiraStyleModel(const string &name, KernelFunction *kernel_fn, Symbols *symbols)
A container for all the member initializers for a particular Factory-constructible instance...
Definition: factory.H:203
MiraStyleModel(const string &name)
const FeatureVector< int, double > & features() const
Returns the feature vector for this candidate.
Definition: candidate.H:137