Batch Gradient Descent

本文介绍了批量梯度下降法在优化线性回归中的应用,详细阐述了成本函数、学习曲线和梯度下降公式,并探讨了如何选择合适的步长以确保算法收敛。内容包括不同参数数量的学习曲线和迭代过程的可视化,强调了适当步长选择的重要性。

Batch Gradient Descent

We use linear regression as example to explain this optimization algorithm.

1. Formula

1.1. Cost Function

We prefer residual sum of squared to evaluate linear regression.

J(θ)=12mi=1n[hθ(xi)yi]2

1.2. Visualize Cost Function

E.g. 1 :

one parameter only θ1 –> hθ(x)=θ1x1

Learning Curve 1

1. Learning Curve 1 [1]


E.g. 2 :

two parameters θ0,θ1 –> hθ(x)=θ0+θ1x1

Learning Curve 2

2. Learning Curve 2 [2]


Switch to contour plot

Learning Curve 2 - contour

3. Learning Curve 2 - contour[2]


1.3. Gradient Descent Formula

For all θi

Jθθi=1mi=1n[hθ(xi)yi](xi)

E.g.,
two parameters θ0,θ1 –> hθ(x)=θ0+θ1x1

For i = 0 :

Jθθ0=1mi=1n[hθ(xi)yi](x0)

For i = 1:

Jθθ1=1mi=1n[hθ(xi)yi](x1)
% Octave
%% =================== Gradient Descent ===================
% Add a column(x0) of ones to X

X = [ones(len, 1), data(:,1)];
theta = zeros(2, 1);
alpha = 0.01;
ITERATION = 1500;
jTheta = zeros(ITERATION, 1);

for iter = 1:ITERATION
    % Perform a single gradient descent on the parameter vector
    % Note: since the theta will be updated, a tempTheta is needed to store the data.
    tempTheta = theta;
    theta(1) = theta(1) - (alpha / len) * (sum(X * tempTheta - Y));  % ignore the X(:,1) since the values are all ones.
    theta(2) = theta(2) - (alpha / len) * (sum((X * tempTheta - Y) .* X(:,2)));

    %% =================== Compute Cost ===================
    jTheta(iter) = sum((X * theta - Y) .^ 2) / (2 * len);
endfor

2. Algorithm

For all θi

θi:=θiαθiJ(θ1,θ2,,θn)

E.g.,
two parameters θ0,θ1 –> hθ(x)=θ0+θ1x1

For i = 0 :

θ0:=θ0α1mi=1n[hθ(xi)yi]

For i = 1 :

θ1:=θ1α1mi=1n[hθ(xi)yi](x1)

Iterative for multiple times (depends on data content, data size and step size). Finally, we could see the result as below.

Converge
Visualize Convergence

3. Analyze

ProsCons
Controllable by manuplate stepsize, datasizeComputing effort is large
Easy to program

4. How to Choose Step Size?

Choose an approriate step size is significant. If the step size is too small, it doesn’t hurt the result, but it took even more times to converge. If the step size is too large, it may cause the algorithm diverge (not converge).

The graph below shows that the value is not converge since the step size is too big.

Large Step Size
Large Step Size

The best way, as far as I know, is to decrease the step size according to the iteration times.

E.g.,

α(t+1)=αtt

or

α(t+1)=αtt

Reference

  1. 机器学习基石(台湾大学-林轩田)\lecture_slides-09_handout.pdf

  2. Coursera-Standard Ford CS229: Machine Learning - Andrew Ng

### 回答1: 批量梯度下降是一种机器学习中的优化算法,用于最小化损失函数。它通过计算所有训练样本的梯度来更新模型参数,因此也被称为全批量梯度下降。相比于随机梯度下降和小批量梯度下降,批量梯度下降的计算量较大,但通常能够获得更好的收敛性和稳定性。 ### 回答2: 批量梯度下降是一种用于训练机器学习模型的优化算法。它是梯度下降算法的一种变体,区别在于在每一次迭代中,批量梯度下降会使用整个训练数据集来计算损失函数的梯度。 在批量梯度下降算法中,首先需要定义一个损失函数,它可以是均方误差、交叉熵等。然后初始化模型参数,例如权重和偏置。接下来,对于每一次迭代,批量梯度下降会使用所有的训练样本计算梯度,并更新模型参数。这意味着每一次迭代都需要遍历整个训练数据集。 批量梯度下降的优点是在选择合适的学习率的情况下,可以更快地收敛到全局最优解。然而,由于每次迭代都需要使用所有的训练样本进行计算,批量梯度下降的计算开销较大,尤其是当训练数据集很大时。此外,批量梯度下降可能会停留在局部最优解附近,而无法找到全局最优解。 为了解决批量梯度下降的缺点,研究人员提出了随机梯度下降和小批量梯度下降等变种算法。随机梯度下降每次迭代只使用一个样本进行更新,而小批量梯度下降则会使用固定大小的样本批次进行计算。这些变种算法在减少计算开销的同时,往往需要更多的迭代才能收敛到最优解。 总而言之,批量梯度下降是一种使用整个训练数据集来计算梯度的优化算法。它的主要优点是可以更快地收敛到全局最优解,但计算开销较大。为了解决这些问题,人们提出了随机梯度下降和小批量梯度下降等改进算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值