static std::pair<T, T>
minmax(const dlib::matrix<T> &matrix) {
    T maxValue = matrix(0, 0);
    T minValue = matrix(0, 0);
    for (long i=0; i<matrix.nr(); ++i) {
        for (long j=0; j<matrix.nc(); ++j) {
            maxValue = std::max(maxValue, matrix(i, j));
            minValue = std::min(minValue, matrix(i, j));
        }
    }
    return std::make_pair(minValue, maxValue);
}
示例#2
0
double CrossEntropyCounts::operator() ( const dlib::matrix<double,0,1>& arg) const
{
  double total = 0.0;
  double n = 0.0;
  std::vector<float> weight_vector (m_model->m_numModels);

  for (int i=0; i < arg.nr(); i++) {
    weight_vector[i] = arg(i);
  }
  if (m_model->m_mode == "interpolate") {
    weight_vector = m_model->normalizeWeights(weight_vector);
  }

  for ( std::vector<multiModelCountsStatisticsOptimization*>::const_iterator iter = m_optimizerStats.begin(); iter != m_optimizerStats.end(); ++iter ) {
    multiModelCountsStatisticsOptimization* statistics = *iter;
    size_t f = statistics->f;

    double score;
    if (m_iFeature == 0) {
      score = m_model->m_combineFunction(statistics->fst, statistics->ft, weight_vector);
    } else if (m_iFeature == 1) {
      score = m_model->ComputeWeightedLexicalTranslationFromCache(statistics->lexCachee2f, weight_vector);
    } else if (m_iFeature == 2) {
      score = m_model->m_combineFunction(statistics->fst, statistics->fs, weight_vector);
    } else if (m_iFeature == 3) {
      score = m_model->ComputeWeightedLexicalTranslationFromCache(statistics->lexCachef2e, weight_vector);
    } else {
      score = 0;
      UTIL_THROW(util::Exception, "Trying to optimize feature that I don't know. Aborting");
    }
    total -= (FloorScore(TransformScore(score))/TransformScore(2))*f;
    n += f;
  }
  return total/n;
}
double CrossEntropy::operator() ( const dlib::matrix<double,0,1>& arg) const
{
  double total = 0.0;
  double n = 0.0;
  std::vector<float> weight_vector (m_model->m_numModels);

  for (int i=0; i < arg.nr(); i++) {
    weight_vector[i] = arg(i);
  }
  if (m_model->m_mode == "interpolate") {
    weight_vector = m_model->normalizeWeights(weight_vector);
  }

  for ( std::vector<multiModelStatisticsOptimization*>::const_iterator iter = m_optimizerStats.begin(); iter != m_optimizerStats.end(); ++iter ) {
    multiModelStatisticsOptimization* statistics = *iter;
    size_t f = statistics->f;

    double score;
    score = std::inner_product(statistics->p[m_iFeature].begin(), statistics->p[m_iFeature].end(), weight_vector.begin(), 0.0);

    total -= (FloorScore(TransformScore(score))/TransformScore(2))*f;
    n += f;
  }
  return total/n;
}
示例#4
0
Double Callback::operator()(const ::dlib::matrix<double, 0, 1>& Parameters) const {
  // Update our Components with the New Parameters
  vector<Estimate*> estimates = model_->managers().estimate()->GetIsEstimated();

  if (Parameters.size() != (int)estimates.size()) {
    LOG_CODE_ERROR() << "The number of enabled estimates does not match the number of test solution values";
  }

  double penalty = 0;
  for (int i = 0; i < Parameters.size(); ++i) {
    Double value = utilities::math::unscale_value(Parameters(i), penalty, estimates[i]->lower_bound(), estimates[i]->upper_bound());
    estimates[i]->set_value(value);
  }

  model_->managers().estimate_transformation()->RestoreEstimates();
  model_->FullIteration();
  LOG_MEDIUM() << "Iteration Complete";
  ObjectiveFunction& objective = model_->objective_function();
  objective.CalculateScore();

  model_->managers().estimate_transformation()->TransformEstimates();
  return objective.score() + penalty;
}
static void
munkresCost(const dlib::matrix<double> &src, T scale, dlib::matrix<T> &dst /*out*/) {
    ASSERT_require(dst.nr()==src.nr() && dst.nc()==src.nc());
    std::pair<double, double> range = minmax(src);
    if (range.first==range.second) {
        for (long i=0; i<src.nr(); ++i) {
            for (long j=0; j<dst.nc(); ++j)
                dst(i, j) = 0;
        }
    } else {
        for (long i=0; i<src.nr(); ++i) {
            for (long j=0; j<dst.nc(); ++j)
                dst(i, j) = round(((range.second-src(i, j)) / (range.second-range.first)) * scale);
        }
    }
}