Example #1
0
void
PerceptronModel::DefaultUpdater::Update(Model *m, CandidateSet &example) {
  PerceptronModel *model = dynamic_cast<PerceptronModel *>(m);
  ++(model->num_updates_);
  unordered_set<int> gold_features;
  unordered_set<int> best_scoring_features;
  model->ComputeFeaturesToUpdate(example, gold_features, best_scoring_features);

  model->models_.UpdateGoldAndCandidateFeatureAverages(model->time_,
                                                       gold_features,
                                                       best_scoring_features);
  double step_size =
      model->ComputeStepSize(gold_features, best_scoring_features, example);

  // Finally, update perceptrons.

  if (DEBUG >= 2) {
    cerr << "Updating weights for gold features [";
    for (unordered_set<int>::const_iterator it = gold_features.begin();
         it != gold_features.end(); ++it) {
      cerr << " " << *it;
    }
    cerr << "] from\n\t" << example.GetGold() << endl;

    cerr << "Updating weights for best scoring features [";
    for (unordered_set<int>::const_iterator it = best_scoring_features.begin();
         it != best_scoring_features.end(); ++it) {
      cerr << " " << *it;
    }
    cerr << "] from\n\t" << example.GetBestScoring() << endl;

  }

  double positive_step = step_size;
  model->models_.UpdateWeights(model->time_, gold_features,
                               example.GetGold().features(), positive_step);
  double negative_step = -step_size;
  model->models_.UpdateWeights(model->time_, best_scoring_features,
                        example.GetBestScoring().features(), negative_step);

  if (DEBUG >=2) {
    cerr << "Raw model: " << model->models_.GetModel(true) << endl;
    cerr << "Avg model: " << model->models_.GetModel(false) << endl;
  }
}
Example #2
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;
  }

}