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. [Windows Server 2012] 手工破解MySQL密码

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:破解MySQL ...

  2. 微服务的一种开源实现方式——dubbo+zookeeper

    转自: http://blog.csdn.NET/zhdd99/article/details/52263609 微服务架构成了当下的技术热点,实现微服务是要付出很大成本的,但也许是因为微服务的优点太 ...

  3. 解决 i5 6500 安装黑苹果 Sierra 显卡不正常问题

    i5 6500内置HD 530显卡,装好Sierra显卡驱动不太正常. 先下载Clover configurator 用Clover configurator加载 EFI (Mount EFI)分区 ...

  4. ngFor 循环带索引

    *ngFor="let item of userList,let i = index"   或者 *ngFor="let item of userList index a ...

  5. 匈牙利算法求最大匹配(HDU-4185 Oil Skimming)

    如下图:要求最多可以凑成多少对对象 大佬博客: https://blog.csdn.net/cillyb/article/details/55511666 https://blog.csdn.net/ ...

  6. HTML学习笔记之HTML5新特性

    目录 1.拖放 2.画布 3.可伸缩矢量图形 4.地理定位 5.Web 存储 6.应用缓存 7.Web Worker 1.拖放 拖放是一种常见的特性,用于抓取对象以后拖到另一个位置,它是 HTML5 ...

  7. 洛谷 P1308 统计单词数【字符串处理】

    题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数. 现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给 ...

  8. Java基础——基础数据类型与读入输出

    首先我们写完了HelloWorld就学会了java的一种输出 System.out.println() 用起来就像是被强化过的C++的puts函数 或者就是自带endl的cout函数,中间的" ...

  9. Leetcode 30.与所有单词相关联的子串

    与所有单词相关联的字串 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...

  10. jquery-radio 事件监听以及取值

    $("input:radio[name='ssx']").change(function (){ alert( $(this).val()); alert($("inpu ...