import numpy as np
from matplotlib import pyplot as plt
np.random.seed(100)
x = np.linspace(-1, 1, 100).reshape(100, 1)
y = 3 * np.power(x, 2) + 2 + 0.2 * np.random.randn(x.size).reshape(100, 1)
# plt.scatter(list(x), list(y))
# plt.show()
w1 = np.random.rand(1, 1)
b1 = np.random.rand(1, 1)
# print(w1) # [[0.40288033]]
# print(b1) # [[0.3542983]]
lr = 0.001
for i in range(800):
# 损失函数, 只作为收敛的观察,不参与后续的计算
y_predict = w1 * np.power(x, 2) + b1
loss = 0.5 * (y_predict - y) ** 2
loss = loss.sum()
# 计算梯度
grad_w = np.sum((y_predict - y) * np.power(x, 2))
grad_b = np.sum((y_predict - y))
# 使用梯度下降法
w1 -= lr * grad_w
b1 -= lr * grad_b
plt.plot(x, y_predict, '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)