最简单的Traing: 人工Training
监督学习: 通过训练已知数据集结果的模型进行训练,然后用训练好的模型来预测未知测试集的结果达到某一功能的过程。
使模型尽可能准确,训练集要尽可能接近用户上传的数据 |
---|
泛化能力: 训练完之后,对于没见过的数据也能正确地识别,将训练集再进行划分,做评估,之后再测试,提高泛化能力。
y = xw + b ⇒ 简化 ⇒ y = xw,比较随机权重得到的y与实际的y直接的平方差
计算损失,取w,使损失最小
一个样本的损失
一个训练集上的 平均平方误差 MSE
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] # 输出y
# 定义前馈模型
def forward(x):
return x*w
# 定义损失函数
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) ** 2
w_list = [] # 权重列表
mse_list = [] # 损失值列表
for w in np.arange(0.0, 4.1, 0.1).round(1): # 生成一组序列 0.0 - 4.1, 以0.1为间隔
# w = round(w, 1) # 默认步长是整数,需要精确到小数点后一位
print('w=', w)
l_sum = 0
for x_val, y_val in zip(x_data, y_data):
y_pred_val = forward(x_val)
loss_val = loss(x_val, y_val)
l_sum += loss_val
print('\t', x_val, y_val, y_pred_val, loss_val)
print('MSE=', l_sum / 3)
w_list.append(w)
mse_list.append(l_sum / 3)
# 绘图:以w为横坐标,mse为纵坐标
plt.plot(w_list, mse_list)
plt.xlabel('w')
plt.ylabel('Loss')
plt.show()
输出:
w= 0.0
1.0 2.0 0.0 4.0
2.0 4.0 0.0 16.0
3.0 6.0 0.0 36.0
MSE= 18.666666666666668
w= 0.1
1.0 2.0 0.1 3.61
2.0 4.0 0.2 14.44
3.0 6.0 0.30000000000000004 32.49
MSE= 16.846666666666668
w= 0.2
1.0 2.0 0.2 3.24
2.0 4.0 0.4 12.96
3.0 6.0 0.6000000000000001 29.160000000000004
MSE= 15.120000000000003
w= 0.3
1.0 2.0 0.3 2.8899999999999997
2.0 4.0 0.6 11.559999999999999
3.0 6.0 0.8999999999999999 26.009999999999998
MSE= 13.486666666666665
w= 0.4
1.0 2.0 0.4 2.5600000000000005
2.0 4.0 0.8 10.240000000000002
3.0 6.0 1.2000000000000002 23.04
MSE= 11.946666666666667
w= 0.5
1.0 2.0 0.5 2.25
2.0 4.0 1.0 9.0
3.0 6.0 1.5 20.25
MSE= 10.5
w= 0.6
1.0 2.0 0.6 1.9599999999999997
2.0 4.0 1.2 7.839999999999999
3.0 6.0 1.7999999999999998 17.64
MSE= 9.146666666666667
w= 0.7
1.0 2.0 0.7 1.6900000000000002
2.0 4.0 1.4 6.760000000000001
3.0 6.0 2.0999999999999996 15.210000000000003
MSE= 7.886666666666668
w= 0.8
1.0 2.0 0.8 1.44
2.0 4.0 1.6 5.76
3.0 6.0 2.4000000000000004 12.959999999999997
MSE= 6.719999999999999
w= 0.9
1.0 2.0 0.9 1.2100000000000002
2.0 4.0 1.8 4.840000000000001
3.0 6.0 2.7 10.889999999999999
MSE= 5.646666666666666
w= 1.0
1.0 2.0 1.0 1.0
2.0 4.0 2.0 4.0
3.0 6.0 3.0 9.0
MSE= 4.666666666666667
w= 1.1
1.0 2.0 1.1 0.8099999999999998
2.0 4.0 2.2 3.2399999999999993
3.0 6.0 3.3000000000000003 7.289999999999998
MSE= 3.779999999999999
w= 1.2
1.0 2.0 1.2 0.6400000000000001
2.0 4.0 2.4 2.5600000000000005
3.0 6.0 3.5999999999999996 5.760000000000002
MSE= 2.9866666666666677
w= 1.3
1.0 2.0 1.3 0.48999999999999994
2.0 4.0 2.6 1.9599999999999997
3.0 6.0 3.9000000000000004 4.409999999999998
MSE= 2.2866666666666657
w= 1.4
1.0 2.0 1.4 0.3600000000000001
2.0 4.0 2.8 1.4400000000000004
3.0 6.0 4.199999999999999 3.2400000000000024
MSE= 1.6800000000000008
w= 1.5
1.0 2.0 1.5 0.25
2.0 4.0 3.0 1.0
3.0 6.0 4.5 2.25
MSE= 1.1666666666666667
w= 1.6
1.0 2.0 1.6 0.15999999999999992
2.0 4.0 3.2 0.6399999999999997
3.0 6.0 4.800000000000001 1.4399999999999984
MSE= 0.746666666666666
w= 1.7
1.0 2.0 1.7 0.09000000000000002
2.0 4.0 3.4 0.3600000000000001
3.0 6.0 5.1 0.8100000000000006
MSE= 0.4200000000000002
w= 1.8
1.0 2.0 1.8 0.03999999999999998
2.0 4.0 3.6 0.15999999999999992
3.0 6.0 5.4 0.3599999999999996
MSE= 0.1866666666666665
w= 1.9
1.0 2.0 1.9 0.010000000000000018
2.0 4.0 3.8 0.04000000000000007
3.0 6.0 5.699999999999999 0.09000000000000043
MSE= 0.046666666666666835
w= 2.0
1.0 2.0 2.0 0.0
2.0 4.0 4.0 0.0
3.0 6.0 6.0 0.0
MSE= 0.0
w= 2.1
1.0 2.0 2.1 0.010000000000000018
2.0 4.0 4.2 0.04000000000000007
3.0 6.0 6.300000000000001 0.09000000000000043
MSE= 0.046666666666666835
w= 2.2
1.0 2.0 2.2 0.04000000000000007
2.0 4.0 4.4 0.16000000000000028
3.0 6.0 6.6000000000000005 0.36000000000000065
MSE= 0.18666666666666698
w= 2.3
1.0 2.0 2.3 0.0899999999999999
2.0 4.0 4.6 0.3599999999999996
3.0 6.0 6.8999999999999995 0.809999999999999
MSE= 0.4199999999999995
w= 2.4
1.0 2.0 2.4 0.15999999999999992
2.0 4.0 4.8 0.6399999999999997
3.0 6.0 7.199999999999999 1.4399999999999984
MSE= 0.746666666666666
w= 2.5
1.0 2.0 2.5 0.25
2.0 4.0 5.0 1.0
3.0 6.0 7.5 2.25
MSE= 1.1666666666666667
w= 2.6
1.0 2.0 2.6 0.3600000000000001
2.0 4.0 5.2 1.4400000000000004
3.0 6.0 7.800000000000001 3.2400000000000024
MSE= 1.6800000000000008
w= 2.7
1.0 2.0 2.7 0.49000000000000027
2.0 4.0 5.4 1.960000000000001
3.0 6.0 8.100000000000001 4.410000000000006
MSE= 2.2866666666666693
w= 2.8
1.0 2.0 2.8 0.6399999999999997
2.0 4.0 5.6 2.5599999999999987
3.0 6.0 8.399999999999999 5.759999999999994
MSE= 2.986666666666664
w= 2.9
1.0 2.0 2.9 0.8099999999999998
2.0 4.0 5.8 3.2399999999999993
3.0 6.0 8.7 7.2899999999999965
MSE= 3.779999999999999
w= 3.0
1.0 2.0 3.0 1.0
2.0 4.0 6.0 4.0
3.0 6.0 9.0 9.0
MSE= 4.666666666666667
w= 3.1
1.0 2.0 3.1 1.2100000000000002
2.0 4.0 6.2 4.840000000000001
3.0 6.0 9.3 10.890000000000004
MSE= 5.646666666666668
w= 3.2
1.0 2.0 3.2 1.4400000000000004
2.0 4.0 6.4 5.760000000000002
3.0 6.0 9.600000000000001 12.96000000000001
MSE= 6.720000000000003
w= 3.3
1.0 2.0 3.3 1.6899999999999995
2.0 4.0 6.6 6.759999999999998
3.0 6.0 9.899999999999999 15.209999999999988
MSE= 7.886666666666662
w= 3.4
1.0 2.0 3.4 1.9599999999999997
2.0 4.0 6.8 7.839999999999999
3.0 6.0 10.2 17.639999999999993
MSE= 9.146666666666663
w= 3.5
1.0 2.0 3.5 2.25
2.0 4.0 7.0 9.0
3.0 6.0 10.5 20.25
MSE= 10.5
w= 3.6
1.0 2.0 3.6 2.5600000000000005
2.0 4.0 7.2 10.240000000000002
3.0 6.0 10.8 23.040000000000006
MSE= 11.94666666666667
w= 3.7
1.0 2.0 3.7 2.8900000000000006
2.0 4.0 7.4 11.560000000000002
3.0 6.0 11.100000000000001 26.010000000000016
MSE= 13.486666666666673
w= 3.8
1.0 2.0 3.8 3.2399999999999993
2.0 4.0 7.6 12.959999999999997
3.0 6.0 11.399999999999999 29.159999999999986
MSE= 15.119999999999996
w= 3.9
1.0 2.0 3.9 3.61
2.0 4.0 7.8 14.44
3.0 6.0 11.7 32.489999999999995
MSE= 16.846666666666664
w= 4.0
1.0 2.0 4.0 4.0
2.0 4.0 8.0 16.0
3.0 6.0 12.0 36.0
MSE= 18.666666666666668
1. 可以用visdom可视化工具 ⇒ 画图 |
---|
2. 训练过程中可能会出现断点,对模型进行实体化,即实时存盘 |
mport numpy as np
import matplotlib.pyplot as plt
# 定义数据集
x_data = [1.0, 2.0, 3.0]
y_data = [1.9, 3.9, 5.9]
# 生成矩阵坐标
W, B = np.arange(0.0, 4.1, 0.1).round(1), np.arange(-2.0, 2.1, 0.1).round(1)
w, b = np.meshgrid(W, B)
# 定义模型:y = x * w - b
def forward(x):
return x * w + b
# 损失函数
def loss(y_pre, y):
return (y_pre - y) ** 2
l_sum = 0 # 计算损失之和
for x_val, y_val in zip(x_data, y_data):
y_pre = forward(x_val)
loss_val = loss(y_pre, y_val)
l_sum += loss_val
mse = l_sum / len(x_data)
# 定义figure
fig = plt.figure(figsize=(10, 10), dpi=300)
# 画3D图
ax = plt.axes(projection='3d')
surf = ax.plot_surface(w, b, mse, rstride=1, cstride=1, cmap='rainbow')
# 设置z轴
ax.set_zlim(0, 40)
# 设置图标
ax.set_xlabel('w')
ax.set_ylabel('b')
ax.set_zlabel('Loss')
ax.text(0.2, 2, 43, 'Cost Value', color='black')
# 增加颜色条
fig.colorbar(surf, shrink=0.5, aspect=5)
fig.show()