void passive_aggressive_2::train(const common::sfv_t& sfv,
                                 const string& label) {
  check_touchable(label);

  labels_.get_model()->increment(label);

  string incorrect_label;
  float margin = calc_margin(sfv, label, incorrect_label);
  float loss = 1.f + margin;

  if (loss < 0.f) {
    storage_->register_label(label);
    return;
  }
  float sfv_norm = squared_norm(sfv);
  if (sfv_norm == 0.f) {
    storage_->register_label(label);
    return;
  }
  update_weight(
      sfv,
      loss / (2 * sfv_norm + 1 / (2 * config_.regularization_weight)),
      label,
      incorrect_label);
  touch(label);
}
Esempio n. 2
0
void linear_classifier::touch(const std::string& label) {
  check_touchable(label);

  if (unlearner_) {
    util::concurrent::scoped_lock unlearner_lk(unlearner_mutex_);
    unlearner_->touch(label);
  }
}
Esempio n. 3
0
void perceptron::train(const common::sfv_t& sfv, const std::string& label) {
  check_touchable(label);

  labels_.get_model()->increment(label);

  std::string predicted_label = classify(sfv);
  if (label == predicted_label) {
    return;
  }
  update_weight(sfv, 1.0, label, predicted_label);
  touch(label);
}
Esempio n. 4
0
bool linear_classifier::set_label(const string& label) {
  check_touchable(label);

  bool result = storage_->set_label(label);
  result = labels_.get_model()->add(label) && result;
  if (unlearner_ && result) {
    util::concurrent::scoped_lock unlearner_lk(unlearner_mutex_);
    result = unlearner_->touch(label);
  }

  return result;
}
void confidence_weighted::train(const common::sfv_t& sfv, const string& label) {
  check_touchable(label);

  const float C = config_.regularization_weight;
  string incorrect_label;
  float variance = 0.f;
  float margin = -calc_margin_and_variance(sfv, label, incorrect_label,
                                           variance);
  float b = 1.f + 2 * C * margin;
  float gamma = -b + std::sqrt(b * b - 8 * C * (margin - C * variance));

  if (gamma <= 0.f) {
    storage_->register_label(label);
    return;
  }
  gamma /= 4 * C * variance;
  update(sfv, gamma, label, incorrect_label);
}
Esempio n. 6
0
void passive_aggressive_1::train(const common::sfv_t& sfv,
                                 const string& label) {
  check_touchable(label);

  string incorrect_label;
  float margin = calc_margin(sfv, label, incorrect_label);
  float loss = 1.f + margin;
  if (loss < 0.f) {
    storage_->register_label(label);
    return;
  }
  float sfv_norm = squared_norm(sfv);
  if (sfv_norm == 0.f) {
    storage_->register_label(label);
    return;
  }

  update_weight(
      sfv, min(config_.C, loss / (2 * sfv_norm)), label, incorrect_label);
  touch(label);
}