Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
training-vector-set.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_TRAINING_VECTOR_SET_H_
37 #define RERANKER_TRAINING_VECTOR_SET_H_
38 
39 #include <iostream>
40 #include <unordered_map>
41 #include <unordered_set>
42 
43 #include "training-time.H"
44 #include "feature-vector.H"
45 
46 namespace reranker {
47 
48 using std::cerr;
49 using std::endl;
50 using std::unordered_map;
51 using std::unordered_set;
52 
60  public:
65  virtual ~TrainingVectorSet() { }
66 
67  // accessors
68 
72  const FeatureVector<int,double> &weights() const { return weights_; }
75  return average_weights_;
76  }
77 
83  const FeatureVector<int,double> &GetModel(bool raw) const {
84  return raw ? weights() : average_weights();
85  }
86 
87  // mutators
88 
103  template <typename Collection>
104  void UpdateWeights(const Time &time,
105  const Collection &feature_uids,
106  const FeatureVector<int,double> &feature_vector,
107  double scalar) {
108  weights_.AddScaledSubvector(feature_uids, feature_vector, scalar);
109  }
110 
114  template <typename Collection>
116  const Collection &
117  gold_feature_uids,
118  const Collection &
119  candidate_feature_uids) {
120  UpdateFeatureAverages(time,
121  gold_feature_uids.begin(),
122  gold_feature_uids.end());
123  UpdateFeatureAverages(time,
124  candidate_feature_uids.begin(),
125  candidate_feature_uids.end());
126  }
127 
128  void UpdateAllFeatureAverages(const Time &time) {
129  // Get set union of non-zero feature weights and average feature weights.
130  unordered_set<int> uids;
131  weights_.GetNonZeroFeatures(uids);
132  average_weights_.GetNonZeroFeatures(uids);
133  UpdateFeatureAverages(time, uids.begin(), uids.end());
134  }
135 
136  void RemapFeatureUids(const unordered_map<int, int> &old_to_new_uids) {
137  RemapFeatureUids(old_to_new_uids, weights_);
138  RemapFeatureUids(old_to_new_uids, average_weights_);
139  RemapFeatureUids(old_to_new_uids, weight_sums_);
140  RemapFeatureUids(old_to_new_uids, last_update_indices_);
141  }
142 
143  // I/O methods
144  friend ostream &operator<<(ostream &os, const TrainingVectorSet &tvs) {
145  os << "weights: " << tvs.weights_ << "\n"
146  << "average weights: " << tvs.average_weights_ << "\n"
147  << "weight sums: " << tvs.weight_sums_ << "\n"
148  << "last update indices: " << tvs.last_update_indices_ << "\n";
149  return os;
150  }
151 
152  private:
153 
154  template <typename V>
155  void RemapFeatureUids(const unordered_map<int, int> &old_to_new_uids,
156  FeatureVector<int, V> &vector) {
157  FeatureVector<int, V> old_vector = vector;
158  vector.clear();
159  typedef typename FeatureVector<int, V>::const_iterator fv_const_iterator;
160  for (fv_const_iterator old_vector_it = old_vector.begin();
161  old_vector_it != old_vector.end();
162  ++old_vector_it) {
163  int old_uid = old_vector_it->first;
164  unordered_map<int, int>::const_iterator old_to_new_uid_it =
165  old_to_new_uids.find(old_uid);
166  if (old_to_new_uid_it != old_to_new_uids.end()) {
167  int new_uid = old_to_new_uid_it->second;
168  V old_value = old_vector_it->second;
169  vector.SetWeight(new_uid, old_value);
170  }
171  }
172  }
173 
177  void UpdateAverage(const Time &time, int uid) {
178  int iterations_since_update =
179  time.absolute_index() - last_update_indices_.GetValue(uid);
180  if (iterations_since_update <= 0) {
181  return;
182  }
183  // Add in perceptron values to weight sum.
184  double add_to_sum = iterations_since_update * weights_.GetWeight(uid);
185  double new_weight_sum = weight_sums_.IncrementWeight(uid, add_to_sum);
186  average_weights_.SetWeight(uid, new_weight_sum / time.absolute_index());
187  last_update_indices_.SetValue(uid, time.absolute_index());
188  }
189 
191  template <typename Iterator>
192  void UpdateFeatureAverages(const Time &time,
193  Iterator feature_uids_begin_it,
194  Iterator feature_uids_end_it) {
195  for (Iterator it = feature_uids_begin_it; it != feature_uids_end_it; ++it) {
196  UpdateAverage(time, *it);
197  }
198  }
199 
200  // data members
201  FeatureVector<int,double> weights_;
202  FeatureVector<int,double> average_weights_;
203  FeatureVector<int,double> weight_sums_;
204  FeatureVector<int,int> last_update_indices_;
205 };
206 
207 } // namespace reranker
208 
209 #endif
void UpdateAllFeatureAverages(const Time &time)
A simple class to hold the three notions of time during training: the current epoch, the current time index within the current epoch, and the absolute time index.
Definition: training-time.H:56
void RemapFeatureUids(const unordered_map< int, int > &old_to_new_uids)
V GetWeight(const K &uid) const
Returns the weight of the feature with the specified uid, where crucially features not "present" in t...
const_iterator end() const
Returns a const iterator pointing to the end of the feature-value pairs of this feature vector...
const_iterator begin() const
Returns a const iterator pointing to the first of the feature-value pairs of this feature vector...
V SetValue(const K &uid, V new_value)
Synonym for SetWeight.
TrainingVectorSet()
Constructs a new set of feature vectors (models) for use during training.
A class to construct a PerceptronModel from a ModelMessage instance.
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.
const FeatureVector< int, double > & average_weights() const
Returns the feature vector corresponding to the averaged perceptron.
FeatureMap::const_iterator const_iterator
The type of const iterator for the feature-weight pairs in this vector.
A class to hold the several feature vectors needed during training (especially for the perceptron fam...
V SetWeight(const K &uid, V new_weight)
Sets the weight of the specified feature to the specified value.
const FeatureVector< int, double > & weights() const
Returns the "raw" feature weights computed during training.
V IncrementWeight(const K &uid, V by)
Increments the weight of the specified feature by the specified amount.
Defines the reranker::FeatureVector class, which, as it happens, is used to store feature vectors...
const FeatureVector< int, double > & GetModel(bool raw) const
Returns either the raw or averaged feature vector, depending on the argument.
V GetValue(const K &uid) const
Synonymous with GetWeight.
void UpdateGoldAndCandidateFeatureAverages(const Time &time, const Collection &gold_feature_uids, const Collection &candidate_feature_uids)
Updates the feature averages the specified pair of feature uid collections, one for a gold reference ...
unordered_set< K > & GetNonZeroFeatures(unordered_set< K > &set) const
Inserts the uid's of features with non-zero weights into the specified set.
void UpdateWeights(const Time &time, const Collection &feature_uids, const FeatureVector< int, double > &feature_vector, double scalar)
Increments the weights for the specified collection of features.
friend ostream & operator<<(ostream &os, const TrainingVectorSet &tvs)
void clear()
Sets all feature weights to zero and, because this is a sparse vector, clears all storage...
virtual ~TrainingVectorSet()
Destroys this instance.
Provides the reranker::Time class, which holds the three notions of training time: current epoch...