OpenCV 4.13.0
开源计算机视觉库 (Open Source Computer Vision)
正在加载...
正在搜索...
未找到匹配项
cv::ml::SVMSGD 类引用抽象

随机梯度下降 SVM 分类器。 更多...

#include <opencv2/ml.hpp>

cv::ml::SVMSGD 的协作图

公开类型

enum  MarginType {
  SOFT_MARGIN ,
  HARD_MARGIN
}
 
enum  SvmsgdType {
  SGD ,
  ASGD
}
 
- 从 cv::ml::StatModel 继承的公共类型
enum  标志 {
  UPDATE_MODEL = 1 ,
  RAW_OUTPUT =1 ,
  COMPRESSED_INPUT =2 ,
  PREPROCESSED_INPUT =4
}
 

公开成员函数

virtual float getInitialStepSize () const =0
 SVMSGD 优化问题的初始步长参数。
 
virtual float getMarginRegularization () const =0
 SVMSGD 优化问题的边距正则化参数。
 
virtual int getMarginType () const =0
 边距类型,是 SVMSGD::MarginType 之一。
 
virtual float getShift ()=0
 
virtual float getStepDecreasingPower () const =0
 SVMSGD 优化问题的步长递减幂参数。
 
virtual int getSvmsgdType () const =0
 算法类型,是 SVMSGD::SvmsgdType 之一。
 
virtual TermCriteria getTermCriteria () const =0
 训练算法的终止条件。您可以指定最大迭代次数 (maxCount) 和/或在迭代之间误差的变化量以使算法继续(epsilon)。
 
virtual Mat getWeights ()=0
 
virtual void setInitialStepSize (float InitialStepSize)=0
 SVMSGD 优化问题的初始步长参数。
 
virtual void setMarginRegularization (float marginRegularization)=0
 SVMSGD 优化问题的边距正则化参数。
 
virtual void setMarginType (int marginType)=0
 边距类型,是 SVMSGD::MarginType 之一。
 
virtual void setOptimalParameters (int svmsgdType=SVMSGD::ASGD, int marginType=SVMSGD::SOFT_MARGIN)=0
 该函数为选定的 SVM SGD 模型设置最优参数值。
 
virtual void setStepDecreasingPower (float stepDecreasingPower)=0
 SVMSGD 优化问题的步长递减幂参数。
 
virtual void setSvmsgdType (int svmsgdType)=0
 算法类型,是 SVMSGD::SvmsgdType 之一。
 
virtual void setTermCriteria (const cv::TermCriteria &val)=0
 训练算法的终止条件。您可以指定最大迭代次数 (maxCount) 和/或在迭代之间误差的变化量以使算法继续(epsilon)。
 
- 从 cv::ml::StatModel 继承的公共成员函数
virtual float calcError (const Ptr< TrainData > &data, bool test, OutputArray resp) const
 计算训练集或测试集上的误差。
 
virtual bool empty () const CV_OVERRIDE
 如果 Algorithm 为空(例如,在最开始或读取不成功后),则返回 true。
 
virtual int getVarCount () const =0
 返回训练样本中的变量数量。
 
virtual bool isClassifier () const =0
 如果模型是分类器,则返回 true。
 
virtual bool isTrained () const =0
 如果模型已训练,则返回 true。
 
virtual float predict (InputArray samples, OutputArray results=noArray(), int flags=0) const =0
 预测所提供样本的响应
 
virtual bool train (const Ptr< TrainData > &trainData, int flags=0)
 训练统计模型。
 
virtual bool train (InputArray samples, int layout, InputArray responses)
 训练统计模型。
 
- 从 cv::Algorithm 继承的公共成员函数
 Algorithm ()
 
virtual ~Algorithm ()
 
virtual void clear ()
 清除算法状态。
 
virtual String getDefaultName () const
 
virtual void read (const FileNode &fn)
 从文件存储中读取算法参数。
 
virtual void save (const String &filename) const
 
void write (const Ptr< FileStorage > &fs, const String &name=String()) const
 
virtual void write (FileStorage &fs) const
 将算法参数存储在文件存储中。
 
void write (FileStorage &fs, const String &name) const
 

静态公开成员函数

static Ptr< SVMSGDcreate ()
 创建空模型。使用 StatModel::train 来训练模型。由于 SVMSGD 有多个参数,您可能需要为您的项目找到最佳参数,或者使用 setOptimalParameters() 来设置一些默认参数。
 
static Ptr< SVMSGDload (const String &filepath, const String &nodeName=String())
 从文件加载并创建一个序列化的 SVMSGD
 
- 从 cv::ml::StatModel 继承的静态公共成员函数
template<typename _Tp >
static Ptr< _Tptrain (const Ptr< TrainData > &data, int flags=0)
 创建并训练具有默认参数的模型。
 
- 从 cv::Algorithm 继承的静态公共成员函数
template<typename _Tp >
static Ptr< _Tpload (const String &filename, const String &objname=String())
 从文件中加载算法。
 
