LogisticFunction is incorrect; it's actually the objective function for logistic

regression, which is not the logistic function.  Also remove unnecessary
overloads in LogisticRegressionFunction.
This commit is contained in:
Ryan Curtin
2013-11-11 17:33:28 +00:00
parent 05bc80f9c0
commit 91ce8b07ea
4 changed files with 29 additions and 38 deletions
@@ -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
@@ -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 {
@@ -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 @@ class LogisticFunction
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 @@ class LogisticFunction
}; // namespace regression
}; // namespace mlpack
#endif // __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_FUNCTION_HPP
#endif // __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_FUNCTION_HPP
@@ -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 @@ LogisticFunction::LogisticFunction(
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 @@ LogisticFunction::LogisticFunction(
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 @@ double LogisticFunction::Evaluate(
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 @@ void LogisticFunction::Gradient(
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