ImageVerifierCode 换一换
格式:DOC , 页数:18 ,大小:235.28KB ,
资源ID:4769894      下载积分:8 金币
验证码下载
登录下载
邮箱/手机:
图形码:
验证码: 获取验证码
温馨提示:
支付成功后,系统会自动生成账号(用户名为邮箱或者手机号,密码是验证码),方便下次登录下载和查询订单;
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/4769894.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请。


权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:4009-655-100;投诉/维权电话:18658249818。

注意事项

本文(利用梯度下降法实现线性回归的算法及matlab实现-0515.doc)为本站上传会员【人****来】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

利用梯度下降法实现线性回归的算法及matlab实现-0515.doc

1、利用梯度下降法实现线性回归的算法及matlab实现 1. 线性回归算法概述 线性回归属于监督学习,因此方法和监督学习应该是一样的,先给定一个训练集,根据这个训练集学习出一个线性函数,然后测试这个函数训练的好不好(即此函数是否足够拟合训练集数据),挑选出最好的函数(cost function最小)即可; 注意: (1)因为是线性回归,所以学习到的函数为线性函数,即直线函数; (2)线性回归可分为单变量线性回归和多变量线性回归;对于单变量线性回归而言,只有一个输入变量x; (1). 单变量线性回归 我们能够给出单变量线性回归的模型:  

2、 我们常称x为feature,h(x)为hypothesis;上述模型中的θ0和θ1在代码中分别用theta0和theta1表示。 从上面“方法”中,我们肯定有一个疑问,怎么样能够看出线性函数拟合的好不好呢?我们需要使用到Cost Function(代价函数),代价函数越小,说明线性回归地越好(和训练集拟合地越好),当然最小就是0,即完全拟合。cost Function的内部构造如下面公式所述:  其中: 表示向量x中的第i个元素; 表示向量y中的第i个元素; 表示已知的假设函数; m为训练集的数量; 虽然给定一个函数,我们能够根据cost function知道这个函

3、数拟合的好不好,但是毕竟函数有这么多,总不可能一个一个试吧?因此我们引出了梯度下降:能够找出cost function函数的最小值; 梯度下降原理:将函数比作一座山,我们站在某个山坡上,往四周看,从哪个方向向下走一小步,能够下降的最快;当然解决问题的方法有很多,梯度下降只是其中一个,还有一种方法叫Normal Equation; 方法: (1)先确定向下一步的步伐大小,我们称为Learning rate (alpha); (2)任意给定一个初始值:(用theta0和theta1表示); (3)确定一个向下的方向,并向下走预先规定的步伐,并更新; (4)当下降的高度小于某个定义的值,

4、则停止下降; 算法: 特点: (1)初始点不同,获得的最小值也不同,因此梯度下降求得的只是局部最小值; (2)越接近最小值时,下降速度越慢; 梯度下降能够求出一个函数的最小值; 线性回归需要使得cost function的最小; 因此我们能够对cost function运用梯度下降,即将梯度下降和线性回归进行整合,如下图所示: 上式中右边的公式推导过程如下: ∂hθ∂θ0=1 ∂hθ∂θ1=x(i) ∂∂θ0Jθ0,θ1=12mi=1m2×(hθx(i)-y(i))×∂hθ∂θ0=1mi=1m(hθx(i)-y(i)) ∂∂θ1Jθ0,θ1=12mi=1

5、m2×(hθx(i)-y(i))×∂hθ∂θ1=1mi=1m(hθx(i)-y(i))×x(i) 从上面的推导中可以看出,要想满足梯度下降的条件,则(hθx(i)-y(i))项后面必须乘以对应的输入信号x(i)。 梯度下降是通过不停的迭代,而我们比较关注迭代的次数,因为这关系到梯度下降的执行速度,为了减少迭代次数,因此引入了Feature Scaling。 (2). Feature Scaling 此种方法应用于梯度下降,为了加快梯度下降的执行速度; 思想:将各个feature的值标准化,使得取值范围大致都在-1<=x<=1之间; (3). 常用的方法是Mean Normali

6、zation(均值归一化处理) 或者: [X-mean(X)]/std(X); (4). 收获汇总 学习速率的大小对于系统是否收敛有决定性的影响。如果学习速率太大,那么可能导致系统震荡发撒;如果学习速率太小,那么可能导致系统收敛速度变慢。 (a) 根据给定数据架设预测函数h(x) (b) 计算代价函数J (c) 计算各参数偏导 (d) 更新参数 (e) 重复2~4直到代价函数跟新的步长小于设定值或者是重复次数达到预设值。 为了在相同学习速率的前提下加快相同收敛,可采用训练数据归一化的方法来对样本数据进行预处理。采用均值均值归一化处理后,缩小了原来数据的变化幅

