import numpy as np
x = np.linspace(1, 10, 10)
y = x + 0.5
w = np.random.rand() * 10
b = np.random.rand()
lr = 0.001
for n in range(1000):
loss = np.zeros_like(x)
for i in range(len(x)):
y_ = w * x[i] + b
loss[i] = (y[i] - y_) ** 2
w = w - lr * x[i]
b = b - lr
mse = loss.mean()
print(n, ':', mse)
if (mse < 0.01):
break
print(w, b)
网友评论