Python深度學(xué)習(xí)pyTorch權(quán)重衰減與L2范數(shù)正則化解析

下面進(jìn)行一個(gè)高維線性實(shí)驗(yàn)
假設(shè)我們的真實(shí)方程是:

假設(shè)feature數(shù)200,訓(xùn)練樣本和測(cè)試樣本各20個(gè)
模擬數(shù)據(jù)集
num_train,num_test = 10,10 num_features = 200 true_w = torch.ones((num_features,1),dtype=torch.float32) * 0.01 true_b = torch.tensor(0.5) samples = torch.normal(0,1,(num_train+num_test,num_features)) noise = torch.normal(0,0.01,(num_train+num_test,1)) labels = samples.matmul(true_w) + true_b + noise train_samples, train_labels= samples[:num_train],labels[:num_train] test_samples, test_labels = samples[num_train:],labels[num_train:]
定義帶正則項(xiàng)的loss function
def loss_function(predict,label,w,lambd):
loss = (predict - label) ** 2
loss = loss.mean() + lambd * (w**2).mean()
return loss
畫圖的方法
def semilogy(x_val,y_val,x_label,y_label,x2_val,y2_val,legend):
plt.figure(figsize=(3,3))
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.semilogy(x_val,y_val)
if x2_val and y2_val:
plt.semilogy(x2_val,y2_val)
plt.legend(legend)
plt.show()
擬合和畫圖
def fit_and_plot(train_samples,train_labels,test_samples,test_labels,num_epoch,lambd):
w = torch.normal(0,1,(train_samples.shape[-1],1),requires_grad=True)
b = torch.tensor(0.,requires_grad=True)
optimizer = torch.optim.Adam([w,b],lr=0.05)
train_loss = []
test_loss = []
for epoch in range(num_epoch):
predict = train_samples.matmul(w) + b
epoch_train_loss = loss_function(predict,train_labels,w,lambd)
optimizer.zero_grad()
epoch_train_loss.backward()
optimizer.step()
test_predict = test_sapmles.matmul(w) + b
epoch_test_loss = loss_function(test_predict,test_labels,w,lambd)
train_loss.append(epoch_train_loss.item())
test_loss.append(epoch_test_loss.item())
semilogy(range(1,num_epoch+1),train_loss,'epoch','loss',range(1,num_epoch+1),test_loss,['train','test'])

可以發(fā)現(xiàn)加了正則項(xiàng)的模型,在測(cè)試集上的loss確實(shí)下降了
以上就是Python深度學(xué)習(xí)pyTorch權(quán)重衰減與L2范數(shù)正則化解析的詳細(xì)內(nèi)容,更多關(guān)于Python pyTorch權(quán)重與L2范數(shù)正則化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python的Django框架中消息通知的計(jì)數(shù)器實(shí)現(xiàn)教程
通知的計(jì)數(shù)器非常有用,新通知時(shí)+1和讀過(guò)通知后的-1是最基本的功能,這里我們就來(lái)看一下Python的Django框架中消息通知的計(jì)數(shù)器實(shí)現(xiàn)教程2016-06-06
Python使用selenium + headless chrome獲取網(wǎng)頁(yè)內(nèi)容的方法示例
這篇文章主要介紹了Python使用selenium + headless chrome獲取網(wǎng)頁(yè)內(nèi)容的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python實(shí)現(xiàn)上課點(diǎn)名器系統(tǒng)
今天給大家分享一個(gè)讀者粉絲投稿的,關(guān)于上課點(diǎn)名的實(shí)戰(zhàn)案例,對(duì)Python上課點(diǎn)名器實(shí)現(xiàn)過(guò)程感興趣的朋友,一起來(lái)看看是如何實(shí)現(xiàn)的吧2021-10-10
Python利用pdfplumber實(shí)現(xiàn)讀取PDF寫入Excel
pdfplumber專注PDF內(nèi)容提取,例如文本(位置、字體及顏色等)和形狀(矩形、直線、曲線),還有解析表格的功能。本文主要為大家介紹如何利用pdfplumber實(shí)現(xiàn)讀取PDF寫入Excel,需要的可以參考一下2022-06-06
python3 實(shí)現(xiàn)函數(shù)寫文件路徑的正確方法
今天小編就為大家分享一篇python3 實(shí)現(xiàn)函數(shù)寫文件路徑的正確方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Django實(shí)現(xiàn)登錄隨機(jī)驗(yàn)證碼的示例代碼
登錄驗(yàn)證碼是每個(gè)網(wǎng)站登錄時(shí)的基本標(biāo)配,這篇文章主要介紹了Django實(shí)現(xiàn)登錄隨機(jī)驗(yàn)證碼的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06

