## 梯度下降是什幺？

y = x 2 y=x^2 x 2

xNew = xOld – alpha × 导数

xNew： 需要计算的值；
xOld： 上一次的x值，x=2;
alpha：步长，我们赋予步长一个适当的值，比如0.2；

xNew = xOld – alpha × 导数

`xNew = 2 - 0.2 * ( 2 × 2 ) = 2 - 0.8 = 1.2`

`yOld = 2^2 = 4`

`yNew = 1.2 ^ 2 = 1.44`

xNew = 2 – 0.2 * ( 2 × 2 ) = 2 – 0.8 = 1.2 , 此时 y = x 2 = 1. 2 2 = 1.44 y=x^2=1.2^2=1.44 1. 2 2 = 1.44

xNew = 1.2 – 0.2 * ( 2 × 1.2 ) = 1.2 – 0.48 = 0.72 , 此时 y = x 2 = 0.7 2 2 = 0.518 y=x^2=0.72^2=0.518 0.7 2 2 = 0.518

xNew = 0.72 – 0.2 * ( 2 × 0.72 ) = 0.72 – 0.288 = 0.432 , 此时 y = x 2 = 0.43 2 2 = 0.186624 y=x^2=0.432^2=0.186624 0.43 2 2 = 0.186624

xNew = 0.432 – 0.2 * ( 2 × 0.432 ) = 0.432 – 0.1728 = 0.2592 , 此时 y = x 2 = 0.259 2 2 = 0.067185 y=x^2=0.2592^2=0.067185 0.259 2 2 = 0.067185

```# -*- coding: utf-8 -*-
"""
Created on Fri Sep 16 20:34:56 2022
@author: 李立宗

"""
import matplotlib.pyplot as plt
import numpy as np
# 函数：y=x^2
def fx(x):
return x**2
times = 5 # 要计算多少次（迭代次数）
alpha = 0.2 # 学习率，步长
x =2 # x初始值
x_axis = np.linspace(-2, 2) #x轴范围
fig = plt.figure(1,figsize=(5,5)) #显示窗口大小
ax.set_xlabel('X', fontsize=14)
ax.set_ylabel('Y', fontsize=14)
ax.plot(x_axis,fx(x_axis)) #绘图
# 开始多轮计算
for i in range(times):  #控制运行次数
x1 = x
y1= fx(x)
xOld=x
x = x - alpha * 2 * x
y = fx(x)
print("第%d次迭代：x=%f=(%f-%f*(2*%f))，y=%f" % (i + 1, x,xOld,alpha,xOld, y1))
ax.plot([x1,x], [y1,y], 'ko', lw=1, ls='-', color='coral')
plt.show()
if __name__ == "__main__":

## 梯度的理解

```# -*- coding: utf-8 -*-
"""
Created on Sun Sep 18 20:53:55 2022
@author: 李立宗

"""
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10,10,500)
y1 = 1/2*x**2
y2 = x**2
y3 = 2*x**2

plt.plot(x,y1,color = 'r',label = 'y1')
plt.plot(x,y2,'-.', color = 'b', label = 'y2')
plt.plot(x,y3,'--', color = 'g', label = 'y3')
plt.legend(loc=' best')
plt.show()```

## 几个疑惑

### 疑惑1：为什幺一定要沿着梯度的反方向走才能找到最小值

2x是增加y值方向(梯度方向)
-2x是减少y值方向（梯度反方向）

## 梯度下降应用

y=kx+b

y：消费总额
k：单价
x：土豆重量
b：快递费

y=kx+b

1.马同学、梯度下降的通俗理解

2.King [email protected],策略产品经理必读系列—第八讲梯度下降法,https://zhuanlan.zhihu.com/p/335191534

3.深入浅出–梯度下降法及其实现，https://www.jianshu.com/p/c7e642877b0e

4.https://www.jiqizhixin.com/articles/2019-04-07-6

5.https://zhuanlan.zhihu.com/p/107782332?utm_source=wechat_session&utm_medium=social&utm_oi=63655349583872&utm_campaign=shareopn

1.马同学、梯度下降的通俗理解

2.King [email protected],策略产品经理必读系列—第八讲梯度下降法,https://zhuanlan.zhihu.com/p/335191534

3.深入浅出–梯度下降法及其实现，https://www.jianshu.com/p/c7e642877b0e

4.https://www.jiqizhixin.com/articles/2019-04-07-6

5.https://zhuanlan.zhihu.com/p/107782332?utm_source=wechat_session&utm_medium=social&utm_oi=63655349583872&utm_campaign=shareopn

6.文中山谷图片：https://pixabay.com/photos/landscape-fjords-norway-moutains-4933256/