pytorch深度学习:线性模型y=w*x+b

数据 x_data = [1.0, 2.0, 3.0],y_data = [5.0, 8.0, 11.0]
模型选择:y = x * w + b
代码如下:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x_data = [1.0, 2.0, 3.0]
y_data = [5.0, 8.0, 11.0]

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

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

mse_list = []
W = np.arange(0.0, 4.1, 0.1)
B = np.arange(0.0, 4.1, 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)
    l_sum += loss_val

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(w, b, l_sum/3)
plt.show()

运用Axes3D显示的w, b和损失的关系的关系图如下:
pytorch深度学习:线性模型y=w*x+b

上一篇:人体关键点检测(Keypoints Detection)


下一篇:[BJDCTF2020]The mystery of ip