Class cv::ml::SVMSGD#
Stochastic Gradient Descent SVM classifier. View details
#include <opencv2/ml.hpp>Collaboration diagram for cv::ml::SVMSGD:
Public Types#
enum MarginType {
SOFT_MARGIN,
HARD_MARGIN
}
enum SvmsgdType {
SGD,
ASGD
}Public Types inherited from cv::ml::StatModel
Return |
Name |
Description |
|---|---|---|
Public Member Functions#
Public Member Functions inherited from cv::ml::StatModel
Return |
Name |
Description |
|---|---|---|
|
|
Computes error on the training or test dataset. |
|
Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read. |
|
|
Returns the number of variables in training samples. |
|
|
Returns true if the model is classifier. |
|
|
Returns true if the model is trained. |
|
|
|
Predicts response(s) for the provided sample(s) |
|
Trains the statistical model. |
|
|
|
Trains the statistical model. |
Public Member Functions inherited from cv::Algorithm
Return |
Name |
Description |
|---|---|---|
|
Clears the algorithm state. |
|
|
Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read. |
|
|
Reads algorithm parameters from a file storage. |
|
|
||
|
|
|
|
Stores algorithm parameters in a file storage. |
|
|
Static Public Member Functions#
Static Public Member Functions inherited from cv::ml::StatModel
Return |
Name |
Description |
|---|---|---|
|
Create and train model with default parameters. |
Static Public Member Functions inherited from cv::Algorithm
Return |
Name |
Description |
|---|---|---|
|
|
Loads algorithm from the file. |
|
|
Loads algorithm from a String. |
|
Reads algorithm from the file node. |
Additional Inherited Members#
Protected Member Functions inherited from cv::Algorithm
Return |
Name |
Description |
|---|---|---|
|
Detailed Description#
Stochastic Gradient Descent SVM classifier.
SVMSGD provides a fast and easy-to-use implementation of the SVM classifier using the Stochastic Gradient Descent approach, as presented in bottou2010large.
The classifier has following parameters:
model type,
margin type,
margin regularization ( \(\lambda\)),
initial step size ( \(\gamma_0\)),
step decreasing power ( \(c\)),
and termination criteria.
The model type may have one of the following values: SGD and ASGD.
SGD is the classic version of SVMSGD classifier: every next step is calculated by the formula
\[w_{t+1} = w_t - \gamma(t) \frac{dQ_i}{dw} |_{w = w_t}\]where\(w_t\) is the weights vector for decision function at step \(t\),
\(\gamma(t)\) is the step size of model parameters at the iteration \(t\), it is decreased on each step by the formula \(\gamma(t) = \gamma_0 (1 + \lambda \gamma_0 t) ^ {-c}\)
\(Q_i\) is the target functional from SVM task for sample with number \(i\), this sample is chosen stochastically on each step of the algorithm.
ASGD is Average Stochastic Gradient Descent SVM Classifier. ASGD classifier averages weights vector on each step of algorithm by the formula \(\widehat{w}_{t+1} = \frac{t}{1+t}\widehat{w}_{t} + \frac{1}{1+t}w_{t+1}\)
The recommended model type is ASGD (following bottou2010large).
The margin type may have one of the following values: SOFT_MARGIN or HARD_MARGIN.
You should use HARD_MARGIN type, if you have linearly separable sets.
You should use SOFT_MARGIN type, if you have non-linearly separable sets or sets with outliers.
In the general case (if you know nothing about linear separability of your sets), use SOFT_MARGIN.
The other parameters may be described as follows:
Margin regularization parameter is responsible for weights decreasing at each step and for the strength of restrictions on outliers (the less the parameter, the less probability that an outlier will be ignored). Recommended value for SGD model is 0.0001, for ASGD model is 0.00001.
Initial step size parameter is the initial value for the step size \(\gamma(t)\). You will have to find the best initial step for your problem.
Step decreasing power is the power parameter for \(\gamma(t)\) decreasing by the formula, mentioned above. Recommended value for SGD model is 1, for ASGD model is 0.75.
Termination criteria can be TermCriteria::COUNT, TermCriteria::EPS or TermCriteria::COUNT + TermCriteria::EPS. You will have to find the best termination criteria for your problem.
Note that the parameters margin regularization, initial step size, and step decreasing power should be positive.
To use SVMSGD algorithm do as follows:
first, create the SVMSGD object. The algorithm will set optimal parameters by default, but you can set your own parameters via functions setSvmsgdType(), setMarginType(), setMarginRegularization(), setInitialStepSize(), and setStepDecreasingPower().
then the SVM model can be trained using the train features and the correspondent labels by the method train().
after that, the label of a new feature vector can be predicted using the method predict().
Member Enumeration Documentation#
enum MarginType
|
General case, suits to the case of non-linearly separable sets, allows outliers. |
|
More accurate for the case of linearly separable sets. |
enum SvmsgdType
|
Stochastic Gradient Descent. |
|
Average Stochastic Gradient Descent. |
Member Function Documentation#
getInitialStepSize()#
float cv::ml::SVMSGD::getInitialStepSize()
Python:
cv.ml.SVMSGD.getInitialStepSize() -> retval
Parameter initialStepSize of a SVMSGD optimization problem.
See also
getMarginRegularization()#
float cv::ml::SVMSGD::getMarginRegularization()
Python:
cv.ml.SVMSGD.getMarginRegularization() -> retval
Parameter marginRegularization of a SVMSGD optimization problem.
See also
getMarginType()#
int cv::ml::SVMSGD::getMarginType()
Python:
cv.ml.SVMSGD.getMarginType() -> retval
Margin type, one of SVMSGD::MarginType.
See also
getShift()#
float cv::ml::SVMSGD::getShift()
Python:
cv.ml.SVMSGD.getShift() -> retval
Returns
the shift of the trained model (decision function f(x) = weights * x + shift).
getStepDecreasingPower()#
float cv::ml::SVMSGD::getStepDecreasingPower()
Python:
cv.ml.SVMSGD.getStepDecreasingPower() -> retval
Parameter stepDecreasingPower of a SVMSGD optimization problem.
See also
getSvmsgdType()#
int cv::ml::SVMSGD::getSvmsgdType()
Python:
cv.ml.SVMSGD.getSvmsgdType() -> retval
Algorithm type, one of SVMSGD::SvmsgdType.
See also
getTermCriteria()#
TermCriteria cv::ml::SVMSGD::getTermCriteria()
Python:
cv.ml.SVMSGD.getTermCriteria() -> retval
Termination criteria of the training algorithm. You can specify the maximum number of iterations (maxCount) and/or how much the error could change between the iterations to make the algorithm continue (epsilon).
See also
getWeights()#
Mat cv::ml::SVMSGD::getWeights()
Python:
cv.ml.SVMSGD.getWeights() -> retval
Returns
the weights of the trained model (decision function f(x) = weights * x + shift).
setInitialStepSize()#
void cv::ml::SVMSGD::setInitialStepSize(float InitialStepSize)
Python:
cv.ml.SVMSGD.setInitialStepSize(InitialStepSize)
Parameter initialStepSize of a SVMSGD optimization problem.
See also
setMarginRegularization()#
void cv::ml::SVMSGD::setMarginRegularization(float marginRegularization)
Python:
cv.ml.SVMSGD.setMarginRegularization(marginRegularization)
Parameter marginRegularization of a SVMSGD optimization problem.
See also
setMarginType()#
void cv::ml::SVMSGD::setMarginType(int marginType)
Python:
cv.ml.SVMSGD.setMarginType(marginType)
Margin type, one of SVMSGD::MarginType.
See also
setOptimalParameters()#
void cv::ml::SVMSGD::setOptimalParameters(
int svmsgdType = SVMSGD::ASGD,
int marginType = SVMSGD::SOFT_MARGIN )
Python:
cv.ml.SVMSGD.setOptimalParameters([, svmsgdType[, marginType]])
Function sets optimal parameters values for chosen SVM SGD model.
Parameters
svmsgdType— is the type of SVMSGD classifier.marginType— is the type of margin constraint.
setStepDecreasingPower()#
void cv::ml::SVMSGD::setStepDecreasingPower(float stepDecreasingPower)
Python:
cv.ml.SVMSGD.setStepDecreasingPower(stepDecreasingPower)
Parameter stepDecreasingPower of a SVMSGD optimization problem.
See also
setSvmsgdType()#
void cv::ml::SVMSGD::setSvmsgdType(int svmsgdType)
Python:
cv.ml.SVMSGD.setSvmsgdType(svmsgdType)
Algorithm type, one of SVMSGD::SvmsgdType.
See also
setTermCriteria()#
void cv::ml::SVMSGD::setTermCriteria(const cv::TermCriteria & val)
Python:
cv.ml.SVMSGD.setTermCriteria(val)
Termination criteria of the training algorithm. You can specify the maximum number of iterations (maxCount) and/or how much the error could change between the iterations to make the algorithm continue (epsilon).
See also
create()#
static Ptr< SVMSGD > cv::ml::SVMSGD::create()
Python:
cv.ml.SVMSGD.create() -> retval
cv.ml.SVMSGD_create() -> retval
Creates empty model. Use StatModel::train to train the model. Since SVMSGD has several parameters, you may want to find the best parameters for your problem or use setOptimalParameters() to set some default parameters.
load()#
static Ptr< SVMSGD > cv::ml::SVMSGD::load(
const String & filepath,
const String & nodeName = String() )
Python:
cv.ml.SVMSGD.load(filepath[, nodeName]) -> retval
cv.ml.SVMSGD_load(filepath[, nodeName]) -> retval
Loads and creates a serialized SVMSGD from a file.
Use SVMSGD::save to serialize and store an SVMSGD to disk. Load the SVMSGD from this file again, by calling this function with the path to the file. Optionally specify the node for the file containing the classifier
Parameters
filepath— path to serialized SVMSGDnodeName— name of node containing the classifier
Source file#
The documentation for this class was generated from the following file:
opencv2/ml.hpp