Stochastic Gradient Descent收敛判断及收敛速度的控制

要判断Stochastic Gradient Descent是否收敛,可以像Batch Gradient Descent一样打印出iteration的次数和Cost的函数关系图,然后判断曲线是否呈现下降且区域某一个下限值的状态。由于训练样本m值很大,而对于每个样本,都会更新一次θ向量(权重向量),因此可以在每次更新θ向量前,计算当时状况下的cost值,然后每1000次迭代后,计算一次average cost的值。然后打印出iteration和cost之间的关系。

1、不同曲线图代表的含义及应对策略

可能会看到的曲线图有如下几种:

情况1

这样的曲线说明算法已经收敛。

如果我们使用小一点的学习率α,那么可能最终会训练到比较好的θ向量(红色线)

但是小的学习率也意味着更长的训练时间。

情况2

如果我们不是1000次迭代计算并打印一次,而是5000次迭代后才计算并打印一次。那么曲线可能会更加平滑一些(绿色线)。

情况3

如果我们得到的曲线(1000次迭代并打印)是波动很剧烈,并且没有显示任何下降趋势,如下图:

那么有两种可能,一噪声太剧烈而无法看出算法收敛的趋势;二算法没有收敛。

这种情况下,我们可以调整打印的步长(比如5000次迭代才计算并打印一次),那么可能会得到两种不同的曲线(如下两幅图所示)。

如果得到得是类似这条红色的曲线,那么说明算法已经收敛或已经表现出收敛的趋势了。如果得到的是如下图所示的绿色的线,说明算法没有收敛。

情况4

还有一种情况,就是曲线不但没有呈现下降的趋势,反而出现了上升的趋势,如下图:

这说明学习率α设置得过大,需要调小学习率。

 

2、学习率的设置

当学习率比较小的时候,可以训练出更优的权重向量。但是较小的学习率也意味着更长的训练时间,而且如果是非凸问题则还有可能会陷入局部解中。那么,如果使用动态递减的学习率(即在学习开始之初,学习率较大,然后根据迭代次数的增加,学习率逐渐减小)也许会好一些。这样我们可以用一个式子来按照迭代次数调整学习率,例如:

常量1和常量2的目的是为了保证学习率在一个正常的范围内(不至于当循环次数很高或很低时,学习率会变得过大或过小)。

通过调整学习率(手工或如上式自动调整),就可以控制算法收敛的速度。

 

Reference:

Andrew Ng Stochastic Gradient Descent Convergence (12 min)

The stochastic gradient descent (SGD) algorithm is a popular optimization algorithm used in machine learning. It is an iterative algorithm that updates the model parameters in small steps based on the gradient of the loss function with respect to the parameters. The algorithm works as follows: 1. Initialize the model parameters randomly. 2. Set the learning rate, which determines the step size of the updates. 3. For each training example: - Compute the gradient of the loss function with respect to the parameters using the current example. - Update the model parameters by subtracting the gradient multiplied by the learning rate. The key difference between SGD and regular gradient descent is that in SGD, the gradient is computed and the parameters are updated for each training example, rather than for the entire training set. This makes the algorithm faster and more scalable for large datasets. The stochastic aspect of the algorithm comes from the fact that the training examples are sampled randomly from the training set, rather than being processed in a fixed order. This randomness can help the algorithm escape from local minima and find better solutions. Here is the pseudocode for the SGD algorithm: ``` Input: Training set (X, Y), learning rate α, number of iterations T Output: Model parameters θ Initialize θ randomly for t = 1 to T do Sample a training example (x, y) from (X, Y) randomly Compute the gradient ∇θ L(θ; x, y) using the current example Update the parameters: θ ← θ - α * ∇θ L(θ; x, y) end for return θ ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值