At each step the weight vector is moved in the direction of the greatest rate of decrease of the error function,

and so this approach is known as gradient descent(梯度下降法) or steepest descent(最速下降法).

Techniques that use the whole data set at once are called batch methods.

With the method of gradient descent used to perform the training, the advantages of batch learning

include the following:

1)accurate estimation of the gradient vector(i.e., the derivative of the cost function with respect to the weight vector w),

thereby guaranteeing, under simple conditions, convergence of the method of steepest descent to a local minimum;

2)parallalization of the learning process.

However, from a practical perspective, batch learning is rather demanding in terms of storage requirements.

#include <iostream>
#include <vector>
#include <cmath>
#include <cfloat>

/*批量梯度下降法*/
int main() {
    double datax[]={1,2,3,4,5};
    double datay[]={1,1,2,2,4};
    std::vector<double> v_datax,v_datay;

for(size_t i=0;i<sizeof(datax)/sizeof(datax[0]);++i) {
        v_datax.push_back(datax[i]);
        v_datay.push_back(datay[i]);
    }

double a=0,b=0;
    double J=0.0;

for(std::vector<double>::iterator iterx=v_datax.begin(),itery=v_datay.begin();iterx!=v_datax.end(),itery!=v_datay.end();++iterx,++itery) {
        J+=(a+b*(*iterx)-*itery)*(a+b*(*iterx)-*itery);
    }
    J=J*0.5/v_datax.size();
                            
    while(true) {
        double grad0=0,grad1=0;
        for(std::vector<double>::iterator iterx=v_datax.begin(),itery=v_datay.begin();iterx!=v_datax.end(),itery!=v_datay.end();++iterx,++itery) {
            grad0+=(a+b*(*iterx)-*itery);
            grad1+=(a+b*(*iterx)-*itery)*(*iterx);
        }

grad0=grad0/v_datax.size();
        grad1=grad1/v_datax.size();

//0.03为学习率阿尔法
        a=a-0.03*grad0;
        b=b-0.03*grad1;
        double MSE=0;
        
        for(std::vector<double>::iterator iterx=v_datax.begin(),itery=v_datay.begin();iterx!=v_datax.end(),itery!=v_datay.end();++iterx,++itery) {
            MSE+=(a+b*(*iterx)-*itery)*(a+b*(*iterx)-*itery);
        }
        MSE=MSE*0.5/v_datax.size();
        
        if(std::abs(J-MSE)<0.0000001)
            break;
        J=MSE;
    }

std::cout<<"批量梯度下降法得到的结果:"<<std::endl;
    std::cout<<"a = "<<a<<std::endl;
    std::cout<<"b = "<<b<<std::endl;

return 0;
}

In a statistical context, batch learning may be viewed as a form of statistical inference. It is therefore well suited

for solving nonlinear regression problems.

