示例#1
0
void
PerceptronModel::ComputeFeaturesToUpdate(const CandidateSet &example,
                                         unordered_set<int> &
                                         gold_features_to_update,
                                         unordered_set<int> &
                                         best_scoring_features_to_update)
    const {
  // Collect gold features that are not in best-scoring candidate.
  const FeatureVector<int,double> &gold_features =
      example.GetGold().features();
  const FeatureVector<int,double> &best_scoring_features =
      example.GetBestScoring().features();

  if (DEBUG >= 2) {
    cerr << "Gold index: " << example.gold_index()
         << "; best scoring index: " << example.best_scoring_index()
         << endl;
    cerr << "Original gold features: " << gold_features << endl
         << "Original best scoring features: " << best_scoring_features << endl;
  }

  gold_features.GetNonZeroFeatures(gold_features_to_update);
  best_scoring_features.RemoveEqualFeatures(gold_features,
                                            gold_features_to_update);

  if (DEBUG >= 2) {
    cerr << "Time:" << time_.to_string() << ": new gold features: [";
    for (unordered_set<int>::const_iterator it =
             gold_features_to_update.begin();
         it != gold_features_to_update.end();
         ++it) {
      cerr << " " << *it;
    }
    cerr << "]" << endl;
  }

  best_scoring_features.GetNonZeroFeatures(best_scoring_features_to_update);
  gold_features.RemoveEqualFeatures(best_scoring_features,
                                    best_scoring_features_to_update);
  if (DEBUG >= 2) {
    cerr << "Time:" << time_.to_string() << ": new best scoring features: [";
    for (unordered_set<int>::const_iterator it =
             best_scoring_features_to_update.begin();
         it != best_scoring_features_to_update.end();
         ++it) {
      cerr << " " << *it;
    }
    cerr << "]" << endl;
  }

}
示例#2
0
void
PerceptronModel::TrainOnExample(CandidateSet &example) {
  time_.Tick();

  if (symbols_ != NULL) {
    example.CompileFeatures(symbols_);
  }

  bool training = true;
  ScoreCandidates(example, training);

  if (NeedToUpdate(example)) {
    if (DEBUG >= 2) {
      cerr << "Time:" << time_.to_string() << ": need to update because "
           << "best scoring index " << example.best_scoring_index()
           << " is not equal to gold index " << example.gold_index() << endl;
    }
    ++(*num_training_errors_per_epoch_.rbegin());
    ++num_training_errors_;
    Update(example);
  }
}
示例#3
0
bool
PerceptronModel::DefaultUpdatePredicate::NeedToUpdate(Model *model,
                                                      CandidateSet &example) {
  return example.best_scoring_index() != example.gold_index();
}