使用Numpy实现机器学习
表达式: y = 3 x 2 + 2 y=3x^2+2 y=3x2+2
模型: y = w x 2 + b y=wx^2+b y=wx2+b
损失函数: L o s s = 1 2 ∑ i = 1 100 ( w x i 2 + b − y i ) 2 Loss=\frac{1}{2}\sum_{i=1}^{100}(wx^2_i+b-y_i)^2 Loss=21∑i=1100(wxi2+b−yi)2
对损失函数求导:
∂
L
o
s
s
∂
w
=
∑
i
=
1
100
(
w
x
i
2
+
b
−
y
i
)
2
x
i
2
\frac{\partial Loss}{\partial w}=\sum_{i=1}^{100}(wx^2_i+b-y_i)^2x^2_i
∂w∂Loss=∑i=1100(wxi2+b−yi)2xi2
∂ L o s s ∂ b = ∑ i = 1 100 ( w x i 2 + b − y i ) 2 \frac{\partial Loss}{\partial b}=\sum_{i=1}^{100}(wx^2_i+b-y_i)^2 ∂b∂Loss=∑i=1100(wxi2+b−yi)2
利用梯度下降法学习参数,学习率为:lr
w 1 − = l r ∗ ∂ L o s s ∂ w w_1-=lr*\frac{\partial Loss}{\partial w} w1−=lr∗∂w∂Loss
b 1 − = l r ∗ ∂ L o s s ∂ b b_1-=lr*\frac{\partial Loss}{\partial b} b1−=lr∗∂b∂Loss
import numpy as np
from matplotlib import pyplot as plt
1.生成训练数据
#设置随机种子,生成同一份数据
np.random.seed(100)
x = np.linspace(-1, 1, 100).reshape(100, 1)
# y在真实值上增加噪声
y = 3*np.power(x, 2)+2+0.2*np.random.rand(x.size).reshape(100, 1)
2.查看x,y分布
plt.scatter(x, y)
plt.show()
3.初始化权重参数
# 随即初始化参数
w1 = np.random.rand(1, 1)
b1 = np.random.rand(1, 1)
4.求解模型
lr = 0.001
for i in range(800):
# 前向传播
y_pred = np.power(x, 2)*w1+b1
# 定义损失函数
loss = 0.5 * (y_pred-y)**2
# print(loss)
# 对各维度求和
loss = loss.sum()
# 计算梯度(求导)
grad_w = np.sum((y_pred-y)*np.power(x, 2))
grad_b = np.sum((y_pred-y))
# 使用梯度下降法,使得loss最小
w1 -= lr*grad_w
b1 -= lr*grad_b
5.结果可视化
plt.plot(x, y_pred, 'r-', label='predict')
plt.scatter(x, y, color='blue', marker='o', label='true')
plt.xlim(-1, 1)
plt.ylim(2, 6)
plt.legend()
plt.show()
# 预测值
print(w1, b1)
[[2.98927619]] [[2.09818307]]