36 #ifndef RERANKER_TRAINING_VECTOR_SET_H_
37 #define RERANKER_TRAINING_VECTOR_SET_H_
40 #include <unordered_map>
41 #include <unordered_set>
50 using std::unordered_map;
51 using std::unordered_set;
75 return average_weights_;
103 template <
typename Collection>
105 const Collection &feature_uids,
114 template <
typename 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());
130 unordered_set<int> uids;
133 UpdateFeatureAverages(time, uids.begin(), uids.end());
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";
154 template <
typename V>
160 for (fv_const_iterator old_vector_it = old_vector.
begin();
161 old_vector_it != old_vector.
end();
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;
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) {
184 double add_to_sum = iterations_since_update * weights_.
GetWeight(uid);
186 average_weights_.
SetWeight(uid, new_weight_sum / time.absolute_index());
187 last_update_indices_.
SetValue(uid, time.absolute_index());
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);
201 FeatureVector<int,double> weights_;
202 FeatureVector<int,double> average_weights_;
203 FeatureVector<int,double> weight_sums_;
204 FeatureVector<int,int> last_update_indices_;
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.
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...