diff --git a/src/mlpack/methods/logistic_regression/logistic_regression.hpp b/src/mlpack/methods/logistic_regression/logistic_regression.hpp index b02ac59f29..bc117b380b 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression.hpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression.hpp @@ -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& optimizer); + //! Return the parameters (the b vector). const arma::vec& Parameters() const { return parameters; } //! Modify the parameters (the b vector). diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp b/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp index d284fe8e4b..f09305f690 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp @@ -45,7 +45,21 @@ LogisticRegression::LogisticRegression( LearnModel(); } -template class OptimizerType> +template class OptimizerType> +LogisticRegression::LogisticRegression( + OptimizerType& 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 class OptimizerType> void LogisticRegression::Predict(const arma::mat& predictors, arma::vec& responses, const double decisionBoundary)