Pytorch深度学习实践-刘二大人-线性模型作业

import numpy as np
import matplotlib.pyplot as plt

x_data = [1, 2, 3, 4]
y_data = [2, 4, 6, 8]

w_list = np.arange(0.0, 4.1, 0.1)
b_list = np.arange(-2.0, 2.1, 0.1)
w, b = np.meshgrid(w_list, b_list)


def forward(x):
    return x * w + b


def loss(x, y):
    y_pre = forward(x)
    return (y - y_pre) * (y - y_pre)


mse_list = []
for x_val, y_val in zip(x_data, y_data):
    l_sum = 0
    y_pre_val = forward(x_val)
    loss_val = loss(x_val, y_val)
    l_sum += loss_val
    print('\t', x_val, y_val, y_pre_val, loss_val)
print('MSE',  l_sum/4)
mse_list.append(l_sum / 4)


fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.plot_surface(w, b, mse_list[0],  ##x,y,z二维矩阵(坐标矩阵xv,yv,zv)
                rstride=1,  ##retride(row)指定行的跨度
                cstride=1,  ##retride(column)指定列的跨度
                cmap='rainbow')  ##设置颜色映射
plt.show()

实验结果为:

Pytorch深度学习实践-刘二大人-线性模型作业_第1张图片

 

你可能感兴趣的:(pytorch,python,机器学习)