欢迎来到天天文库
浏览记录
ID:56800277
大小:541.00 KB
页数:19页
时间:2020-07-12
《带激活函数的梯度下降及线性回归算法和matlab代码2017.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、带激活函数的梯度下降及线性回归和matlab代码1.单变量线性回归带输入输出数据归一化处理我们知道,对于输入数据进行归一化处理能极大地提高神经网络的学习速率,从而提高神经网络的收敛速度。对于同样的学习速率,未经过归一化数据处理的同样的数据集训练神经网络时,将会出现发散的现象,导致神经网络无法收敛。神经网络的激活函数(比如Relu、Sigmoid函数)会导致神经网络的输出节点只能输出正值,无法输出负值。此时由于激活函数的输出值范围是正值,因此神经网络的期望输出值也是正值。万一当神经网络的训练数据集
2、中包含负值时,可对输出数据集进行加一个最大负值的绝对值的操作,使得神经网络的期望输出值全部为正值。如果神经网络的输出值的范围比较大,也可以对神经网络输出值进行归一化处理。如果神经元包含激活函数,则激活函数会自动使得神经元的输出为非负值。(1).未进行输入数据归一化处理的代码clearallclc%trainingsampledata;p0=3;p1=7;x=1:3;y=p0+p1*x;num_sample=size(y,2);%gradientdescendingprocess%initialv
3、aluesofparameterstheta0=1;theta1=3;%learningratealpha=0.33;%ifalphaistoolarge,thefinalerrorwillbemuchlarge.%ifalphaistoosmall,theconvergencewillbeslowepoch=100;fork=1:epochv_k=kh_theta_x=theta0+theta1*x;%hypothesisfunctionJcost(k)=((h_theta_x(1)-y(1)
4、)^2+(h_theta_x(2)-y(2))^2+(h_theta_x(3)-y(3))^2)/num_sampletheta0=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))/n
5、um_sample;endplot(Jcost)yn=theta0+theta1*x上述未进行输入数据归一化处理的代码最大的学习速率为0.35,在迭代次数到达60次时,输出误差下降至0.0000。(1).进行输入数据归一化处理的代码clearallclc%trainingsampledata;p0=3;p1=7;x=1:3;x_mean=mean(x)x_max=max(x)x_min=min(x)xn=(x-x_mean)/(x_max-x_min)x=xn;y=p0+p1*xy=y+0.5;
6、num_sample=size(y,2);%gradientdescendingprocess%initialvaluesofparameterstheta0=1;theta1=3;%learningratealpha=0.9;%ifalphaistoolarge,thefinalerrorwillbemuchlarge.%ifalphaistoosmall,theconvergencewillbeslowepoch=100;fork=1:epochv_k=kh_theta_x=theta0+t
7、heta1*x;%hypothesisfunctionJcost(k)=((h_theta_x(1)-y(1))^2+(h_theta_x(2)-y(2))^2+(h_theta_x(3)-y(3))^2)/num_sampletheta0=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(
8、1)+(h_theta_x(2)-y(2))*x(2)+(h_theta_x(3)-y(3))*x(3))/num_sample;endyn=theta0+theta1*x;plot(Jcost)上述进行输入数据归一化处理的代码最大的学习速率为0.96,在迭代次数到达33次时,输出误差下降至0.0000。上述代码为了使得输出值中不包含负数,对所有输出值都加了0.5。这个0.5被反映至theta0的值上。Theta0的值比p0的值多0.5。(1).输出带sigmoid激活函数的线性回归算法matl
此文档下载收益归作者所有