mlpack
logistic_regression_impl.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_IMPL_HPP
15 #define MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_IMPL_HPP
16 
17 // In case it hasn't been included yet.
18 #include "logistic_regression.hpp"
19 
20 namespace mlpack {
21 namespace regression {
22 
23 template<typename MatType>
25  const MatType& predictors,
26  const arma::Row<size_t>& responses,
27  const double lambda) :
28  lambda(lambda)
29 {
30  Train(predictors, responses);
31 }
32 
33 template<typename MatType>
35  const MatType& predictors,
36  const arma::Row<size_t>& responses,
37  const arma::rowvec& initialPoint,
38  const double lambda) :
39  parameters(initialPoint),
40  lambda(lambda)
41 {
42  Train(predictors, responses);
43 }
44 
45 template<typename MatType>
47  const size_t dimensionality,
48  const double lambda) :
49  parameters(arma::rowvec(dimensionality + 1, arma::fill::zeros)),
50  lambda(lambda)
51 {
52  // No training to do here.
53 }
54 
55 template<typename MatType>
56 template<typename OptimizerType>
58  const MatType& predictors,
59  const arma::Row<size_t>& responses,
60  OptimizerType& optimizer,
61  const double lambda) :
62  lambda(lambda)
63 {
64  Train(predictors, responses, optimizer);
65 }
66 
67 template<typename MatType>
68 template<typename OptimizerType, typename... CallbackTypes>
70  const MatType& predictors,
71  const arma::Row<size_t>& responses,
72  CallbackTypes&&... callbacks)
73 {
74  OptimizerType optimizer;
75  return Train(predictors, responses, optimizer, callbacks...);
76 }
77 
78 template<typename MatType>
79 template<typename OptimizerType, typename... CallbackTypes>
81  const MatType& predictors,
82  const arma::Row<size_t>& responses,
83  OptimizerType& optimizer,
84  CallbackTypes&&... callbacks)
85 {
86  LogisticRegressionFunction<MatType> errorFunction(predictors, responses,
87  lambda);
88 
89  // Set size of parameters vector according to the input data received.
90  if (parameters.n_elem != predictors.n_rows + 1)
91  parameters = arma::rowvec(predictors.n_rows + 1, arma::fill::zeros);
92 
93  Timer::Start("logistic_regression_optimization");
94  const double out = optimizer.Optimize(errorFunction, parameters,
95  callbacks...);
96  Timer::Stop("logistic_regression_optimization");
97 
98  Log::Info << "LogisticRegression::LogisticRegression(): final objective of "
99  << "trained model is " << out << "." << std::endl;
100 
101  return out;
102 }
103 
104 template<typename MatType>
105 template<typename VecType>
106 size_t LogisticRegression<MatType>::Classify(const VecType& point,
107  const double decisionBoundary)
108  const
109 {
110  return size_t(1.0 / (1.0 + std::exp(-parameters(0) - arma::dot(point,
111  parameters.tail_cols(parameters.n_elem - 1)))) +
112  (1.0 - decisionBoundary));
113 }
114 
115 template<typename MatType>
116 void LogisticRegression<MatType>::Classify(const MatType& dataset,
117  arma::Row<size_t>& labels,
118  const double decisionBoundary) const
119 {
120  // Calculate sigmoid function for each point. The (1.0 - decisionBoundary)
121  // term correctly sets an offset so that floor() returns 0 or 1 correctly.
122  labels = arma::conv_to<arma::Row<size_t>>::from((1.0 /
123  (1.0 + arma::exp(-parameters(0) -
124  parameters.tail_cols(parameters.n_elem - 1) * dataset))) +
125  (1.0 - decisionBoundary));
126 }
127 
128 template<typename MatType>
129 void LogisticRegression<MatType>::Classify(const MatType& dataset,
130  arma::mat& probabilities) const
131 {
132  // Set correct size of output matrix.
133  probabilities.set_size(2, dataset.n_cols);
134 
135  probabilities.row(1) = 1.0 / (1.0 + arma::exp(-parameters(0) -
136  parameters.tail_cols(parameters.n_elem - 1) * dataset));
137  probabilities.row(0) = 1.0 - probabilities.row(1);
138 }
139 
140 template<typename MatType>
142  const MatType& predictors,
143  const arma::Row<size_t>& responses) const
144 {
145  // Construct a new error function.
146  LogisticRegressionFunction<> newErrorFunction(predictors, responses,
147  lambda);
148 
149  return newErrorFunction.Evaluate(parameters);
150 }
151 
152 template<typename MatType>
154  const MatType& predictors,
155  const arma::Row<size_t>& responses,
156  const double decisionBoundary) const
157 {
158  // Predict responses using the current model.
159  arma::Row<size_t> tempResponses;
160  Classify(predictors, tempResponses, decisionBoundary);
161 
162  // Count the number of responses that were correct.
163  size_t count = 0;
164  for (size_t i = 0; i < responses.n_elem; ++i)
165  {
166  if (responses(i) == tempResponses(i))
167  count++;
168  }
169 
170  return (double) (count * 100) / responses.n_elem;
171 }
172 
173 template<typename MatType>
174 template<typename Archive>
176  const uint32_t /* version */)
177 {
178  ar(CEREAL_NVP(parameters));
179  ar(CEREAL_NVP(lambda));
180 }
181 
182 } // namespace regression
183 } // namespace mlpack
184 
185 #endif // MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_IMPL_HPP
The log-likelihood function for the logistic regression objective function.
Definition: logistic_regression_function.hpp:30
double ComputeError(const MatType &predictors, const arma::Row< size_t > &responses) const
Compute the error of the model.
Definition: logistic_regression_impl.hpp:141
static void Start(const std::string &name)
Start the given timer.
Definition: timers.cpp:28
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
double Evaluate(const arma::mat &parameters) const
Evaluate the logistic regression log-likelihood function with the given parameters.
Definition: logistic_regression_function_impl.hpp:69
size_t Classify(const VecType &point, const double decisionBoundary=0.5) const
Classify the given point.
Definition: logistic_regression_impl.hpp:106
double Train(const MatType &predictors, const arma::Row< size_t > &responses, CallbackTypes &&... callbacks)
Train the LogisticRegression model on the given input data.
Definition: logistic_regression_impl.hpp:69
void serialize(Archive &ar, const uint32_t)
Serialize the model.
Definition: logistic_regression_impl.hpp:175
LogisticRegression(const MatType &predictors, const arma::Row< size_t > &responses, const double lambda=0)
Construct the LogisticRegression class with the given labeled training data.
Definition: logistic_regression_impl.hpp:24
static void Stop(const std::string &name)
Stop the given timer.
Definition: timers.cpp:36
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
double ComputeAccuracy(const MatType &predictors, const arma::Row< size_t > &responses, const double decisionBoundary=0.5) const
Compute the accuracy of the model on the given predictors and responses, optionally using the given d...
Definition: logistic_regression_impl.hpp:153