批量梯度下降(Batch gradient descent) C++的更多相关文章

  1. 梯度下降(Gradient Descent)小结

    在求解机器学习算法的模型参数,即无约束优化问题时,梯度下降(Gradient Descent)是最常采用的方法之一,另一种常用的方法是最小二乘法.这里就对梯度下降法做一个完整的总结. 1. 梯度 在微 ...

  2. 梯度下降(Gradient Descent)

    在求解机器学习算法的模型参数,即无约束优化问题时,梯度下降(Gradient Descent)是最常采用的方法之一,另一种常用的方法是最小二乘法.这里就对梯度下降法做一个完整的总结. 1. 梯度 在微 ...

  3. 梯度下降(Gradient Descent)相关概念

    梯度,直观理解: 梯度: 运算的对像是纯量,运算出来的结果会是向量在一个标量场中, 梯度的计算结果会是"在每个位置都算出一个向量,而这个向量的方向会是在任何一点上从其周围(极接近的周围,学过 ...

  4. ML:梯度下降(Gradient Descent)

    现在我们有了假设函数和评价假设准确性的方法,现在我们需要确定假设函数中的参数了,这就是梯度下降(gradient descent)的用武之地. 梯度下降算法 不断重复以下步骤,直到收敛(repeat ...

  5. 随机梯度下降 Stochastic gradient descent

    梯度下降法先随机给出参数的一组值,然后更新参数,使每次更新后的结构都能够让损失函数变小,最终达到最小即可. 在梯度下降法中,目标函数其实可以看做是参数的函数,因为给出了样本输入和输出值后,目标函数就只 ...

  6. 多变量线性回归时使用梯度下降(Gradient Descent)求最小值的注意事项

    梯度下降是回归问题中求cost function最小值的有效方法,对大数据量的训练集而言,其效果要 好于非迭代的normal equation方法. 在将其用于多变量回归时,有两个问题要注意,否则会导 ...

  7. 随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的公式对比、实现对比[转]

    梯度下降(GD)是最小化风险函数.损失函数的一种常用方法,随机梯度下降和批量梯度下降是两种迭代求解思路,下面从公式和实现的角度对两者进行分析,如有哪个方面写的不对,希望网友纠正. 下面的h(x)是要拟 ...

  8. 【转】 随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的公式对比、实现对比

    梯度下降(GD)是最小化风险函数.损失函数的一种常用方法,随机梯度下降和批量梯度下降是两种迭代求解思路,下面从公式和实现的角度对两者进行分析,如有哪个方面写的不对,希望网友纠正. 下面的h(x)是要拟 ...

  9. batch gradient descent(批量梯度下降) 和 stochastic gradient descent(随机梯度下降)

    批量梯度下降是一种对参数的update进行累积,然后批量更新的一种方式.用于在已知整个训练集时的一种训练方式,但对于大规模数据并不合适. 随机梯度下降是一种对参数随着样本训练,一个一个的及时updat ...

  10. 机器学习-随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )

    梯度下降(GD)是最小化风险函数.损失函数的一种常用方法,随机梯度下降和批量梯度下降是两种迭代求解思路,下面从公式和实现的角度对两者进行分析,如有哪个方面写的不对,希望网友纠正. 下面的h(x)是要拟 ...

随机推荐

  1. 8 switch case

    当一个case成立,从这个case向后穿透所有case,即使后面的case条件不成立 包括default,直到程序结束或者遇到break程序才结束. 1.case是常量,且不能重复 2.表达式可以是b ...

  2. vmware vSphere client中,选择文件->部署OVF模板,报错处理方法

    在vmware vSphere client中,选择文件->部署OVF模板,选择指定的OVA文件,按步骤进行,则会出现这样的错误:此OVF软件包使用了不受支持的功能.OVF软件包需要不支持的硬件 ...

  3. php第二十节课

    JSON弹窗 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  4. Aizu - 1379 Parallel Lines

    平行直线 题意:给出一些点,这些点两两相连成一条直线,问最多能连成多少条直线. 思路:暴力出奇迹!!记得当时比赛做这道题的时候一直依赖于板子,结果却限制了自己的思路,这得改.dfs直接暴力,但是需要将 ...

  5. HDU - 1087 Super Jumping!Jumping!Jumping!(dp求最长上升子序列的和)

    传送门:HDU_1087 题意:现在要玩一个跳棋类游戏,有棋盘和棋子.从棋子st开始,跳到棋子en结束.跳动棋子的规则是下一个落脚的棋子的号码必须要大于当前棋子的号码.st的号是所有棋子中最小的,en ...

  6. JS练习:两级联动

    代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...

  7. 实验十二 团队作业8:软件测试与Alpha冲刺 第五天

    项目 内容 这个作业属于哪个课程 老师链接 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 Always Run! 作业学习目标 (1)掌握软件测试基础技术 (2)学习 ...

  8. 【CodeCraft-19 and Codeforces Round #537 (Div. 2) C】Creative Snap

    [链接] 我是链接,点我呀:) [题意] 横坐标1..2^n对应着2^n个复仇者的基地,上面有k个复仇者(位置依次给出). 你是灭霸你要用以下方法消灭这k个复仇者: 一开始你获取整个区间[1..2^n ...

  9. ActiveMQ学习总结(1)——ActiveMQ快速入门

    1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 2.运行ActiveMQ 解压缩apache-activemq-5.5.1-bin.zip,然后双击a ...

  10. chromeDriver下载地址

    http://chromedriver.storage.googleapis.com/index.html