| 1 |
/** @file svm.h |
| 2 |
** @brief Support Vector Machines (@ref svm) |
| 3 |
** @author Milan Sulc |
| 4 |
** @author Daniele Perrone |
| 5 |
** @author Andrea Vedaldi |
| 6 |
**/ |
| 7 |
|
| 8 |
/* |
| 9 |
Copyright (C) 2013 Milan Sulc. |
| 10 |
Copyright (C) 2012 Daniele Perrone. |
| 11 |
Copyright (C) 2011-13 Andrea Vedaldi. |
| 12 |
|
| 13 |
All rights reserved. |
| 14 |
|
| 15 |
This file is part of the VLFeat library and is made available under |
| 16 |
the terms of the BSD license (see the COPYING file). |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef VL_SVM_H |
| 20 |
#define VL_SVM_H |
| 21 |
|
| 22 |
#include "generic.h" |
| 23 |
#include "svmdataset.h" |
| 24 |
|
| 25 |
/** @typedef VlSvm |
| 26 |
** @brief SVM solver. |
| 27 |
** This object implements VLFeat SVM solvers (see @ref svm.h). |
| 28 |
**/ |
| 29 |
|
| 30 |
#ifndef __DOXYGEN__ |
| 31 |
struct VlSvm_ ; |
| 32 |
typedef struct VlSvm_ VlSvm ; |
| 33 |
#else |
| 34 |
typedef OPAQUE VlSvm ; |
| 35 |
#endif |
| 36 |
|
| 37 |
/** @brief Type of SVM solver */ |
| 38 |
typedef enum |
| 39 |
{ |
| 40 |
VlSvmSolverNone = 0, /**< No solver (used to evaluate an SVM). */ |
| 41 |
VlSvmSolverSgd = 1, /**< SGD algorithm (@ref svm-sgd). */ |
| 42 |
VlSvmSolverSdca /**< SDCA algorithm (@ref svm-sdca). */ |
| 43 |
} VlSvmSolverType ; |
| 44 |
|
| 45 |
/** @brief Type of SVM loss |
| 46 |
** |
| 47 |
** Default SVM loss types. The loss can be set by using ::vl_svm_set_loss. |
| 48 |
** Note that custom losses can be used too by using ::vl_svm_set_loss_function, |
| 49 |
** ::vl_svm_set_loss_derivative_function, etc. |
| 50 |
** |
| 51 |
** @sa svm-loss-functions |
| 52 |
**/ |
| 53 |
typedef enum |
| 54 |
{ |
| 55 |
VlSvmLossHinge = 0, /**< Standard hinge loss. */ |
| 56 |
VlSvmLossHinge2 = 1, /**< Hinge loss squared. */ |
| 57 |
VlSvmLossL1, /**< L1 loss. */ |
| 58 |
VlSvmLossL2, /**< L2 loss. */ |
| 59 |
VlSvmLossLogistic /**< Logistic loss. */ |
| 60 |
} VlSvmLossType ; |
| 61 |
|
| 62 |
/** @brief Solver status */ |
| 63 |
typedef enum |
| 64 |
{ |
| 65 |
VlSvmStatusTraining = 1, /**< Optimization in progress. */ |
| 66 |
VlSvmStatusConverged, /**< Optimization finished because the convergence criterion was met. */ |
| 67 |
VlSvmStatusMaxNumIterationsReached /**< Optimization finished without convergence. */ |
| 68 |
} VlSvmSolverStatus ; |
| 69 |
|
| 70 |
/** @brief SVM statistics |
| 71 |
** This structure contains statistics characterising the state of |
| 72 |
** the SVM solver, such as the current value of the objective function. |
| 73 |
** |
| 74 |
** Not all fields are used by all solvers. |
| 75 |
**/ |
| 76 |
typedef struct VlSvmStatistics_ { |
| 77 |
VlSvmSolverStatus status ; /**< Solver status. */ |
| 78 |
vl_size iteration ; /**< Solver iteration. */ |
| 79 |
vl_size epoch ; /**< Solver epoch (iteration / num samples). */ |
| 80 |
double objective ; /**< Objective function value. */ |
| 81 |
double regularizer ; /**< Regularizer value. */ |
| 82 |
double loss ; /**< Loss value. */ |
| 83 |
double dualObjective ; /**< Dual objective value. */ |
| 84 |
double dualLoss ; /**< Dual loss value. */ |
| 85 |
double dualityGap ; /**< Duality gap = objective - dualObjective. */ |
| 86 |
double scoresVariation ; /**< Variance of the score updates. */ |
| 87 |
double elapsedTime ; /**< Time elapsed from the start of training. */ |
| 88 |
} VlSvmStatistics ; |
| 89 |
|
| 90 |
/** @name Create and destroy |
| 91 |
** @{ */ |
| 92 |
VL_EXPORT VlSvm * vl_svm_new (VlSvmSolverType type, |
| 93 |
double const * data, |
| 94 |
vl_size dimension, |
| 95 |
vl_size numData, |
| 96 |
double const * labels, |
| 97 |
double lambda) ; |
| 98 |
|
| 99 |
VL_EXPORT VlSvm * vl_svm_new_with_dataset (VlSvmSolverType type, |
| 100 |
VlSvmDataset * dataset, |
| 101 |
double const * labels, |
| 102 |
double lambda) ; |
| 103 |
|
| 104 |
VL_EXPORT VlSvm * vl_svm_new_with_abstract_data (VlSvmSolverType type, |
| 105 |
void * data, |
| 106 |
vl_size dimension, |
| 107 |
vl_size numData, |
| 108 |
double const * labels, |
| 109 |
double lambda) ; |
| 110 |
|
| 111 |
VL_EXPORT void vl_svm_delete (VlSvm * self) ; |
| 112 |
/** @} */ |
| 113 |
|
| 114 |
/** @name Retrieve parameters and data |
| 115 |
** @{ */ |
| 116 |
VL_EXPORT VlSvmStatistics const * vl_svm_get_statistics (VlSvm const *self) ; |
| 117 |
VL_EXPORT double const * vl_svm_get_model (VlSvm const *self) ; |
| 118 |
VL_EXPORT double vl_svm_get_bias (VlSvm const *self) ; |
| 119 |
VL_EXPORT vl_size vl_svm_get_dimension (VlSvm *self) ; |
| 120 |
VL_EXPORT vl_size vl_svm_get_num_data (VlSvm *self) ; |
| 121 |
VL_EXPORT double vl_svm_get_epsilon (VlSvm const *self) ; |
| 122 |
VL_EXPORT double vl_svm_get_bias_learning_rate (VlSvm const *self) ; |
| 123 |
VL_EXPORT vl_size vl_svm_get_max_num_iterations (VlSvm const *self) ; |
| 124 |
VL_EXPORT vl_size vl_svm_get_diagnostic_frequency (VlSvm const *self) ; |
| 125 |
VL_EXPORT VlSvmSolverType vl_svm_get_solver (VlSvm const *self) ; |
| 126 |
VL_EXPORT double vl_svm_get_bias_multiplier (VlSvm const *self) ; |
| 127 |
VL_EXPORT double vl_svm_get_lambda (VlSvm const *self) ; |
| 128 |
VL_EXPORT vl_size vl_svm_get_iteration_number (VlSvm const *self) ; |
| 129 |
VL_EXPORT double const * vl_svm_get_scores (VlSvm const *self) ; |
| 130 |
VL_EXPORT double const * vl_svm_get_weights (VlSvm const *self) ; |
| 131 |
/** @} */ |
| 132 |
|
| 133 |
/** @name Set parameters |
| 134 |
** @{ */ |
| 135 |
VL_EXPORT void vl_svm_set_epsilon (VlSvm *self, double epsilon) ; |
| 136 |
VL_EXPORT void vl_svm_set_bias_learning_rate (VlSvm *self, double rate) ; |
| 137 |
VL_EXPORT void vl_svm_set_max_num_iterations (VlSvm *self, vl_size maxNumIterations) ; |
| 138 |
VL_EXPORT void vl_svm_set_diagnostic_frequency (VlSvm *self, vl_size f) ; |
| 139 |
VL_EXPORT void vl_svm_set_bias_multiplier (VlSvm *self, double b) ; |
| 140 |
VL_EXPORT void vl_svm_set_model (VlSvm *self, double const *model) ; |
| 141 |
VL_EXPORT void vl_svm_set_bias (VlSvm *self, double b) ; |
| 142 |
VL_EXPORT void vl_svm_set_iteration_number (VlSvm *self, vl_uindex n) ; |
| 143 |
VL_EXPORT void vl_svm_set_weights (VlSvm *self, double const *weights) ; |
| 144 |
|
| 145 |
VL_EXPORT void vl_svm_set_diagnostic_function (VlSvm *self, VlSvmDiagnosticFunction f, void *data) ; |
| 146 |
VL_EXPORT void vl_svm_set_loss_function (VlSvm *self, VlSvmLossFunction f) ; |
| 147 |
VL_EXPORT void vl_svm_set_loss_derivative_function (VlSvm *self, VlSvmLossFunction f) ; |
| 148 |
VL_EXPORT void vl_svm_set_conjugate_loss_function (VlSvm *self, VlSvmLossFunction f) ; |
| 149 |
VL_EXPORT void vl_svm_set_dca_update_function (VlSvm *self, VlSvmDcaUpdateFunction f) ; |
| 150 |
VL_EXPORT void vl_svm_set_data_functions (VlSvm *self, VlSvmInnerProductFunction inner, VlSvmAccumulateFunction acc) ; |
| 151 |
VL_EXPORT void vl_svm_set_loss (VlSvm *self, VlSvmLossType loss) ; |
| 152 |
/** @} */ |
| 153 |
|
| 154 |
/** @name Process data |
| 155 |
** @{ */ |
| 156 |
VL_EXPORT void vl_svm_train (VlSvm * self) ; |
| 157 |
/** @} */ |
| 158 |
|
| 159 |
/** @name Loss functions |
| 160 |
** @sa @ref svm-advanced |
| 161 |
** @{ */ |
| 162 |
|
| 163 |
/* hinge */ |
| 164 |
VL_EXPORT double vl_svm_hinge_loss (double label, double inner) ; |
| 165 |
VL_EXPORT double vl_svm_hinge_loss_derivative (double label, double inner) ; |
| 166 |
VL_EXPORT double vl_svm_hinge_conjugate_loss (double label, double u) ; |
| 167 |
VL_EXPORT double vl_svm_hinge_dca_update (double alpha, double inner, double norm2, double label) ; |
| 168 |
|
| 169 |
/* square hinge */ |
| 170 |
VL_EXPORT double vl_svm_hinge2_loss (double label, double inner) ; |
| 171 |
VL_EXPORT double vl_svm_hinge2_loss_derivative (double label, double inner) ; |
| 172 |
VL_EXPORT double vl_svm_hinge2_conjugate_loss (double label, double u) ; |
| 173 |
VL_EXPORT double vl_svm_hinge2_dca_update (double alpha, double inner, double norm2, double label) ; |
| 174 |
|
| 175 |
/* l1 */ |
| 176 |
VL_EXPORT double vl_svm_l1_loss (double label, double inner) ; |
| 177 |
VL_EXPORT double vl_svm_l1_loss_derivative (double label, double inner) ; |
| 178 |
VL_EXPORT double vl_svm_l1_conjugate_loss (double label, double u) ; |
| 179 |
VL_EXPORT double vl_svm_l1_dca_update (double alpha, double inner, double norm2, double label) ; |
| 180 |
|
| 181 |
/* l2 */ |
| 182 |
VL_EXPORT double vl_svm_l2_loss (double label, double inner) ; |
| 183 |
VL_EXPORT double vl_svm_l2_loss_derivative (double label, double inner) ; |
| 184 |
VL_EXPORT double vl_svm_l2_conjugate_loss (double label, double u) ; |
| 185 |
VL_EXPORT double vl_svm_l2_dca_update (double alpha, double inner, double norm2, double label) ; |
| 186 |
|
| 187 |
/* logistic */ |
| 188 |
VL_EXPORT double vl_svm_logistic_loss (double label, double inner) ; |
| 189 |
VL_EXPORT double vl_svm_logistic_loss_derivative (double label, double inner) ; |
| 190 |
VL_EXPORT double vl_svm_logistic_conjugate_loss (double label, double u) ; |
| 191 |
VL_EXPORT double vl_svm_logistic_dca_update (double alpha, double inner, double norm2, double label) ; |
| 192 |
/** } */ |
| 193 |
|
| 194 |
/* VL_SVM_H */ |
| 195 |
#endif |