步骤:数据集-模型选择-训练-应用
Maching Learning:Input-DataSet ( training、testing)-prediction
有监督学习(Supervised Learning):过拟合、泛化能力(训练集=训练+验证)
Linear Model:
用visdom可视化
课堂代码
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]
def forward(x): # 前馈
return x * w
def loss(x, y): # 损失函数
y_pred = forward(x) # y_hat
return (y_pred - y) * (y_pred - y)
w_list = []
mse_list = []
for w in np.arange(0.0, 4.1, 0.1): # 间隔为0.1,0.0~4.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)
plt.plot(w_list, mse_list)
plt.ylabel('Loss') # 纵坐标
plt.xlabel('W') # 横坐标
plt.show()
作业代码
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 设y=2x+1
x_data = [1.0, 2.0, 3.0]
y_data = [3.0, 5.0, 7.0]
def forward(x): # 前馈
return x * w + b
def loss(x, y): # 损失函数
y_pred = forward(x) # y_hat
return (y_pred - y) * (y_pred - y)
w_list = []
b_list = []
mse_list = []
for w in np.arange(0.0, 4.0, 0.1): # 间隔为0.1,0.0~4.0
for b in np.arange(0.0, 4.0, 0.1):
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 # 求和
b_list.append(b)
mse_list.append(l_sum / 3)
w_list.append(w)
mse_r = np.array(mse_list)
b_r = np.array(b_list)
mse_j = np.transpose(mse_r.reshape(40, 40))
b_j = b_r.reshape(40, 40)
fig = plt.figure()
ax = fig.add_axes(Axes3D(fig, auto_add_to_figure=False))
[w, b] = np.meshgrid(w_list, b_j[1])
ax.plot_surface(w, b, mse_j, cmap='rainbow')
ax.set_xlabel('W', color='b')
ax.set_ylabel('B', color='g')
ax.set_zlabel('MSE', color='r')
plt.draw()
plt.show()