7、度,从而可极大地提高学习速率,从而提高了梯度下降的执行速度。在第四部分的matlab代码中,对输入值向量进行归一化处理之后,将学习速率从0.01提高到1.9,从而将获得系统收敛的epoch次数由10000次减少到300次。 2. Matlab实现单变量梯度下降的线性回归 (1). Gradient descend code 1 clear all clc % training sample data; p0=3; p1=7; x=1:3; y=p0+p1*x; num_sample=size(y,2); % gradient descending p

8、rocess % initial values of parameters theta0=1; theta1=3; %learning rate alpha=0.02; % if alpha is too large, the final error will be much large. % if alpha is too small, the convergence will be slow epoch=500; for k=1:epoch v_k=k h_theta_x=theta0+theta1*x; % hypothesis function

9、 Jcost(k)=((h_theta_x(1)-y(1))^2+(h_theta_x(2)-y(2))^2+(h_theta_x(3)-y(3))^2)/num_sample theta0=theta0-alpha*((h_theta_x(1)-y(1))+(h_theta_x(2)-y(2))+(h_theta_x(3)-y(3)))/num_sample; theta1=theta1-alpha*((h_theta_x(1)-y(1))*x(1)+(h_theta_x(2)-y(2))*x(2)+(h_theta_x(3)-y(3))*x(3))/num_s

10、ample; end plot(Jcost) (2). Gradient descend code 2 clear all clc % training sample data; p0=26; p1=73; x=1:3; y=p0+p1*x; num_sample=size(y,2); % gradient descending process % initial values of parameters theta0=1; theta1=3; %learning rate alpha=0.08; % if alpha is too l

11、arge, the final error will be much large. % if alpha is too small, the convergence will be slow epoch=500; for k=1:epoch v_k=k h_theta_x=theta0+theta1*x; % hypothesis function Jcost(k)=((h_theta_x(1)-y(1))^2+(h_theta_x(2)-y(2))^2+(h_theta_x(3)-y(3))^2)/num_sample; theta0=th

12、eta0-alpha*((h_theta_x(1)-y(1))+(h_theta_x(2)-y(2))+(h_theta_x(3)-y(3)))/num_sample; theta1=theta1-alpha*((h_theta_x(1)-y(1))*x(1)+(h_theta_x(2)-y(2))*x(2)+(h_theta_x(3)-y(3))*x(3))/num_sample; % disp('*********comp 1**************'); r1=((h_theta_x(1)-y(1))+(h_theta_x(2)-y(2))+(h_theta_x(3

13、)-y(3))); r2=sum(h_theta_x-y); % disp('*********comp 2**************'); r3=((h_theta_x(1)-y(1))^2+(h_theta_x(2)-y(2))^2+(h_theta_x(3)-y(3))^2); r4=sum((h_theta_x-y).^2); % disp('*********comp 3**************'); r5=((h_theta_x(1)-y(1))*x(1)+(h_theta_x(2)-y(2))*x(2)+(h_theta_x(3)-y(3

14、))*x(3)); r6=sum((h_theta_x-y).*x); if((r1~=r2)||(r3~=r4)||(r5~=r6)) disp('***wrong result******') end end plot(Jcost) 3. 线性回归与单神经元的对应关系 单变量线性回归的模型:   与单神经元模型具有对应的关系,可实现相同的功能。其中θ0相当于单神经元的偏置值bias值,x相当于单神经元的单个输入,θ1相当于单神经元的权值w。如果hθ不包含θ0,那么则可能无法准确完

15、成线性回归的功能。因此,对于单神经元结构而言,偏置值bias是必不可少的。 图1. 单神经元单变量输入与线性回归的对应模型 对于双变量线性回归模型: hθx=θ0+θ1×x1+θ2×x2 而言,可实现双输入单神经元的功能。其偏置值对应θ0。 图2. 单神经元双变量输入与线性回归的对应模型 4. Matlab实现多变量梯度下降的线性回归 (1). 未进行归一化处理之前的matlab代码 两个输入变量x1和x2. clear all clc % training sample data; p0=6; p1=7; p2=2; % the

16、first group of inputs x1=[7 9 12 5 4]; x2=[1 8 21 3 5]; % the second group of inputs x1=[7 9 12 5 4 3]; x2=[1 8 21 3 5 26]; y=p0+p1*x1+p2*x2; num_sample=size(y,2); % gradient descending process % initial values of parameters theta0=9; theta1=3; theta2=9; % theta0=19;theta

