线性模型
传送门 Pytorch深度学习教程 本文借鉴CSDN专栏 PyTorch 深度学习实践
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| 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)**2
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()
|
代码说明
- 函数forward中的w在后续的for循环中传入
- np.arange numpy.arange([start, ]stop, [step, ]dtype=None)
- start(可选):序列的起始值,默认为
0
。
- stop :序列的结束值( 不包含该值本身 )。
- step(可选):步长(间隔),默认为
1
。
- dtype(可选):指定输出数组的数据类型(如
int
, float
等)。
- 不包括结束值
- zip函数在这里的作用是配对基本元素