[mlpack-git] master: Add another Train(); remove templated constructors. (f379ee2)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Wed Sep 16 17:30:07 EDT 2015
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/e67787e336136a9e46b2d502bd583b8aea2668a4...d6e9b1be05f6fa78ff56b86fb66332bbe039d8ae
>---------------------------------------------------------------
commit f379ee2dd5e68a07ec40be752675486928194e9e
Author: Ryan Curtin <ryan at ratml.org>
Date: Wed Sep 16 21:29:10 2015 +0000
Add another Train(); remove templated constructors.
C++ language restrictions prevent the templated constructors from being usable.
>---------------------------------------------------------------
f379ee2dd5e68a07ec40be752675486928194e9e
.../logistic_regression/logistic_regression.hpp | 46 +++++++++++++++-------
.../logistic_regression_impl.hpp | 9 ++---
2 files changed, 35 insertions(+), 20 deletions(-)
diff --git a/src/mlpack/methods/logistic_regression/logistic_regression.hpp b/src/mlpack/methods/logistic_regression/logistic_regression.hpp
index a0cdf8f..d828efc 100644
--- a/src/mlpack/methods/logistic_regression/logistic_regression.hpp
+++ b/src/mlpack/methods/logistic_regression/logistic_regression.hpp
@@ -16,6 +16,15 @@
namespace mlpack {
namespace regression {
+/**
+ * The LogisticRegression class implements an L2-regularized logistic regression
+ * model, and supports training with multiple optimizers and classification.
+ * The class supports different observation types via the MatType template
+ * parameter; for instance, logistic regression can be performed on sparse
+ * datasets by specifying arma::sp_mat as the MatType parameter.
+ *
+ * @tparam MatType Type of data matrix.
+ */
template<typename MatType = arma::mat>
class LogisticRegression
{
@@ -26,13 +35,15 @@ class LogisticRegression
* penalty parameter for L2-regularization. If not specified, it is set to 0,
* which results in standard (unregularized) logistic regression.
*
+ * It is not possible to set a custom optimizer with this constructor. Either
+ * use a constructor that does not train and call Train() with a custom
+ * optimizer type, or use the constructor that takes an instantiated
+ * optimizer. (This unfortunate situation is a language restriction of C++.)
+ *
* @param predictors Input training variables.
* @param responses Outputs resulting from input training variables.
* @param lambda L2-regularization parameter.
*/
- template<
- template<typename> class OptimizerType = mlpack::optimization::L_BFGS
- >
LogisticRegression(const MatType& predictors,
const arma::Row<size_t>& responses,
const double lambda = 0);
@@ -43,14 +54,16 @@ class LogisticRegression
* penalty parameter for L2-regularization. If not specified, it is set to 0,
* which results in standard (unregularized) logistic regression.
*
+ * It is not possible to set a custom optimizer with this constructor. Either
+ * use a constructor that does not train and call Train() with a custom
+ * optimizer type, or use the constructor that takes an instantiated
+ * optimizer. (This unfortunate situation is a language restriction of C++.)
+ *
* @param predictors Input training variables.
* @param responses Outputs results from input training variables.
* @param initialPoint Initial model to train with.
* @param lambda L2-regularization parameter.
*/
- template<
- template<typename> class OptimizerType = mlpack::optimization::L_BFGS
- >
LogisticRegression(const MatType& predictors,
const arma::Row<size_t>& responses,
const arma::vec& initialPoint,
@@ -66,9 +79,6 @@ class LogisticRegression
* @param dimensionality Dimensionality of the data.
* @param lambda L2-regularization parameter.
*/
- template<
- template<typename> class OptimizerType = mlpack::optimization::L_BFGS
- >
LogisticRegression(const size_t dimensionality,
const double lambda = 0);
@@ -83,14 +93,22 @@ class LogisticRegression
*
* @param optimizer Instantiated optimizer with instantiated error function.
*/
- template<
- template<typename> class OptimizerType = mlpack::optimization::L_BFGS
- >
+ template<template<typename> class OptimizerType>
LogisticRegression(
OptimizerType<LogisticRegressionFunction<MatType>>& optimizer);
/**
- * Train the LogisticRegression model on the given input data.
+ * Train the LogisticRegression model on the given input data. By default,
+ * the L-BFGS optimization algorithm is used, but others can be specified
+ * (such as mlpack::optimization::SGD).
+ *
+ * This will use the existing model parameters as a starting point for the
+ * optimization. If this is not what you want, then you should access the
+ * parameters vector directly with Parameters() and modify it as desired.
+ *
+ * @tparam OptimizerType Type of optimizer to use to train the model.
+ * @param predictors Input training variables.
+ * @param responses Outputs results from input training variables.
*/
template<
template<typename> class OptimizerType = mlpack::optimization::L_BFGS
@@ -179,7 +197,7 @@ class LogisticRegression
std::string ToString() const;
private:
- //! Vector of trained parameters.
+ //! Vector of trained parameters (size: dimensionality plus one).
arma::vec parameters;
//! L2-regularization penalty parameter.
double lambda;
diff --git a/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp b/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp
index 704d95d..ebfb392 100644
--- a/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp
+++ b/src/mlpack/methods/logistic_regression/logistic_regression_impl.hpp
@@ -15,7 +15,6 @@ namespace mlpack {
namespace regression {
template<typename MatType>
-template<template<typename> class OptimizerType>
LogisticRegression<MatType>::LogisticRegression(
const MatType& predictors,
const arma::Row<size_t>& responses,
@@ -23,11 +22,10 @@ LogisticRegression<MatType>::LogisticRegression(
parameters(arma::zeros<arma::vec>(predictors.n_rows + 1)),
lambda(lambda)
{
- Train<OptimizerType>(predictors, responses);
+ Train(predictors, responses);
}
template<typename MatType>
-template<template<typename> class OptimizerType>
LogisticRegression<MatType>::LogisticRegression(
const MatType& predictors,
const arma::Row<size_t>& responses,
@@ -36,15 +34,14 @@ LogisticRegression<MatType>::LogisticRegression(
parameters(initialPoint),
lambda(lambda)
{
- Train<OptimizerType>(predictors, responses);
+ Train(predictors, responses);
}
template<typename MatType>
-template<template<typename> class OptimizerType>
LogisticRegression<MatType>::LogisticRegression(
const size_t dimensionality,
const double lambda) :
- parameters(dimensionality),
+ parameters(dimensionality + 1 /* include intercept term */),
lambda(lambda)
{
// No training to do here.
More information about the mlpack-git
mailing list