[mlpack-svn] r16015 - mlpack/trunk/src/mlpack/methods/logistic_regression

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Nov 11 12:33:29 EST 2013


Author: rcurtin
Date: Mon Nov 11 12:33:28 2013
New Revision: 16015

Log:
LogisticFunction is incorrect; it's actually the objective function for logistic
regression, which is not the logistic function.  Also remove unnecessary
overloads in LogisticRegressionFunction.


Added:
   mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression_function.hpp
      - copied, changed from r16014, /mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_function.hpp
   mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression_function_impl.hpp
      - copied, changed from r16014, /mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_function_impl.hpp
Removed:
   mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_function.hpp
   mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_function_impl.hpp
Modified:
   mlpack/trunk/src/mlpack/methods/logistic_regression/CMakeLists.txt
   mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression.hpp

Modified: mlpack/trunk/src/mlpack/methods/logistic_regression/CMakeLists.txt
==============================================================================
--- mlpack/trunk/src/mlpack/methods/logistic_regression/CMakeLists.txt	(original)
+++ mlpack/trunk/src/mlpack/methods/logistic_regression/CMakeLists.txt	Mon Nov 11 12:33:28 2013
@@ -2,10 +2,10 @@
 # Anything not in this list will not be compiled into the output library
 # Do not include test programs here
 set(SOURCES
-  logistic_function.hpp
-  logistic_function_impl.hpp
   logistic_regression.hpp
   logistic_regression_impl.hpp
+  logistic_regression_function.hpp
+  logistic_regression_function_impl.hpp
 )
 
 # add directory name to sources

Modified: mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression.hpp	Mon Nov 11 12:33:28 2013
@@ -11,7 +11,7 @@
 #include <mlpack/core.hpp>
 #include <mlpack/core/optimizers/lbfgs/lbfgs.hpp>
 
