PyTorch深度学习实践(一)线性模型Linear Model作业
前言
视频教程:B站 河北工业大学——刘二大人的【PyTorch深度学习实践】
网址:https://www.bilibili.com/video/BV1Y7411d7Ys
一、线性回归(只有权重w)
代码如下(示例):
import numpy as np
import matplotlib.pyplot as plt
# prepare the training dataset
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
# define the model
def forward(x):
return x * w
# contruct the loss function(MSE)
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) ** 2
# save the weights w
w_list = []
# save the cost values of each w
mse_list = []
# compute cost value at [0.0, 0.1, 0.2, ..., 4.0]
for w in np.arange(0.0, 4.0, 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('x_val:.{}\t y_val:.{}\t y_pred_val:.{}\t loss_val:.{}'.format(x_val, y_val, y_pred_val, loss_val))
print('MSE = ', l_sum / 3)
w_list.append(w)
mse_list.append(l_sum / 3)
# draw the graph
plt.plot(w_list, mse_list)
plt.xlabel('w')
plt.ylabel('loss')
plt.show()
结果展示(仅展示部分结果):
w= 0.0
x_val:.1.0 y_val:.2.0 y_pred_val:.0.0 loss_val:.4.0
x_val:.2.0 y_val:.4.0 y_pred_val:.0.0 loss_val:.16.0
x_val:.3.0 y_val:.6.0 y_pred_val:.0.0 loss_val:.36.0
MSE = 18.666666666666668
w= 0.1
x_val:.1.0 y_val:.2.0 y_pred_val:.0.1 loss_val:.3.61
x_val:.2.0 y_val:.4.0 y_pred_val:.0.2 loss_val:.14.44
x_val:.3.0 y_val:.6.0 y_pred_val:.0.30000000000000004 loss_val:.32.49
MSE = 16.846666666666668
w= 0.2
x_val:.1.0 y_val:.2.0 y_pred_val:.0.2 loss_val:.3.24
x_val:.2.0 y_val:.4.0 y_pred_val:.0.4 loss_val:.12.96
x_val:.3.0 y_val:.6.0 y_pred_val:.0.6000000000000001 loss_val:.29.160000000000004
MSE = 15.120000000000003
w= 0.30000000000000004
x_val:.1.0 y_val:.2.0 y_pred_val:.0.30000000000000004 loss_val:.2.8899999999999997
x_val:.2.0 y_val:.4.0 y_pred_val:.0.6000000000000001 loss_val:.11.559999999999999
x_val:.3.0 y_val:.6.0 y_pred_val:.0.9000000000000001 loss_val:.26.009999999999998
MSE = 13.486666666666665
…
w= 1.9000000000000001
x_val:.1.0 y_val:.2.0 y_pred_val:.1.9000000000000001 loss_val:.0.009999999999999974
x_val:.2.0 y_val:.4.0 y_pred_val:.3.8000000000000003 loss_val:.0.0399999999999999
x_val:.3.0 y_val:.6.0 y_pred_val:.5.7 loss_val:.0.0899999999999999
MSE = 0.046666666666666586
w= 2.0
x_val:.1.0 y_val:.2.0 y_pred_val:.2.0 loss_val:.0.0
x_val:.2.0 y_val:.4.0 y_pred_val:.4.0 loss_val:.0.0
x_val:.3.0 y_val:.6.0 y_pred_val:.6.0 loss_val:.0.0
MSE = 0.0
w= 2.1
x_val:.1.0 y_val:.2.0 y_pred_val:.2.1 loss_val:.0.010000000000000018
x_val:.2.0 y_val:.4.0 y_pred_val:.4.2 loss_val:.0.04000000000000007
x_val:.3.0 y_val:.6.0 y_pred_val:.6.300000000000001 loss_val:.0.09000000000000043
MSE = 0.046666666666666835
…
w= 3.8000000000000003
x_val:.1.0 y_val:.2.0 y_pred_val:.3.8000000000000003 loss_val:.3.240000000000001
x_val:.2.0 y_val:.4.0 y_pred_val:.7.6000000000000005 loss_val:.12.960000000000004
x_val:.3.0 y_val:.6.0 y_pred_val:.11.4 loss_val:.29.160000000000004
MSE = 15.120000000000005
w= 3.9000000000000004
x_val:.1.0 y_val:.2.0 y_pred_val:.3.9000000000000004 loss_val:.3.610000000000001
x_val:.2.0 y_val:.4.0 y_pred_val:.7.800000000000001 loss_val:.14.440000000000005
x_val:.3.0 y_val:.6.0 y_pred_val:.11.700000000000001 loss_val:.32.49000000000001
MSE = 16.84666666666667
损失权重图:
二、线性回归(带偏置b)
代码如下(示例):
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# prepare the training dataset
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
# define the Linear model
def forward(x):
return w * x + b
# define the loss function
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) ** 2
mse_list = []
W = np.arange(0.0, 4.0, 0.1)
B = np.arange(-2.0, 2.0, 0.1)
[w, b] = np.meshgrid(W, B)
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)
print('x_val:.{}\t y_val:.{}\t y_pred_val:.{}\t loss_val:.{}'.format(x_val, y_val, y_pred_val, loss_val))
l_sum += loss_val
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(w, b, l_sum / 3)
ax.set_zlabel('Loss')
ax.set_ylabel('b')
ax.set_xlabel('w')
plt.show()