PyTorch深度学习实践第二集 线性模型 y=wx+b

plt 三维图绘制:Python绘制三维图详解_北山啦的博客-CSDN博客

import numpy as np
import matplotlib.pyplot as plt

x_data=[1.0,2.0,3.0]
y_data=[3.0,5.0,7.0]

def forward(x,w,b):
    y_p=w*x+b
    return y_p

def loss(x,y,w,b):
    y_p=forward(x,w,b)
    loss_=(y-y_p)*(y-y_p)
    return loss_

w_list=np.arange(0.0,4.1,0.1)
b_list=np.arange(-2.0,2.0,0.1)
mse_list=[]

for w in w_list:
    for b in b_list:
        l_sum = 0
        for x,y in zip(x_data,y_data):
            loss_=loss(x,y,w,b)
            l_sum+=loss_
        mse_list.append(l_sum/len(x_data))

w_list, b_list=np.meshgrid(w_list, b_list)
mse_list=np.array(mse_list).reshape(len(w_list),-1)


fig = plt.figure() #创建图片
sub = fig.add_subplot(111,projection='3d')# 添加子图,
surf = sub.plot_surface(w_list,b_list,mse_list,cmap=plt.cm.brg) #绘制曲面,并设置颜色cmap
cb = fig.colorbar(surf,shrink=0.8,aspect=15) #设置颜色棒

sub.set_xlabel(r"$x$")
sub.set_ylabel(r"$y$")
sub.set_zlabel(r"$z$")
plt.show()
#

你可能感兴趣的:(Pytorch,深度学习,pytorch,人工智能)