# 计算当前损失, y = w * x + b
# points为数据,维度[100,2]
# w, b 为当前的参数值
def compute_error_for_line_given_points(b, w, points):
N = len(points)
total_error = 0
for sample in points:
x = sample[0]
y = sample[1]
# compute mean-squared-error(MSE)
total_error += (y - (w * x + b)) ** 2
return total_error/N
step2,梯度计算
# 计算当前梯度
def step_gradient(b_current, w_current, points, learning_rate):
b_gradinet = 0
w_gradient = 0
N = len(points)
for sample in points:
x = sample[0]
y = sample[1]
# grad_b,计算参数 b 的梯度
b_gradinet += (2/N) * (w_current * x + b_current - y)
# grad_w,计算参数 W 的梯度
w_gradient += (2/N) * x * (w_current * x + b_current - y)
# 利用梯度更新参数
new_b = b_current - learning_rate * b_gradinet
new_w = w_current - learning_rate * w_gradient
return [new_b,new_w]
step3,循环迭代
def gradient_descent_runner(points, starting_b, starting_w, learning_rate, num_iterations):
b = starting_b
w = starting_w
# 通过循环固定轮次更新参数
for _ in range(num_iterations):
b, w = step_gradient(b,w,points,learning_rate)
return [b, w]