批量梯度下降(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)是要拟 ...
随机推荐
- 【SQLite】select into 语句
sqlite不支持类似sqlserver中的select into 语法 在SQL Server中,我们要将一个表中的数据复制到一个新表中,可以这样写: SELECT * INTO newtable ...
- 文件上传原理--FileReader
单个文件:<div> <input value="上传" type="file" id="photos_upload"&g ...
- c#用控制台程序安装启动停止卸载服务
第一步:新建控制台项目 第二步:添加服务 第三步:右键新建完成的服务项 点击 在start 和stop事件中分别写上 第四步 编写代码 双击打开 using System; using Syst ...
- react 父组件给子组件传值
父组件 import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child fr ...
- 洛谷——P1063 能量项链
P1063 能量项链 题目描述 在MarsMars星球上,每个MarsMars人都随身佩带着一串能量项链.在项链上有NN颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对 ...
- 四、Spider用法
本文转载自以下链接: https://scrapy-chs.readthedocs.io/zh_CN/latest/topics/spiders.html https://doc.scrapy.org ...
- python爬虫13 | 秒爬,这多线程爬取速度也太猛了,这次就是要让你的爬虫效率杠杠的
快 快了 啊 嘿 小老弟 想啥呢 今天这篇爬虫教程的主题就是一个字 快 想要做到秒爬 就需要知道 什么是多进程 什么是多线程 什么是协程(微线程) 你先去沏杯茶 坐下来 小帅b这就好好给你说道说道 关 ...
- sql 语句实现可用户名、邮箱、手机号登录系统
select top 1 nid from Users where (userName collate Chinese_PRC_CS_AS=@userName or mobile collate Ch ...
- 该页必须使用安全性较高的Web 浏览器查看
当用https访问某个网站时,IE提醒“该页必须使用安全性较高的Web 浏览器查看” 您要访问的资源使用了128位版本的“安全套接层(SSL)” 安全保护.要查看该资源,您需要使用支持该版本的SSL浏 ...
- HDU 3208 Integer’s Power
Integer’s Power Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Origina ...