mlpack
softmax_regression_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_SOFTMAX_REGRESSION_SOFTMAX_REGRESSION_IMPL_HPP
13 #define MLPACK_METHODS_SOFTMAX_REGRESSION_SOFTMAX_REGRESSION_IMPL_HPP
14 
15 // In case it hasn't been included yet.
16 #include "softmax_regression.hpp"
17 
18 namespace mlpack {
19 namespace regression {
20 
21 template<typename OptimizerType>
23  const arma::mat& data,
24  const arma::Row<size_t>& labels,
25  const size_t numClasses,
26  const double lambda,
27  const bool fitIntercept,
28  OptimizerType optimizer) :
29  numClasses(numClasses),
30  lambda(lambda),
31  fitIntercept(fitIntercept)
32 {
33  Train(data, labels, numClasses, optimizer);
34 }
35 
36 template<typename OptimizerType, typename... CallbackTypes>
38  const arma::mat& data,
39  const arma::Row<size_t>& labels,
40  const size_t numClasses,
41  const double lambda,
42  const bool fitIntercept,
43  OptimizerType optimizer,
44  CallbackTypes&&... callbacks) :
45  numClasses(numClasses),
46  lambda(lambda),
47  fitIntercept(fitIntercept)
48 {
49  Train(data, labels, numClasses, optimizer, callbacks...);
50 }
51 
52 template<typename VecType>
53 size_t SoftmaxRegression::Classify(const VecType& point) const
54 {
55  arma::Row<size_t> label(1);
56  Classify(point, label);
57  return size_t(label(0));
58 }
59 
60 template<typename OptimizerType>
61 double SoftmaxRegression::Train(const arma::mat& data,
62  const arma::Row<size_t>& labels,
63  const size_t numClasses,
64  OptimizerType optimizer)
65 {
66  SoftmaxRegressionFunction regressor(data, labels, numClasses, lambda,
67  fitIntercept);
68  if (parameters.n_elem != regressor.GetInitialPoint().n_elem)
69  parameters = regressor.GetInitialPoint();
70 
71  // Train the model.
72  Timer::Start("softmax_regression_optimization");
73  const double out = optimizer.Optimize(regressor, parameters);
74  Timer::Stop("softmax_regression_optimization");
75 
76  Log::Info << "SoftmaxRegression::SoftmaxRegression(): final objective of "
77  << "trained model is " << out << "." << std::endl;
78 
79  return out;
80 }
81 
82 template<typename OptimizerType, typename... CallbackTypes>
83 double SoftmaxRegression::Train(const arma::mat& data,
84  const arma::Row<size_t>& labels,
85  const size_t numClasses,
86  OptimizerType optimizer,
87  CallbackTypes&&... callbacks)
88 {
89  SoftmaxRegressionFunction regressor(data, labels, numClasses, lambda,
90  fitIntercept);
91  if (parameters.n_elem != regressor.GetInitialPoint().n_elem)
92  parameters = regressor.GetInitialPoint();
93 
94  // Train the model.
95  Timer::Start("softmax_regression_optimization");
96  const double out = optimizer.Optimize(regressor, parameters, callbacks...);
97  Timer::Stop("softmax_regression_optimization");
98 
99  Log::Info << "SoftmaxRegression::SoftmaxRegression(): final objective of "
100  << "trained model is " << out << "." << std::endl;
101 
102  return out;
103 }
104 
105 } // namespace regression
106 } // namespace mlpack
107 
108 #endif
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
static void Stop(const std::string &name)
Stop the given timer.
Definition: timers.cpp:36
void Classify(const arma::mat &dataset, arma::Row< size_t > &labels) const
Classify the given points, returning the predicted labels for each point.
Definition: softmax_regression.cpp:30
SoftmaxRegression(const size_t inputSize=0, const size_t numClasses=0, const bool fitIntercept=false)
Initialize the SoftmaxRegression without performing training.
Definition: softmax_regression.cpp:19
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
Definition: softmax_regression_function.hpp:21
double Train(const arma::mat &data, const arma::Row< size_t > &labels, const size_t numClasses, OptimizerType optimizer=OptimizerType())
Train the softmax regression with the given training data.
Definition: softmax_regression_impl.hpp:61
const arma::mat & GetInitialPoint() const
Return the initial point for the optimization.
Definition: softmax_regression_function.hpp:167