template<typename _Tp >
static Ptr< _TploadFromString (const String &strModel, const String &objname=String())
 从字符串中加载算法。
 
template<typename _Tp >
static Ptr< _Tpread (const FileNode &fn)
 从文件节点中读取算法。
 

更多继承的成员

- 从 cv::Algorithm 继承的保护成员函数
void writeFormat (FileStorage &fs) const
 

详细说明

随机梯度下降 SVM 分类器。

SVMSGD 提供了一个快速易用的 SVM 分类器实现,采用了随机梯度下降方法,如 [37] 所述。

该分类器具有以下参数

  • 模型类型,
  • 边距类型,
  • 边距正则化( \(\lambda\)),
  • 初始步长( \(\gamma_0\)),
  • 步长递减幂( \(c\)),
  • 以及终止条件。

模型类型可以具有以下值之一: SGDASGD

  • SGDSVMSGD 分类器的经典版本:每一步的计算公式如下

    \[w_{t+1} = w_t - \gamma(t) \frac{dQ_i}{dw} |_{w = w_t}\]

    其中
    • \(w_t\) 是第 \(t\) 步决策函数的权重向量,
    • \(\gamma(t)\) 是模型参数在迭代 \(t\) 时的步长,它通过公式 \(\gamma(t) = \gamma_0 (1 + \lambda \gamma_0 t) ^ {-c}\) 逐渐减小。
    • \(Q_i\) 是第 \(i\) 个样本的 SVM 任务的目标函数,该样本在算法的每一步被随机选择。
  • ASGD 是平均随机梯度下降 SVM 分类器。ASGD 分类器通过公式 \(\widehat{w}_{t+1} = \frac{t}{1+t}\widehat{w}_{t} + \frac{1}{1+t}w_{t+1}\) 对算法每一步的权重向量进行平均。

推荐的模型类型为 ASGD(遵循 [37])。

边距类型可以具有以下值之一: SOFT_MARGINHARD_MARGIN

  • 如果您的数据集是线性可分的,则应使用 HARD_MARGIN 类型。
  • 如果您的数据集是非线性可分的或包含异常值,则应使用 SOFT_MARGIN 类型。
  • 在一般情况下(如果您对数据集的线性可分性一无所知),请使用 SOFT_MARGIN。

其他参数可以描述如下

  • 边距正则化参数负责每一步的权重衰减以及对异常值的限制强度(参数越小,异常值被忽略的可能性越小)。SGD 模型推荐值为 0.0001,ASGD 模型推荐值为 0.00001。
  • 初始步长参数是步长 \(\gamma(t)\) 的初始值。您需要为您的项目找到最佳初始步长。
  • 步长递减幂是 \(\gamma(t)\) 按上述公式递减的幂参数。SGD 模型推荐值为 1,ASGD 模型推荐值为 0.75。
  • 终止条件可以是 TermCriteria::COUNTTermCriteria::EPSTermCriteria::COUNT + TermCriteria::EPS。您需要为您的项目找到最佳的终止条件。

请注意,边距正则化、初始步长和步长递减幂参数应为正值。

要使用 SVMSGD 算法,请按以下步骤操作

// 创建空对象
// 训练随机梯度下降 SVM
svmsgd->train(trainData);
// 预测新样本的标签
svmsgd->predict(samples, responses);
static Ptr< SVMSGD > create()
创建空模型。使用 StatModel::train 来训练模型。由于 SVMSGD 有多个参数,...
std::shared_ptr< _Tp > Ptr
定义 cvstd_wrapper.hpp:23

成员枚举文档

◆ MarginType

边距类型。

枚举值 (Enumerator)
SOFT_MARGIN 

一般情况,适用于非线性可分数据集,允许异常值。

HARD_MARGIN 

对于线性可分数据集更准确。

◆ SvmsgdType

SVMSGD 类型。ASGD 通常是更优的选择。

枚举值 (Enumerator)
SGD 

随机梯度下降。

ASGD 

平均随机梯度下降。

成员函数说明

◆ create()

static Ptr< SVMSGD > cv::ml::SVMSGD::create ( )
static (静态)
Python
cv.ml.SVMSGD.create() -> retval
cv.ml.SVMSGD_create() -> retval

创建空模型。使用 StatModel::train 来训练模型。由于 SVMSGD 有多个参数,您可能需要为您的项目找到最佳参数,或者使用 setOptimalParameters() 来设置一些默认参数。

◆ getInitialStepSize()

virtual float cv::ml::SVMSGD::getInitialStepSize ( ) const
纯虚函数
Python
cv.ml.SVMSGD.getInitialStepSize() -> retval

SVMSGD 优化问题的初始步长参数。

另请参阅
setInitialStepSize

◆ getMarginRegularization()

virtual float cv::ml::SVMSGD::getMarginRegularization ( ) const
纯虚函数
Python
cv.ml.SVMSGD.getMarginRegularization() -> retval

SVMSGD 优化问题的边距正则化参数。

另请参阅
setMarginRegularization

◆ getMarginType()

