梯度下降的原理应用在监督学习的各个算法中,它的作用至关重要。但是,在学习过程中萌发了一个疑问,偏导数代表函数值在某个点某个变量上的变化方向和速度(变量变化1个单位,函数值变化偏导数个单位,此单位越小越精确)。为什么迭代式寻找函数最小值时,该变量的偏导数可以用作每次迭代的步进幅度呢?本文通过实验,解答了这个问题。
重温公式
以一次线性函数的拟合为例,训练集(x,y)∈(Rm,Rm)(x,y)\in( \reals ^m, \reals ^m)(x,y)∈(Rm,Rm),学习速率 α\alphaα 是标量:
-
预测函数: hθ(x)=θ0+θ1∗xh_{\theta}(x)=\theta_0 + \theta_1 * xhθ(x)=θ0+θ1∗x
-
代价函数:J(θ0,θ1)=12m∑i=1m(hθ(xi)−yi)2J(\theta_0,\theta_1)=\frac{1}{2m}\sum_{i=1}^{m}(h_{\theta}(x^i)-y^i)^2J(θ0,θ1)=2m1∑i=1m(hθ(xi)−yi)2
-
梯度下降:
- θ0:=θ0−α∂∂θ0J(θ0,θ1)\theta_0 := \theta_0 - \alpha\frac{\partial }{\partial \theta_0}J(\theta_0,\theta_1)θ0:=θ0−α∂θ0∂J(θ0,θ1)
- θ1:=θ1−α∂∂θ1J(θ0,θ1)\theta_1 := \theta_1 - \alpha\frac{\partial }{\partial \theta_1}J(\theta_0,\theta_1)θ1:=θ1−α∂θ1∂J(θ0,θ1)
-
梯度下降展开式:
- ∂∂θ0J(θ0,θ1)=θ0−α1m∑i=1m(hθ(xi)−yi)=a∗θ0+b (a=1)\frac{\partial }{\partial \theta_0}J(\theta_0,\theta_1) = \theta_0 - \alpha\frac{1}{m}\sum_{i=1}^{m}(h_\theta(x^i)-y^i) = a*\theta_0+b \space (a=1)∂θ0∂J(θ0,θ1)=θ0−αm1∑i=1m(hθ(xi)−yi)=a∗θ0+b (a=1)
- ∂∂θ1J(θ0,θ1)=θ0−α1m∑i=1m(hθ(xi)−yi)xi=c∗θ1+d (c>0)\frac{\partial }{\partial \theta_1}J(\theta_0,\theta_1) = \theta_0 - \alpha\frac{1}{m}\sum_{i=1}^{m}(h_\theta(x^i)-y^i)x^i = c*\theta_1+d \space (c>0)∂θ1∂J(θ0,θ1)=θ0−αm1∑i=1m(hθ(xi)−yi)xi=c∗θ1+d (c>0)
实验
function theta = test_grad(theta, alpha, num_iters)
load('test_alpha_train.mat');
figure 1
subplot(3,2,1)
plot(x, y, 'ro');
title('Data Set And Regress Result')
m = length(y);
X = [ones(m, 1) x];
J_history = [];
Theta_history = [];
Grad_history = [];
itv = num_iters/100;
for iter = 1:num_iters
grad = X'*(X*theta-y)/m;
theta = theta - alpha*grad;
if mod(iter,itv) == 0
delt = X*theta-y;
J_history = [J_history;(delt'*delt)/2/m];
Theta_history = [Theta_history theta];
Grad_history = [Grad_history grad];
end
end
y_pre = X*theta;
hold on
plot(x, y_pre)
hold off
t = 1:(length(J_history));
subplot(3,2,2)
plot(t, J_history)
title('History of Costfunction J')
subplot(3,2,3)
plot(t, Theta_history(1,:))
title('History of Theta0')
subplot(3,2,4)
plot(t, Theta_history(2,:), 'r')
title('History of Theta1')
subplot(3,2,5)
plot(t, Grad_history(1,:))
title('History of Grad on Theta0')
subplot(3,2,6)
plot(t, Grad_history(2,:), 'r')
title('History of Grad on Theta1')
end
test_grad([0;0], 0.00002,300000)
- 尝试将近 10 次后确定 α=0.00002\alpha=0.00002α=0.00002,在迭代 300000 次后收敛
- 下图为测试数据:
- 图1:收敛后的拟合情况,图2:迭代过程中代价函数的值,图3:迭代过程中 θ0\theta_0θ0 的值,图4:迭代过程中 θ1\theta_1θ1 的值,图5:迭代过程中 θ0\theta_0θ0 的偏导数值,图6:迭代过程中 θ1\theta_1θ1 的偏导数值;
- 偏导数的绝对值一直在向 0 逼近,说明在逐步接近最低点;
- θ0\theta_0θ0 和 θ1\theta_1θ1 变化的绝对值相差 3 个数量级,但是变化的速率(幅度比例)几乎一致;

结论
学习速率 α\alphaα 是标量,我把它理解为梯度下降中一个"恒定"的梯度;偏导数 ∂∂θjJ(θ0,θ1)\frac{\partial }{\partial \theta_j}J(\theta_0,\theta_1)∂θj∂J(θ0,θ1) 代表下降的方向和幅度;因为不同变量与最小值的"距离"不同(实验中 θ0\theta_0θ0 是 318,θ1\theta_1θ1 是 0.49),要通过同样的迭代次数达到最小值,每次迭代的幅度也不相同。
277

被折叠的 条评论
为什么被折叠?



