B站up主“刘二大人”视频 笔记

forward：输出是预测值y_hat

cost：损失函数定义为MSE：均方根误差

forward函数没有变化；

```import numpy as np
import matplotlib.pyplot as plt
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w = 1.0  # 初始权重的猜测
def forward(x):  # 前馈计算
return x*w   # 线性模型y_hat=x*w
def cost(xs, ys):  # 计算MSE（平均平方误差）
cost = 0
for x, y in zip(xs, ys):
y_pre = forward(x)
cost += (y_pre - y) ** 2  # 平方后与cost求和
return cost/len(xs)
for x, y in zip(xs, ys):
epoch_list = []  # 权重
cost_list = []  # 对应权重的损失值
print('predict (before training)', 4, forward(4))
for epoch in range(100):
cost_val = cost(x_data, y_data)
w -= 0.01 * grad_val  # 0.01为学习率，w=w - 学习率*梯度
print('Epoch:', epoch, 'w=', w, 'loss=', cost_val)  # epoch为当前第几轮训练，w为当前权重，loss为当前损失值
epoch_list.append(epoch)
cost_list.append(cost_val)
print('Predict (after training)', 4, forward(4))
'''
w_list = []  # 权重
mse_list = []  # 对应权重的损失值
for w in np.arange(0.0, 4.1, 0.1):  # w的取值是0.0---4.1，每间隔0.1取一个值
print('w=', w)
l_sum = 0
for x_val, y_val in zip(x_data, y_data):
y_pre_val = forward(x_val)
loss_val = loss(x_val, y_val)
l_sum += loss_val
print('\t', x_val, y_val, y_pre_val, loss_val)
print('MSE', l_sum/3)
w_list.append(w)
mse_list.append(l_sum/3)
'''
plt.plot(epoch_list, cost_list)
plt.ylabel('cost')
plt.xlabel('epoch')
plt.grid()
plt.show()```

```"""随机梯度下降"""
import numpy as np
import matplotlib.pyplot as plt
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w = 1.0  # 初始权重的猜测
def forward(x):  # 前馈计算
return x*w   # 线性模型y_hat=x*w
def loss(x, y):  # 求一个样本的损失
y_pre = forward(x)
return (y_pre - y) ** 2
return 2 * x * (x * w - y)   # 对损失函数loss求偏导
epoch_list = []  # 权重
cost_list = []  # 对应权重的损失值
print('predict (before training)', 4, forward(4))
for epoch in range(100):  # 对每一个样本的梯度进行更新
for x, y in zip(x_data, y_data):
w -= 0.01 * grad  # 0.01为学习率，w=w - 学习率*梯度
l = loss(x, y)
print('progress:', epoch, 'w=', w, 'loss=', l)  # epoch为当前第几轮训练，w为当前权重，loss为当前损失值
epoch_list.append(epoch)
cost_list.append(l)
print('Predict (after training)', 4, forward(4))
plt.plot(epoch_list, cost_list)
plt.ylabel('cost')
plt.xlabel('epoch')
plt.grid()
plt.show()```

α为学习率，梯度下降使用了贪心算法的思想