Add a constructor that allows passing an instantiated optimizer.

This commit is contained in:
Ryan Curtin
2013-11-21 18:34:37 +00:00
parent ac9630f129
commit 5b85eb566d
2 changed files with 28 additions and 1 deletions
@@ -52,6 +52,19 @@ class LogisticRegression
const arma::mat& initialPoint,
const double lambda = 0);
/**
* Construct the LogisticRegression class with the given labeled training
* data. This will train the model. This overload takes an already
* instantiated optimizer (which holds the LogisticRegressionFunction error
* function, which must also be instantiated), so that the optimizer can be
* configured before the training is run by this constructor. The predictors
* and responses and initial point are all taken from the error function
* contained in the optimizer.
*
* @param optimizer Instantiated optimizer with instantiated error function.
*/
LogisticRegression(OptimizerType<LogisticRegressionFunction>& optimizer);
//! Return the parameters (the b vector).
const arma::vec& Parameters() const { return parameters; }
//! Modify the parameters (the b vector).
@@ -45,7 +45,21 @@ LogisticRegression<OptimizerType>::LogisticRegression(
LearnModel();
}
template <template<typename> class OptimizerType>
template<template<typename> class OptimizerType>
LogisticRegression<OptimizerType>::LogisticRegression(
OptimizerType<LogisticRegressionFunction>& optimizer) :
predictors(optimizer.Function().Predictors()),
responses(optimizer.Function().Responses()),
parameters(optimizer.Function().GetInitialPoint()),
errorFunction(optimizer.Function()),
optimizer(optimizer)
{
Timer::Start("logistic_regression_optimization");
const double out = optimizer.Optimize(parameters);
Timer::Stop("logistic_regression_optimization");
}
template<template<typename> class OptimizerType>
void LogisticRegression<OptimizerType>::Predict(const arma::mat& predictors,
arma::vec& responses,
const double decisionBoundary)