17、1=23;theta2=91; % theta0=0;theta1=0;theta2=0; %learning rate alpha=0.01; % good for the system with the first group of inputs alpha=0.005; % good for the system the second group of inputs % alpha=0.02; % bad for the system. the system will be unstable % if alpha is too large, the final er

18、ror will be much large. % if alpha is too small, the convergence will be slow epoch=10000; for k=1:epoch v_k=k h_theta_x=theta0+theta1*x1+theta2*x2; % hypothesis function Jcost(k)=((h_theta_x(1)-y(1))^2+(h_theta_x(2)-y(2))^2+(h_theta_x(3)-y(3))^2+(h_theta_x(4)-y(4))^2+(h_theta_x(5

19、)-y(5))^2)/num_sample; theta0=theta0-alpha*((h_theta_x(1)-y(1)) +(h_theta_x(2)-y(2)) +(h_theta_x(3)-y(3)) +(h_theta_x(4)-y(4)) +(h_theta_x(5)-y(5)))/num_sample; theta1=theta1-alpha*((h_theta_x(1)-y(1))*x1(1)+(h_theta_x(2)-y(2))*x1(2)+(h_theta_x(3)-y(3))*x1(3)+(h_theta_

20、x(4)-y(4))*x1(4)+(h_theta_x(5)-y(5))*x1(5))/num_sample; theta2=theta2-alpha*((h_theta_x(1)-y(1))*x2(1)+(h_theta_x(2)-y(2))*x2(2)+(h_theta_x(3)-y(3))*x2(3)+(h_theta_x(4)-y(4))*x2(4)+(h_theta_x(5)-y(5))*x2(5))/num_sample; % % disp('*********comp 1**************'); % r1=((h_theta_x(1)-y(1))

21、h_theta_x(2)-y(2))+(h_theta_x(3)-y(3))); % r2=sum(h_theta_x-y); % % disp('*********comp 2**************'); % r3=((h_theta_x(1)-y(1))^2+(h_theta_x(2)-y(2))^2+(h_theta_x(3)-y(3))^2); % r4=sum((h_theta_x-y).^2); % % disp('*********comp 3**************'); % r5=((h_theta_x(1)-y(

22、1))*x(1)+(h_theta_x(2)-y(2))*x(2)+(h_theta_x(3)-y(3))*x(3)); % r6=sum((h_theta_x-y).*x); % if((r1~=r2)||(r3~=r4)||(r5~=r6)) % disp('***wrong result******') % end end yt=theta0+theta1*x1+theta2*x2 plot(Jcost) 上述matlab代码中,当学习速率alpha小于等于0.01时,系统能收敛;但是,如果学习速率alpha越小,系统收敛的速度越

23、慢。当学习速率alpha大于等于0.02时,系统发散,无法收敛。为了使得系统尽快收敛下一步采用归一化的方法来加速系统收敛。 (2). 对表达式进行简化后的matlab代码 clear all clc % training sample data; p0=6; p1=7; p2=2; x1=[7 9 12 5 4]; x2=[1 8 21 3 5]; y=p0+p1*x1+p2*x2; num_sample=size(y,2); % gradient descending process % initial values of parameters

24、theta0=9; theta1=3; theta2=9; % theta0=19;theta1=23;theta2=91; % theta0=0;theta1=0;theta2=0; %learning rate alpha=0.005; % good for the system alpha=0.01; % good for the system % alpha=0.02; % bad for the system. the system will be unstable % if alpha is too large, the final error wil

25、l be much large. % if alpha is too small, the convergence will be slow epoch=10000; for k=1:epoch v_k=k h_theta_x=theta0+theta1*x1+theta2*x2; % hypothesis function Jcost(k)=sum((h_theta_x-y).^2)/num_sample; r0=sum(h_theta_x-y); theta0=theta0-alpha*r0/num_sample; r1

26、sum((h_theta_x-y).*x1); theta1=theta1-alpha*r1/num_sample; r2=sum((h_theta_x-y).*x2); theta2=theta2-alpha*r2/num_sample; % Jcost(k)=((h_theta_x(1)-y(1))^2+(h_theta_x(2)-y(2))^2+(h_theta_x(3)-y(3))^2+(h_theta_x(4)-y(4))^2+(h_theta_x(5)-y(5))^2)/num_sample; % theta1=theta1-alp

