# 梯度下降原理

最小化损失函数$$L(\theta)$$, 得到参数$$\theta$$在损失最小情况下的对应值. 梯度下降法的迭代步骤如下:

选取初值$$\theta\_{0}$$, 步长$$\alpha$$和算法的终止距离$$\varepsilon$$.

* 确定当前参数下梯度函数的梯度, 对于第$$t$$轮, 对应的参数为$$\theta\_{t}$$, 此时的梯度为$$\frac{\delta L(\theta)}{\delta \theta\_{t}}$$
* 用步长$$\alpha$$乘以损失函数的梯度, 得到当前步的下降长度, 即$$\alpha\frac{\delta L(\theta)}{\delta \theta\_{t}}$$
* 判断当前下降步幅$$\alpha\frac{\delta L(\theta)}{\delta \theta\_{t}}$$是否小于$$\varepsilon$$, 如果小于这个阈值, 停止迭代, $$\theta\_{t}$$即为最终的结果
* 更新$$\theta\_{t}$$: $$\theta\_{t+1}=\theta\_{t}-\frac{\delta L(\theta)}{\delta \theta\_{t}}$$

## 为什么梯度的负方向是局部下降最快的方向

需要结合泰勒展开说明.

在第$$t$$轮迭代中, 将$$L(\theta\_{t})$$在$$\theta\_{t-1}$$处进行一阶泰勒展开, 得到:

$$
\begin{aligned}
L\left(\theta\_{t}\right) &=L\left(\theta\_{t-1}+\Delta \theta\right) \\
& \approx L\left(\theta\_{t-1}\right)+L^{\prime}\left(\theta\_{t-1}\right) \Delta \theta
\end{aligned}
$$

我们的目的是使得损失函数逐渐缩小, 就要使得$$L(\theta\_{t}) \lt L(\theta\_{t-1})$$. 根据泰勒一阶展开式, 有:

$$L\left(\theta\_{t-1}+\Delta \theta\right)-L\left(\theta\_{t-1}\right) \approx L^{\prime}\left(\theta\_{t-1}\right) \Delta \theta$$

参数向量$$\theta$$中包含多个变量参数, 因此损失函数对参数的导数$$L^{\prime}\left(\theta\_{t-1}\right)$$以及参数微小的变化量$$\Delta \theta$$也均为向量, 则$$L^{\prime}\left(\theta\_{t-1}\right) \Delta \theta$$是两个向量的点积.

而$$L^{\prime}\left(\theta\_{t-1}\right) \Delta \theta$$作为损失函数迭代一步的变化量的近似, 我们想要下降的越快越好, 也就是要求这个近似量的值越小越好.

对于两个向量的点积, 其最大值就是两个向量的方向相同时, 点积值最大. $$L^{\prime}\left(\theta\_{t-1}\right)$$正是损失函数在$$\theta\_{t-1}$$位置的梯度. 考虑此时$$\Delta \theta$$的取值, 其模长由步长$$\alpha$$控制, 我们只需要考虑其方向. 前面说了两个向量同向时点积值最大, 反过来, 两个向量反向时点积值最小, 因此取$$\Delta \theta=-\alpha L^{\prime}\left(\theta\_{t-1}\right)$$

这就是梯度负方向是局部下降最快方向的原因.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kerasnoone.gitbook.io/garnet/shen-jing-wang-luo/ti-du/ti-du-xia-jiang-fa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
