跟着b站学习之 刘二大人up的
《PyTorch深度学习实践》完结合集
模型1:y_hat = wx
实现 模型2:y_hat = wx + b
实现难点1:画3D图步骤难点2:画3D图前,数据预处理补充 模型1:y_hat = wx 实现
# -*- coding: UTF-8 -*-# Abstract : 模型:y^ = wximport numpy as npimport matplotlib.pyplot as pltx_data = [1.0, 2.0, 3.0]y_data = [2.0, 4.0, 6.0]def forward(x): # 模型 return x * wdef loss(x,y): # 损失函数 y_pred = forward(x) return (y_pred - y) * (y_pred - y)def draw(): plt.plot(w_list,mse_list) plt.xlabel('w') plt.ylabel('loss') plt.show()w_list = []mse_list = []for w in np.arange(0.0, 4.1, 0.1): # 注意!!!!是np.arange() print('w=', w) l_sum = 0 # 一条条的loss累加 for x_val, y_val in zip(x_data, y_data): y_pred_val = forward(x_val) loss_val = loss(x_val, y_val) # 每一条的loss l_sum += loss_val print('t',x_val, y_val, "%.1f" % y_pred_val, "%.1f" % loss_val) MSE = l_sum / 3 print('MSE=',MSE) w_list.append(w) mse_list.append(MSE)draw()
模型2:y_hat = wx + b 实现# -*- coding: UTF-8 -*-# Abstract : 模型:y^ = wx + bimport numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dx_data = [0.0, 1.0, 2.0]y_data = [1.0, 3.0, 5.0]def forward(x): return w * x + bdef loss(x,y): y_pred = forward(x) return (y_pred - y) * (y_pred - y)'''有两种方式绘图,绘图效果相同,唯一区别在第二行:1、ax = Axes3D(fig)2、sub = fig.add_subplot(111, projection='3d')其中,1操作简单,但没办法标注zlable、zlim等关于z轴的信息画3D图步骤:1、创建绘图对象2、创建3D对象3、创建曲面对象4、添加各种图例5、绘图'''def draw1(): fig = plt.figure() ax = Axes3D(fig) surf = ax.plot_surface(w, b, z, cmap='rainbow') # cmap是颜色 cb = fig.colorbar(surf, shrink=0.8, aspect=15, label=r'$mse(w,b)$') # 添加颜色棒图例 # shrink 颜色棒缩放,aspect 颜色棒长宽比 ax.set_xlabel(r'$w$') # == plt.xlabel(r'$w$') ax.set_ylabel(r'$b$') # == plt.ylabel(r'$b$') ax.set_zlabel(r'$mse$') # 不存在 plt.zlabel(r'$mse$') plt.show()def draw2(): fig = plt.figure() sub = fig.add_subplot(111, projection='3d') # 创建3D子图对象 surf = sub.plot_surface(w, b, z, cmap='rainbow') cb = fig.colorbar(surf, label=r'$mse(w,b)$', shrink=0.8, aspect=15) sub.set_xlabel(r'$w$') sub.set_ylabel(r'$b$') sub.set_zlabel(r'$mse$') plt.show()w_list = []b_list = []mse_list = []for w in np.arange(1.0,3.0,0.2): for b in np.arange(0.0,2.0,0.2): print('w =', w, 'b=', 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 print(x_val, y_val, "%.1f" % y_pred_val, "%.1f" % loss_val) MSE = l_sum / 3 print('MSE=', MSE) w_list.append(w) b_list.append(b) mse_list.append(MSE)# 画3D图前,数据预处理w = np.array(w_list) # list -> array(一维数组)w.resize((10, 10)) # 一维数组 -> 二维数组b = np.array(b_list)b.resize((10, 10))z = np.array(mse_list)z.resize((10, 10))draw1()#draw2()
难点1:画3D图步骤1、创建绘图对象:
fig = plt.figure()
2、创建3D对象:
ax = Axes3D(fig)
3、创建曲面对象:
surf = ax.plot_surface(x, y, z)
4、添加各种图例
cb = fig.colorbar(surf) ax.set_xlabel(r'$x$') ax.set_ylabel(r'$y$') ax.set_zlabel(r'$z$')
5、绘图
plt.show()
难点2:画3D图前,数据预处理创建曲面对象时,传入的 x,y,z 均为二维数组。介绍几个函数:
list -> array:
np.array()
li = [1,2,3,4,5,6]arr = np.array(li)
一维数组 -> 二维数组:
reshape((row,column))、resize((row,column))
d = np.linspace(-1,1,10)D = d.reshape((2,5)) # 将一维数组变成2行5列,原数组不会被覆盖d.resize((2,5)) # 新的结果覆盖原来的数组
二维数组 -> 一维数组:
flatten()
arr = np.array([[1,2,3], [4,5,6]])arr.flatten()
补充 关于3D图的更多细节,参考:
索哥Python科学绘图教程15