《PyTorch深度学习实践》完结合集_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili
原视频下方评论置顶有课件
对于以上一组数据x,y,为了预测接下来的x对应的y,我们选择一个模型。这里选择线性(Linear Model)模型
为了简化,下面的例子去掉了b.
为了评价模型,引入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] def forward(x): return x * w def loss(x,y): y_pred = forward(x) return (y_pred - y) * (y_pred - y) w_list = [] mse_list = [] for w in np.arange(0.0,4.1,0.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()