virtual int cv::ml::SVMSGD::getMarginType ( ) const
纯虚函数
Python
cv.ml.SVMSGD.getMarginType() -> retval

边距类型,是 SVMSGD::MarginType 之一。

另请参阅
setMarginType

◆ getShift()

virtual float cv::ml::SVMSGD::getShift ( )
纯虚函数
Python
cv.ml.SVMSGD.getShift() -> retval
返回
训练模型(决策函数 f(x) = weights * x + shift)的偏移量。

◆ getStepDecreasingPower()

virtual float cv::ml::SVMSGD::getStepDecreasingPower ( ) const
纯虚函数
Python
cv.ml.SVMSGD.getStepDecreasingPower() -> retval

SVMSGD 优化问题的步长递减幂参数。

另请参阅
setStepDecreasingPower

◆ getSvmsgdType()

virtual int cv::ml::SVMSGD::getSvmsgdType ( ) const
纯虚函数
Python
cv.ml.SVMSGD.getSvmsgdType() -> retval

算法类型,是 SVMSGD::SvmsgdType 之一。

另请参阅
setSvmsgdType

◆ getTermCriteria()

virtual TermCriteria cv::ml::SVMSGD::getTermCriteria ( ) const
纯虚函数
Python
cv.ml.SVMSGD.getTermCriteria() -> retval

训练算法的终止条件。您可以指定最大迭代次数 (maxCount) 和/或在迭代之间误差的变化量以使算法继续(epsilon)。

另请参阅
setTermCriteria

◆ getWeights()

virtual Mat cv::ml::SVMSGD::getWeights ( )
纯虚函数
Python
cv.ml.SVMSGD.getWeights() -> retval
返回
训练模型(决策函数 f(x) = weights * x + shift)的权重。

◆ load()

static Ptr< SVMSGD > cv::ml::SVMSGD::load ( const String & filepath,
const String & nodeName=String() )
static (静态)
Python
cv.ml.SVMSGD.load(filepath[, nodeName]) -> retval
cv.ml.SVMSGD_load(filepath[, nodeName]) -> retval

从文件加载并创建一个序列化的 SVMSGD

使用 SVMSGD::saveSVMSGD 序列化并存储到磁盘。通过调用此函数并提供文件路径,可以再次从该文件加载 SVMSGD。可以选择性地指定包含分类器的文件中的节点。

参数
filepath序列化 SVMSGD 的路径
nodeName包含分类器的节点名称

◆ setInitialStepSize()

virtual void cv::ml::SVMSGD::setInitialStepSize ( float InitialStepSize)
纯虚函数
Python
cv.ml.SVMSGD.setInitialStepSize(InitialStepSize) -> None

SVMSGD 优化问题的初始步长参数。

另请参阅
getInitialStepSize

◆ setMarginRegularization()

virtual void cv::ml::SVMSGD::setMarginRegularization ( float marginRegularization)
纯虚函数
Python
cv.ml.SVMSGD.setMarginRegularization(marginRegularization) -> None

SVMSGD 优化问题的边距正则化参数。

另请参阅
getMarginRegularization

◆ setMarginType()

virtual void cv::ml::SVMSGD::setMarginType ( int marginType)
纯虚函数
Python
cv.ml.SVMSGD.setMarginType(marginType) -> None

边距类型,是 SVMSGD::MarginType 之一。

另请参阅
getMarginType

◆ setOptimalParameters()

virtual void cv::ml::SVMSGD::setOptimalParameters ( int svmsgdType=SVMSGD::ASGD,
int marginType=SVMSGD::SOFT_MARGIN )
纯虚函数
Python
cv.ml.SVMSGD.setOptimalParameters([, svmsgdType[, marginType]]) -> None

该函数为选定的 SVM SGD 模型设置最优参数值。

参数
svmsgdTypeSVMSGD 分类器的类型。
marginType是边距约束的类型。

◆ setStepDecreasingPower()

virtual void cv::ml::SVMSGD::setStepDecreasingPower ( float stepDecreasingPower)
纯虚函数
Python
cv.ml.SVMSGD.setStepDecreasingPower(stepDecreasingPower) -> None

SVMSGD 优化问题的步长递减幂参数。

另请参阅
getStepDecreasingPower

◆ setSvmsgdType()

virtual void cv::ml::SVMSGD::setSvmsgdType ( int svmsgdType)
纯虚函数
Python
cv.ml.SVMSGD.setSvmsgdType(svmsgdType) -> None

算法类型,是 SVMSGD::SvmsgdType 之一。

另请参阅
getSvmsgdType

◆ setTermCriteria()

virtual void cv::ml::SVMSGD::setTermCriteria ( const cv::TermCriteria & val)
纯虚函数
Python
cv.ml.SVMSGD.setTermCriteria(val) -> None

训练算法的终止条件。您可以指定最大迭代次数 (maxCount) 和/或在迭代之间误差的变化量以使算法继续(epsilon)。

另请参阅
getTermCriteria

该类的文档由以下文件生成: