2.1神经元模型
- 提出了模拟生物神经元机制的人工神经网络的数学模型
- 进一步发展成感知机(Perceptron)模型
- 均方误差:
MSE
2.2优化方法
- 这种通过严格的公式推导出的精确解也称为解析解
- 数值解:无法得到解析解.通过数值优化去优化出一个近似解
- 梯度下降法:
Gradient Descent
2.3线性模型实战
import numpy as np
import numpy.random
data = []
for i in range(100):
x = np.random.uniform(-10., 10.)
eps = numpy.random.normal(0., 0.01)
y = 1.477 * x + 0.089 + eps
data.append([x, y])
data = np.array(data)
def mse(b, w, points):
total_error = 0
for i in range(0, len(points)):
x = points[i, 0]
y = points[i, 1]
total_error += (y - (w * x + b)) ** 2
return total_error / len(points)
def step_gradient(b_current, w_current, points, lr):
'''
计算误差函数在所有点上的导数,并更新w,b
'''
b_gradient = 0
w_gradient = 0
M = len(points)
for i in range(M):
x = points[i, 0]
y = points[i, 1]
b_gradient += (2 / M) * ((w_current * x + b_current) - y)
w_gradient += (2 / M) * x * ((w_current * x + b_current) - y)
new_b = b_current - lr * b_gradient
new_w = w_current - lr * w_gradient
return [new_b, new_w]
def gradient_descent(points, starting_b, starting_w, lr, num_iterations):
b = starting_b
w = starting_w
losses = []
for step in range(num_iterations):
b, w = step_gradient(b, w, np.array(points), lr)
loss = mse(b, w, points)
losses.append(loss)
return [b, w, losses]
if __name__ == '__main__':
lr = 0.01
inital_b = 0
inital_w = 0
num_iteration = 1000
[b, w, losses] = gradient_descent(data, inital_b, inital_w, lr, num_iteration)
loss = mse(b, w, data)
print(f'loss: {loss} w:{w},b:{b}')
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
plt.plot(range(num_iteration), losses)
plt.show()