1. 引入
已知函数表达式为:
y = w * x + b
如果求未知数w和b,可以给定两个点,联立方程求解,如下图所示:
现在给满足此等式的点加入噪声,通过这一系列的点求出w和b的近似值,评价近似值是否最优的的指标为损失函数,如下图所示:
如何最优化?
2. 实例代码
求解过程:
给定初始的w和b的值,利用给定的100个观测值按梯度步长逐渐让w和b逼近到其各自的真实值。
参数 = 参数 - 学习率 * 梯度
import numpy as np
# y = wx + b
# 计算损失函数的值
def compute_error_for_line_given_points(b, w, points):
totalError = 0
for i in range(0, len(points)):
x = points[i, 0]
y = points[i, 1]
totalError += (y - (w * x + b)) ** 2
return totalError / float(len(points))
#
def step_gradient(b_current, w_current, points, learningRate):
b_gradient = 0
w_gradient = 0
N = float(len(points)) # 100个点
for i in range(0, len(points)):
x = points[i, 0]
y = points[i, 1]
# 所有观测点在(w_gradient,b_gradient)处的梯度的和的平均值
b_gradient += -(2/N) * (y - ((w_current * x) + b_current))
w_gradient += -(2/N) * x * (y - ((w_current * x) + b_current))
new_b = b_current - (learningRate * b_gradient)
new_w = w_current - (learningRate * w_gradient)
print("new_b, new_w",new_b, new_w)
return [new_b, new_w]
def gradient_descent_runner(points, starting_b, starting_w, learning_rate, num_iterations):
b = starting_b
w = starting_w
for i in range(num_iterations):
b, w = step_gradient(b, w, np.array(points), learning_rate)
return [b, w]
def run():
points = np.genfromtxt("data.csv", delimiter=",")
learning_rate = 0.0001
initial_b = 0 # initial y-intercept guess
initial_w = 0 # initial slope guess
num_iterations = 1000
print("Starting gradient descent at b = {0}, m = {1}, error = {2}"
.format(initial_b,
initial_w,
compute_error_for_line_given_points(initial_b, initial_w, points))
)
print("Running...")
[b, w] = gradient_descent_runner(points, initial_b, initial_w, learning_rate, num_iterations)
print("After {0} iterations b = {1}, m = {2}, error = {3}".
format(num_iterations,
b,
w,
compute_error_for_line_given_points(b, w, points))
)
if __name__ == '__main__':
run()
执行结果:
Starting gradient descent at b = 0, m = 0, error = 5565.107834483211
Running...
After 1000 iterations b = 0.08893651993741346, m = 1.4777440851894448, error = 112.61481011613473