Pytorch深度学习实战(二之课后作业)

Pytorch深度学习实战(二之课后作业)

刘二大人:视频连接

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

# 假设函数为 y = 3x + 2

# 训练集
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) ** 2

# 均方误差
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):
    loss_val = loss(x_val, y_val)
    l_sum += loss_val
    
    # 预测值
    y_pred_val = forward(x_val)
    print('预测值:', y_pred_val)


# 绘图
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(w, b, l_sum / len(x_data))
plt.show()

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