《PyTorch深度学习实践》第二节
作者:互联网
B站up主 刘二大人 《PyTorch深度学习实践》完结合集
P2线性模型–作业
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#设函数为y=2x+1
x_data = [1.0,2.0,3.0]
y_data = [3.0,5.0,7.0]
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) #生成坐标矩阵
def forward(x):
return x * w + b
def loss(x,y):
y_pred = forward(x)
return (y_pred-y)*(y_pred-y)
l_sum = 0
for x_val, y_val in zip(x_data, y_data):
y_pred_val = forward(x_val)
print(y_pred_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) #曲面画图
ax.set_xlabel("w")
ax.set_ylabel("b")
ax.set_zlabel("mse")
plt.show()
运行结果:
标签:loss,val,pred,深度,PyTorch,第二节,np,ax,data 来源: https://blog.csdn.net/weixin_44894550/article/details/120811815