pytorch實踐線性模型3d詳解
y = wx +b
通過meshgrid 得到兩個二維矩陣
關(guān)鍵理解:
plot_surface需要的xyz是二維np數(shù)組
這里提前準備meshgrid來生產(chǎn)x和y需要的參數(shù)
下圖的W和I即plot_surface需要xy

Z即我們需要的權(quán)重損失
計算方式要和W,I. I的每行中內(nèi)容是一樣的就是y=wx+b的b是一樣的
fig = plt.figure()
ax = fig.add_axes(Axes3D(fig))
ax.plot_surface(W, I, Z=MSE_data)總的實驗代碼
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
class LinearModel:
@staticmethod
def forward(w, x):
return w * x
@staticmethod
def forward_with_intercept(w, x, b):
return w * x + b
@staticmethod
def get_loss(w, x, y_origin, exp=2, b=None):
if b:
y = LinearModel.forward_with_intercept(w, x, b)
else:
y = LinearModel.forward(w, x)
return pow(y_origin - y, exp)
def test_2d():
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
weight_data = []
MSE_data = []
# 設(shè)定實驗的權(quán)重范圍
for w in np.arange(0.0, 4.1, 0.1):
weight_data.append(w)
loss_total = 0
# 計算每個權(quán)重在數(shù)據(jù)集上的MSE平均平方方差
for x_val, y_val in zip(x_data, y_data):
loss_total += LinearModel.get_loss(w, x_val, y_val)
MSE_data.append(loss_total / len(x_data))
# 繪圖
plt.xlabel("weight")
plt.ylabel("MSE")
plt.plot(weight_data, MSE_data)
plt.show()
def test_3d():
x_data = [1.0, 2.0, 3.0]
y_data = [5.0, 8.0, 11.0]
weight_data = np.arange(0.0, 4.1, 0.1)
intercept_data = np.arange(0.0, 4.1, 0.1)
W, I = np.meshgrid(weight_data, intercept_data)
MSE_data = []
# 設(shè)定實驗的權(quán)重范圍 循環(huán)要先寫截距的 meshgrid 的返回第二個是相當于41*41 同一行值相同 ,要在第二層循環(huán)去遍歷權(quán)重
for intercept in intercept_data:
MSE_data_tmp = []
for w in weight_data:
loss_total = 0
# 計算每個權(quán)重在數(shù)據(jù)集上的MSE平均平方方差
for x_val, y_val in zip(x_data, y_data):
loss_total += LinearModel.get_loss(w, x_val, y_val, b=intercept)
MSE_data_tmp.append(loss_total / len(x_data))
MSE_data.append(MSE_data_tmp)
MSE_data = np.array(MSE_data)
fig = plt.figure()
ax = fig.add_axes(Axes3D(fig))
ax.plot_surface(W, I, Z=MSE_data)
plt.xlabel("weight")
plt.ylabel("intercept")
plt.show()
if __name__ == '__main__':
test_2d()
test_3d()
到此這篇關(guān)于pytorch實踐線性模型3d的文章就介紹到這了,更多相關(guān)pytorch線性模型內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 獲取一個值在某個區(qū)間的指定倍數(shù)的值方法
今天小編就為大家分享一篇python 獲取一個值在某個區(qū)間的指定倍數(shù)的值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
如何將python的數(shù)據(jù)存儲到mysql數(shù)據(jù)庫中
在很多數(shù)據(jù)處理項目中,將數(shù)據(jù)存儲到數(shù)據(jù)庫中是非常常見的操作,下面這篇文章主要給大家介紹了關(guān)于如何將python的數(shù)據(jù)存儲到mysql數(shù)據(jù)庫中的相關(guān)資料,需要的朋友可以參考下2023-12-12
Python?Asyncio中Coroutines,Tasks,Future可等待對象的關(guān)系及作用
這篇文章主要介紹了Python?Asyncio中Coroutines,Tasks,Future可等待對象的關(guān)系及作用,文章圍繞主題展開詳細的內(nèi)容介紹,需要的小伙伴可以參考一下2022-06-06
Python遞歸函數(shù) 二分查找算法實現(xiàn)解析
這篇文章主要介紹了Python遞歸函數(shù) 二分查找算法實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08
Python如何基于rsa模塊實現(xiàn)非對稱加密與解密
這篇文章主要介紹了Python如何基于rsa模塊實現(xiàn)非對稱加密與解密,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01
Django與圖表的數(shù)據(jù)交互的實現(xiàn)
本文主要介紹了Django與圖表的數(shù)據(jù)交互的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
如何解決Python:報錯[Errno 2]No such file or&nb
介紹了Python文件讀取操作時常見的錯誤原因及解決方法,主要錯誤原因包括路徑拼寫錯誤、工作目錄與相對路徑不匹配以及文件不存在,解決方法有使用絕對路徑和動態(tài)獲取腳本路徑,其他注意事項包括驗證文件路徑與名稱、理解工作目錄與相對路徑2025-02-02

