linear_svm_function.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_LINEAR_SVM_LINEAR_SVM_FUNCTION_HPP
15 #define MLPACK_METHODS_LINEAR_SVM_LINEAR_SVM_FUNCTION_HPP
16 
17 #include <mlpack/prereqs.hpp>
18 
19 namespace mlpack {
20 namespace svm {
21 
27 template <typename MatType = arma::mat>
29 {
30  public:
41  LinearSVMFunction(const MatType& dataset,
42  const arma::Row<size_t>& labels,
43  const size_t numClasses,
44  const double lambda = 0.0001,
45  const double delta = 1.0,
46  const bool fitIntercept = false);
47 
51  void Shuffle();
52 
63  static void InitializeWeights(arma::mat& weights,
64  const size_t featureSize,
65  const size_t numClasses,
66  const bool fitIntercept = false);
67 
74  void GetGroundTruthMatrix(const arma::Row<size_t>& labels,
75  arma::sp_mat& groundTruth);
76 
83  double Evaluate(const arma::mat& parameters);
84 
94  double Evaluate(const arma::mat& parameters,
95  const size_t firstId,
96  const size_t batchSize = 1);
97 
106  template <typename GradType>
107  void Gradient(const arma::mat& parameters,
108  GradType& gradient);
109 
120  template <typename GradType>
121  void Gradient(const arma::mat& parameters,
122  const size_t firstId,
123  GradType& gradient,
124  const size_t batchSize = 1);
125 
137  template <typename GradType>
138  double EvaluateWithGradient(const arma::mat& parameters,
139  GradType& gradient) const;
140 
155  template <typename GradType>
156  double EvaluateWithGradient(const arma::mat& parameters,
157  const size_t firstId,
158  GradType& gradient,
159  const size_t batchSize = 1) const;
160 
162  const arma::mat& InitialPoint() const { return initialPoint; }
164  arma::mat& InitialPoint() { return initialPoint; }
165 
167  const arma::sp_mat& Dataset() const { return dataset; }
169  arma::sp_mat& Dataset() { return dataset; }
170 
172  double& Lambda() { return lambda; }
174  double Lambda() const { return lambda; }
175 
177  bool FitIntercept() const { return fitIntercept; }
178 
180  size_t NumFunctions() const;
181 
182  private:
184  arma::mat initialPoint;
185 
187  arma::sp_mat groundTruth;
188 
190  MatType dataset;
191 
193  size_t numClasses;
194 
196  double lambda;
197 
199  double delta;
200 
202  bool fitIntercept;
203 };
204 
205 } // namespace svm
206 } // namespace mlpack
207 
208 // Include implementation
209 #include "linear_svm_function_impl.hpp"
210 
211 #endif // MLPACK_METHODS_LINEAR_SVM_LINEAR_SVM_FUNCTION_HPP
The hinge loss function for the linear SVM objective function.
LinearSVMFunction(const MatType &dataset, const arma::Row< size_t > &labels, const size_t numClasses, const double lambda=0.0001, const double delta=1.0, const bool fitIntercept=false)
Construct the Linear SVM objective function with given parameters.
double Lambda() const
Gets the regularization parameter.
static void InitializeWeights(arma::mat &weights, const size_t featureSize, const size_t numClasses, const bool fitIntercept=false)
Initialize Linear SVM weights (trainable parameters) with the given parameters.
strip_type.hpp
Definition: add_to_po.hpp:21
bool FitIntercept() const
Gets the intercept flag.
The core includes that mlpack expects; standard C++ includes and Armadillo.
double EvaluateWithGradient(const arma::mat &parameters, GradType &gradient) const
Evaluate the gradient of the hinge loss function, following the LinearFunctionType requirements on th...
arma::mat & InitialPoint()
Modify the initial point for the optimization.
arma::sp_mat & Dataset()
Modify the dataset.
void Shuffle()
Shuffle the dataset.
size_t NumFunctions() const
Return the number of functions.
void GetGroundTruthMatrix(const arma::Row< size_t > &labels, arma::sp_mat &groundTruth)
Constructs the ground truth label matrix with the passed labels.
const arma::mat & InitialPoint() const
Return the initial point for the optimization.
double & Lambda()
Sets the regularization parameter.
void Gradient(const arma::mat &parameters, GradType &gradient)
Evaluate the gradient of the hinge loss function following the LinearFunctionType requirements on the...
double Evaluate(const arma::mat &parameters)
Evaluate the hinge loss function for all the datapoints.
const arma::sp_mat & Dataset() const
Get the dataset.