27、ha*((h_theta_x(1)-y(1))*x1(1)+(h_theta_x(2)-y(2))*x1(2)+(h_theta_x(3)-y(3))*x1(3)+(h_theta_x(4)-y(4))*x1(4)+(h_theta_x(5)-y(5))*x1(5))/num_sample; % theta2=theta2-alpha*((h_theta_x(1)-y(1))*x2(1)+(h_theta_x(2)-y(2))*x2(2)+(h_theta_x(3)-y(3))*x2(3)+(h_theta_x(4)-y(4))*x2(4)+(h_theta_x(5)-y(5))*x2(5

28、))/num_sample; % % disp('*********comp 1**************'); % r1=((h_theta_x(1)-y(1))+(h_theta_x(2)-y(2))+(h_theta_x(3)-y(3))); % r2=sum(h_theta_x-y); % % disp('*********comp 2**************'); % r3=((h_theta_x(1)-y(1))^2+(h_theta_x(2)-y(2))^2+(h_theta_x(3)-y(3))^2); % r4=sum((

29、h_theta_x-y).^2); % % disp('*********comp 3**************'); % r5=((h_theta_x(1)-y(1))*x(1)+(h_theta_x(2)-y(2))*x(2)+(h_theta_x(3)-y(3))*x(3)); % r6=sum((h_theta_x-y).*x); % if((r1~=r2)||(r3~=r4)||(r5~=r6)) % disp('***wrong result******') % end end yt=theta0+theta1*x1+t

30、heta2*x2 plot(Jcost) (3). 进行归一化处理之后的matlab代码 clear all clc % training sample data; p0=6; p1=7; p2=2; x1=[7 9 12 5 4]; x2=[1 8 21 3 5]; x1_mean=mean(x1) x1_max=max(x1) x1_min=min(x1) x2_mean=mean(x2) x2_max=max(x2) x2_min=min(x2) x1=(x1-x1_mean)/(x1_max-x1_min) x2=(x2-x2_

31、mean)/(x2_max-x2_min) % x1=[7 9 12 5 4 3]; % x2=[1 8 21 3 5 26]; y=p0+p1*x1+p2*x2; num_sample=size(y,2); % gradient descending process % initial values of parameters theta0=9; theta1=3; theta2=9; % theta0=19;theta1=23;theta2=91; % theta0=0;theta1=0;theta2=0; %learning r

32、ate alpha=1.9; % good for the system % if alpha is too large, the final error will be much large. % if alpha is too small, the convergence will be slow epoch=300; for k=1:epoch v_k=k h_theta_x=theta0+theta1*x1+theta2*x2; % hypothesis function Jcost(k)=sum((h_theta_x-y).^2)/num_s

33、ample; r0=sum(h_theta_x-y); theta0=theta0-alpha*r0/num_sample; r1=sum((h_theta_x-y).*x1); theta1=theta1-alpha*r1/num_sample; r2=sum((h_theta_x-y).*x2); theta2=theta2-alpha*r2/num_sample; end yt=theta0+theta1*x1+theta2*x2 plot(Jcost) (4). 进行归一化处理之后的matlab代码2 三个个输入

34、变量x1、x2和x3. clear all clc % training sample data; p0=6; p1=7; p2=2; p3=9; x1=[7 9 12 5 4]; x2=[1 8 21 3 5]; x3=[3 2 11 4 8]; x1_mean=mean(x1) x1_max=max(x1) x1_min=min(x1) x1=(x1-x1_mean)/(x1_max-x1_min) x2_mean=mean(x2) x2_max=max(x2) x2_min=min(x2) x2=(x2-x2_mean)/(x2_ma

35、x-x2_min) x3_mean=mean(x3) x3_max=max(x3) x3_min=min(x3) x3=(x3-x3_mean)/(x3_max-x3_min) y=p0+p1*x1+p2*x2+p3*x3; num_sample=size(y,2); % gradient descending process % initial values of parameters theta0=9; theta1=3; theta2=9; theta3=2; % theta0=19;theta1=23;theta2=91; % theta0=0;

36、theta1=0;theta2=0; %learning rate alpha=1.8; % good for the system % alpha=0.01; % good for the system % alpha=0.02; % bad for the system. the system will be unstable % if alpha is too large, the final error will be much large. % if alpha is too small, the convergence will be slow epoch

37、2260; for k=1:epoch v_k=k h_theta_x=theta0+theta1*x1+theta2*x2+theta3*x3; % hypothesis function Jcost(k)=sum((h_theta_x-y).^2)/num_sample; r0=sum(h_theta_x-y); theta0=theta0-alpha*r0/num_sample; r1=sum((h_theta_x-y).*x1); theta1=theta1-alpha*r1/num_sample; r2=sum((h_theta_x-y).*x2); theta2=theta2-alpha*r2/num_sample; r3=sum((h_theta_x-y).*x3); theta3=theta3-alpha*r3/num_sample; end yt=theta0+theta1*x1+theta2*x2+theta3*x3 plot(Jcost) (5). 结论 对输入值向量进行归一化处理之后,将学习速率从0.01提高到1.9,从而将获得系统收敛的epoch次数由10000次减少到300次。 18

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服