From b34bb0f90d5664e73db082612372722ebc021524 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Wed, 16 Sep 2015 03:30:45 +0000 Subject: [PATCH] Add and implement Train() methods. --- .../logistic_regression.hpp | 10 +++- .../logistic_regression_impl.hpp | 57 +++++++++++-------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/mlpack/methods/logistic_regression/logistic_regression.hpp b/src/mlpack/methods/logistic_regression/logistic_regression.hpp index 8ca67f50cd..a0cdf8f692 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression.hpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression.hpp @@ -96,14 +96,20 @@ class LogisticRegression template class OptimizerType = mlpack::optimization::L_BFGS > void Train(const MatType& predictors, - const arma::Row& responses, - const MatType& initialPoint); + const arma::Row& responses); /** * Train the LogisticRegression model with the given instantiated optimizer. * Using this overload allows configuring the instantiated optimizer before * training is performed. * + * Note that the initial point of the optimizer + * (optimizer.Function().GetInitialPoint()) will be used as the initial point + * of the optimization, overwriting any existing trained model. If you don't + * want to overwrite the existing model, set + * optimizer.Function().GetInitialPoint() to the current parameters vector, + * accessible via Parameters(). + * * @param optimizer Instantiated optimizer with instantiated error function. */ template< diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp b/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp index 608dfdef79..704d95da8f 100644 --- a/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp +++ b/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp @@ -23,17 +23,7 @@ LogisticRegression::LogisticRegression( parameters(arma::zeros(predictors.n_rows + 1)), lambda(lambda) { - LogisticRegressionFunction errorFunction(predictors, responses, - lambda); - OptimizerType> optimizer(errorFunction); - - // Train the model. - Timer::Start("logistic_regression_optimization"); - const double out = optimizer.Optimize(parameters); - Timer::Stop("logistic_regression_optimization"); - - Log::Info << "LogisticRegression::LogisticRegression(): final objective of " - << "trained model is " << out << "." << std::endl; + Train(predictors, responses); } template @@ -43,21 +33,10 @@ LogisticRegression::LogisticRegression( const arma::Row& responses, const arma::vec& initialPoint, const double lambda) : - parameters(arma::zeros(predictors.n_rows + 1)), + parameters(initialPoint), lambda(lambda) { - LogisticRegressionFunction errorFunction(predictors, responses, - lambda); - errorFunction.InitialPoint() = initialPoint; - OptimizerType> optimizer(errorFunction); - - // Train the model. - Timer::Start("logistic_regression_optimization"); - const double out = optimizer.Optimize(parameters); - Timer::Stop("logistic_regression_optimization"); - - Log::Info << "LogisticRegression::LogisticRegression(): final objective of " - << "trained model is " << out << "." << std::endl; + Train(predictors, responses); } template @@ -78,6 +57,36 @@ LogisticRegression::LogisticRegression( parameters(optimizer.Function().GetInitialPoint()), lambda(optimizer.Function().Lambda()) { + Train(optimizer); +} + +template +template class OptimizerType> +void LogisticRegression::Train(const MatType& predictors, + const arma::Row& responses) +{ + LogisticRegressionFunction errorFunction(predictors, responses, + lambda); + errorFunction.InitialPoint() = parameters; + OptimizerType> optimizer(errorFunction); + + // Train the model. + Timer::Start("logistic_regression_optimization"); + const double out = optimizer.Optimize(parameters); + Timer::Stop("logistic_regression_optimization"); + + Log::Info << "LogisticRegression::LogisticRegression(): final objective of " + << "trained model is " << out << "." << std::endl; +} + +template +template class OptimizerType> +void LogisticRegression::Train( + OptimizerType>& optimizer) +{ + // Everything is good. Just train the model. + parameters = optimizer.Function().GetInitialPoint(); + Timer::Start("logistic_regression_optimization"); const double out = optimizer.Optimize(parameters); Timer::Stop("logistic_regression_optimization");