-#include "logistic_function.hpp"
+#include "logistic_regression_function.hpp"
 
 namespace mlpack {
 namespace regression {

Copied: mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression_function.hpp (from r16014, /mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_function.hpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_function.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression_function.hpp	Mon Nov 11 12:33:28 2013
@@ -3,36 +3,31 @@
  * @author Sumedh Ghaisas
  *
  * Implementation of the logistic regression function, which is meant to be
- * optimized by a separate optimizer class that takes LogisticFunction
+ * optimized by a separate optimizer class that takes LogisticRegressionFunction
  * as its FunctionType class.
  */
-#ifndef __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_FUNCTION_HPP
-#define __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_FUNCTION_HPP
+#ifndef __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_FUNCTION_HPP
+#define __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_FUNCTION_HPP
 
 #include <mlpack/core.hpp>
 
 namespace mlpack {
 namespace regression {
 
-class LogisticFunction
+class LogisticRegressionFunction
 {
  public:
-  LogisticFunction(arma::mat& predictors,
+  LogisticRegressionFunction(arma::mat& predictors,
                              arma::vec& responses,
                              const double lambda = 0);
 
-  LogisticFunction(arma::mat& predictors,
+  LogisticRegressionFunction(arma::mat& predictors,
                              arma::vec& responses,
                              const arma::mat& initialPoint,
                              const double lambda = 0);
 
   arma::vec getSigmoid(const arma::vec& values) const;
 
-  //evaluates the logistic function with given parameters
-  double Evaluate(const arma::mat& predictors,
-                  const arma::vec& responses,
-                  const arma::mat& values) const;
-
   //!Return the initial point
   const arma::mat& InitialPoint() const { return initialPoint; }
   //! Modify the initial point
@@ -44,10 +39,7 @@
   double& Lambda() { return lambda; }
 
   //functions to optimize by l-bfgs
-  double Evaluate(const arma::mat& values) const
-  {
-      return Evaluate(predictors, responses, values);
-  }
+  double Evaluate(const arma::mat& values) const;
 
   void Gradient(const arma::mat& values, arma::mat& gradient);
 
@@ -77,4 +69,4 @@
 }; // namespace regression
 }; // namespace mlpack
 
-#endif // __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_FUNCTION_HPP
+#endif // __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_FUNCTION_HPP

Copied: mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression_function_impl.hpp (from r16014, /mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_function_impl.hpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_function_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/logistic_regression/logistic_regression_function_impl.hpp	Mon Nov 11 12:33:28 2013
@@ -2,10 +2,10 @@
  * @file logistic_regression_function_impl.hpp
  * @author Sumedh Ghaisas
  *
- * Implementation of hte LogisticFunction class.
+ * Implementation of hte LogisticRegressionFunction class.
  */
-#ifndef __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_FUNCTION_IMPL_HPP
-#define __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_FUNCTION_IMPL_HPP
+#ifndef __MLPACK_METHODS_LOGISTIC_REGRESSION_FUNCTION_IMPL_HPP
+#define __MLPACK_METHODS_LOGISTIC_REGRESSION_FUNCTION_IMPL_HPP
 
 // In case it hasn't been done yet.
 #include "logistic_regression_function.hpp"
@@ -13,7 +13,7 @@
 namespace mlpack {
 namespace regression {
 
-LogisticFunction::LogisticFunction(
+LogisticRegressionFunction::LogisticRegressionFunction(
     arma::mat& predictors,
     arma::vec& responses,
     const double lambda) :
@@ -24,7 +24,7 @@
   initialPoint = arma::zeros<arma::mat>(predictors.n_rows + 1, 1);
 }
 
-LogisticFunction::LogisticFunction(
+LogisticRegressionFunction::LogisticRegressionFunction(
     arma::mat& predictors,
     arma::vec& responses,
     const arma::mat& initialPoint,
@@ -35,26 +35,25 @@
     lambda(lambda)
 {
   //to check if initialPoint is compatible with predictors
-  if(initialPoint.n_rows != (predictors.n_rows + 1) || initialPoint.n_cols != 1)
-    this->initialPoint = arma::zeros<arma::mat>(predictors.n_rows + 1,1);
+  if (initialPoint.n_rows != (predictors.n_rows + 1) ||
+      initialPoint.n_cols != 1)
+    this->initialPoint = arma::zeros<arma::mat>(predictors.n_rows + 1, 1);
 }
 
-arma::vec LogisticFunction::getSigmoid(const arma::vec& values) const
+arma::vec LogisticRegressionFunction::getSigmoid(const arma::vec& values,
+                                       arma::vec& output) const
 {
   arma::vec out = arma::ones<arma::vec>(values.n_rows,1) /
       (arma::ones<arma::vec>(values.n_rows,1) + arma::exp(-values));
   return out;
 }
 
-double LogisticFunction::Evaluate(
-    const arma::mat& predictors,
-    const arma::vec& responses,
-    const arma::mat& values) const
+double LogisticRegressionFunction::Evaluate(const arma::mat& values) const
 {
-  size_t nCols = predictors.n_cols;
+  const size_t nCols = predictors.n_cols;
 
   //sigmoid = Sigmoid(X' * values)
-  arma::vec sigmoid = getSigmoid(arma::trans(predictors) * values);
+  arma::vec sigmoid = 1 / (1 + arma::exp(-(arma::trans(predictors) * values)));
 
   //l2-regularization(considering only values(2:end) in regularization
   arma::vec temp = arma::trans(values) * values;
@@ -69,9 +68,8 @@
       predictors.n_cols + regularization;
 }
 
-void LogisticFunction::Gradient(
-    const arma::mat& values,
-    arma::mat& gradient)
+void LogisticRegressionFunction::Gradient(const arma::mat& values,
+                                arma::mat& gradient)
 {
   //regularization
   arma::mat regularization = arma::zeros<arma::mat>(predictors.n_rows, 1);
@@ -79,8 +77,9 @@
       values.rows(1, predictors.n_rows - 1) / responses.n_rows;
 
   //gradient =
-  gradient = -(predictors * (responses - getSigmoid(arma::trans(predictors) *
-      values))) / responses.n_rows + regularization;
+  gradient = -(predictors * (responses
+      - (1 / (1 + arma::exp(-(arma::trans(predictors) * values))))
+      / responses.n_rows + regularization;
 }
 
 }; // namespace regression



More information about the mlpack-svn mailing list