批量梯度下降(Batch gradient descent) C++
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++的更多相关文章
- 梯度下降(Gradient Descent)小结
在求解机器学习算法的模型参数,即无约束优化问题时,梯度下降(Gradient Descent)是最常采用的方法之一,另一种常用的方法是最小二乘法.这里就对梯度下降法做一个完整的总结. 1. 梯度 在微 ...
- 梯度下降(Gradient Descent)
在求解机器学习算法的模型参数,即无约束优化问题时,梯度下降(Gradient Descent)是最常采用的方法之一,另一种常用的方法是最小二乘法.这里就对梯度下降法做一个完整的总结. 1. 梯度 在微 ...
- 梯度下降(Gradient Descent)相关概念
梯度,直观理解: 梯度: 运算的对像是纯量,运算出来的结果会是向量在一个标量场中, 梯度的计算结果会是"在每个位置都算出一个向量,而这个向量的方向会是在任何一点上从其周围(极接近的周围,学过 ...
- ML:梯度下降(Gradient Descent)
现在我们有了假设函数和评价假设准确性的方法,现在我们需要确定假设函数中的参数了,这就是梯度下降(gradient descent)的用武之地. 梯度下降算法 不断重复以下步骤,直到收敛(repeat ...
- 随机梯度下降 Stochastic gradient descent
梯度下降法先随机给出参数的一组值,然后更新参数,使每次更新后的结构都能够让损失函数变小,最终达到最小即可. 在梯度下降法中,目标函数其实可以看做是参数的函数,因为给出了样本输入和输出值后,目标函数就只 ...
- 多变量线性回归时使用梯度下降(Gradient Descent)求最小值的注意事项
梯度下降是回归问题中求cost function最小值的有效方法,对大数据量的训练集而言,其效果要 好于非迭代的normal equation方法. 在将其用于多变量回归时,有两个问题要注意,否则会导 ...
- 随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的公式对比、实现对比[转]
梯度下降(GD)是最小化风险函数.损失函数的一种常用方法,随机梯度下降和批量梯度下降是两种迭代求解思路,下面从公式和实现的角度对两者进行分析,如有哪个方面写的不对,希望网友纠正. 下面的h(x)是要拟 ...
- 【转】 随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的公式对比、实现对比
梯度下降(GD)是最小化风险函数.损失函数的一种常用方法,随机梯度下降和批量梯度下降是两种迭代求解思路,下面从公式和实现的角度对两者进行分析,如有哪个方面写的不对,希望网友纠正. 下面的h(x)是要拟 ...
- batch gradient descent(批量梯度下降) 和 stochastic gradient descent(随机梯度下降)
批量梯度下降是一种对参数的update进行累积,然后批量更新的一种方式.用于在已知整个训练集时的一种训练方式,但对于大规模数据并不合适. 随机梯度下降是一种对参数随着样本训练,一个一个的及时updat ...
- 机器学习-随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )
梯度下降(GD)是最小化风险函数.损失函数的一种常用方法,随机梯度下降和批量梯度下降是两种迭代求解思路,下面从公式和实现的角度对两者进行分析,如有哪个方面写的不对,希望网友纠正. 下面的h(x)是要拟 ...
随机推荐
- angular 零碎
相关链接 api(需要FQ) ui-router 知乎 作用域 angular 中作用域的概念是一个亮点,由不同的指令.controller等作用域组成的作用域树就是一个app.简单理解一个contr ...
- Springboot启动工程后,浏览器出现输入用户名和密码
在使用spring boot的时候发现启动项目时,浏览器需要输入用户名和密码. baidu后发现是因为pom中引用了Spring Security,但是项目中没有使用,在pom中注释掉即可.
- 远程连接阿里云服务器ping不通ip解决方案
搭建了阿里云服务器,发现本地ping不通,查看半天才发现,原来是在阿里云上的安全组少了些东西. 在出入方向上新建一个安全组,就可以搞定了.
- uva 1401
Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing ...
- Python ---- KMP(博文推荐+代码)
既解决完后宫问题(八皇后问题)后,又利用半天的时间完成了著名的“看毛片”算法——KMP.对于初学者来说这绝对是个大坑,非常难以理解. 在此,向提出KMP算法的三位大佬表示诚挚的敬意.!!!牛X!!! ...
- 原始js表单文本框初始化获取焦点和选中
发现: 1 使用dom对象.focus() 放在 window.onload 时可以 让这个DOM对象获取焦点. 2 使用DOM对象.select() 可以选中文本框中的文字. 3 不要加上on , ...
- python爬虫10 | 网站维护人员:真的求求你们了,不要再来爬取了!!
今天 小帅b想给大家讲一个小明的小故事 ... 话说 在很久很久以前 小明不小心发现了一个叫做 学习python的正确姿势 的公众号 从此一发不可收拾 看到什么网站都想爬取 有一天 小明发现了一个小黄 ...
- ganglia3.7.2,web3.7.1安装
1.准备安装包 ganglia-3.7.2-2.el6.x86_64.rpm ganglia-gmetad-3.7.2-2.el6.x86_64.rpm ganglia-gmond-3.7.2-2.e ...
- 【Codeforces 1106D】Lunar New Year and a Wander
[链接] 我是链接,点我呀:) [题意] 让你遍历n个节点,访问过的节点不操作. 如果是没有访问过的点,那就把它加到序列的末尾. 问你形成的最小字典序的序列是多少. [题解] 显然每次找最小的标号 用 ...
- 【codeforces 514D】R2D2 and Droid Army
[题目链接]:http://codeforces.com/contest/514/problem/D [题意] 给你每个机器人的m种属性p1..pm 然后r2d2每次可以选择m种属性中的一种,进行一次 ...