opencv实现ann mlp(多层感知机)识别手写数字 一

本文详细解读了OpenCV 4.10中MLP(多层感知器)实现,重点介绍了setActivationFunction和setTrainMethod函数的作用,以及如何设置激活函数类型(如SIGMOID_SYM)和训练方法。通过实例代码,帮助读者理解参数调整对神经网络性能的影响。
摘要由CSDN通过智能技术生成

突然想用opencv实现一下mlp,说动手就动手。
首先查看了opencv410版本自带的mlp的例子。neural_network。这个例子就是告诉ml模块的可以实现mlp。这伞兵例子不知谁加的,可以说屁用没有。
注释过的例子代码如下

#include <opencv2/ml/ml.hpp>

using namespace std;
using namespace cv;
using namespace cv::ml;

int main()
{
    //create random training data
    Mat_<float> data(100, 100);
    randn(data, Mat::zeros(1, 1, data.type()), Mat::ones(1, 1, data.type()));
    //以高斯分布填充随机数据
    //half of the samples for each class
    Mat_<float> responses(data.rows, 2);
    for (int i = 0; i < data.rows; ++i)
    {
        if (i < data.rows / 2)
        {
            responses(i, 0) = 1;
            responses(i, 1) = 0;
        }
        else
        {
            responses(i, 0) = 0;
            responses(i, 1) = 1;
        }
    }
    //制作标签
    /*
    //example code for just a single response (regression)
    Mat_<float> responses(data.rows, 1);
    for (int i=0; i<responses.rows; ++i)
        responses(i, 0) = i < responses.rows / 2 ? 0 : 1;
    */

    //create the neural network
    Mat_<int> layerSizes(1, 3);
    layerSizes(0, 0) = data.cols;
    layerSizes(0, 1) = 20;
    layerSizes(0, 2) = responses.cols;//层数

    Ptr<ANN_MLP> network = ANN_MLP::create();//初始化一个ann mlp
    network->setLayerSizes(layerSizes);//设置神经网络各个层数目
    network->setActivationFunction(ANN_MLP::SIGMOID_SYM, 0.1, 0.1);//给每个神经元指定一个激活函数
    network->setTrainMethod(ANN_MLP::BACKPROP, 0.1, 0.1);//设置反向传播算法
    Ptr<TrainData> trainData = TrainData::create(data, ROW_SAMPLE, responses);//创建训练数据

    network->train(trainData);//直接训练
    if (network->isTrained())
    {
        printf("Predict one-vector:\n");
        Mat result;
        network->predict(Mat::ones(1, data.cols, data.type()), result);//预测,
        cout << result << endl;

        printf("Predict training data:\n");
        for (int i = 0; i < data.rows; ++i)
        {
            network->predict(data.row(i), result);
            cout << result << endl;
        }
        //整个流程可以说比较简单
    }

    return 0;
}

可以,除了告诉你函数能运行之外,没有任何参考意义。
需要再仔细解释的函数两个: setActivationFunction setTrainMethod

  1. CV_WRAP virtual void setActivationFunction(int type, double param1 = 0, double param2 = 0) = 0;

根据官方文档第一个参数type有四种:
在这里插入图片描述
除了SIGMOID_SYM(对称sigmoid)都比较容易理解。SIGMOID_SYM也是opencv默认和唯一完全支持的激活函数。
只说SIGMOID_SYM,后面两个参数指的是公式中的beta和阿尔法。文档说后两个参数默认值为零。
但是从上面的公式看,都为0时,激活函数fx=0,好像看着是这样。这点看源码可以知道,阿尔法=0会被修正为2.0/3 .beta会被修正为1.7159;。这令人误解的写法。

  if( fabs(_f_param1) < FLT_EPSILON )
                _f_param1 = 2./3;
            if( fabs(_f_param2) < FLT_EPSILON )
                _f_param2 = 1.7159;
  1. CV_WRAP virtual void setTrainMethod(int method, double param1 = 0, double param2 = 0) = 0;
    设置训练方法和通用参数。

训练方法有三个:
在这里插入图片描述
反向传播算法、RPROP算法 、模拟退火算法。三个老当益壮的老东西。
后面两个参数都是服务这三个优化算法的。对这俩参数的说明我直接复制opencv文档里的

method Default value is ANN_MLP::RPROP. See ANN_MLP::TrainingMethods.
param1 passed to setRpropDW0 for ANN_MLP::RPROP and to setBackpropWeightScale for ANN_MLP::BACKPROP and to initialT for ANN_MLP::ANNEAL.
param2 passed to setRpropDWMin for ANN_MLP::RPROP and to setBackpropMomentumScale for ANN_MLP::BACKPROP and to finalT for ANN_MLP::ANNEAL.

对于反向传播算法而言。param1服务于bp的权重比例,可以理解为学习率、权重更新率等等吧(名字多,但是意思是一个)。param2服务bp的动量更新率。opencv的反向传播是带动量。
从这个例子源码里,能看到的就那么多了。

基本上,这篇是准备工作,分析了一下参考代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值