เหตุใดการทำให้เป็นมาตรฐานใน pytorch และ scratch code ไม่ตรงกันและสูตรที่ใช้สำหรับการทำให้เป็นมาตรฐานใน pytorch คืออะไร?
ฉันพยายามทำ L2 normalization ในรูปแบบการจำแนกไบนารีใน PyTorch แต่เมื่อฉันจับคู่ผลลัพธ์ของ PyTorch และ scratch code ไม่ตรงกันรหัส Pytorch:
class LogisticRegression(nn.Module):
def __init__(self,n_input_features):
super(LogisticRegression,self).__init__()
self.linear=nn.Linear(4,1)
self.linear.weight.data.fill_(0.0)
self.linear.bias.data.fill_(0.0)
def forward(self,x):
y_predicted=torch.sigmoid(self.linear(x))
return y_predicted
model=LogisticRegression(4)
criterion=nn.BCELoss()
optimizer=torch.optim.SGD(model.parameters(),lr=0.05,weight_decay=0.1)
dataset=Data()
train_data=DataLoader(dataset=dataset,batch_size=1096,shuffle=False)
num_epochs=1000
for epoch in range(num_epochs):
for x,y in train_data:
y_pred=model(x)
loss=criterion(y_pred,y)
loss.backward()
optimizer.step()
optimizer.zero_grad()
รหัสขูด:
def sigmoid(z):
s = 1/(1+ np.exp(-z))
return s
def yinfer(X, beta):
return sigmoid(beta[0] + np.dot(X,beta[1:]))
def cost(X, Y, beta, lam):
sum = 0
sum1 = 0
n = len(beta)
m = len(Y)
for i in range(m):
sum = sum + Y[i]*(np.log( yinfer(X[i],beta)))+ (1 -Y[i])*np.log(1-yinfer(X[i],beta))
for i in range(0, n):
sum1 = sum1 + beta[i]**2
return (-sum + (lam/2) * sum1)/(1.0*m)
def pred(X,beta):
if ( yinfer(X, beta) > 0.5):
ypred = 1
else :
ypred = 0
return ypred
beta = np.zeros(5)
iterations = 1000
arr_cost = np.zeros((iterations,4))
print(beta)
n = len(Y_train)
for i in range(iterations):
Y_prediction_train=np.zeros(len(Y_train))
Y_prediction_test=np.zeros(len(Y_test))
for l in range(len(Y_train)):
Y_prediction_train[l]=pred(X[l,:],beta)
for l in range(len(Y_test)):
Y_prediction_test[l]=pred(X_test[l,:],beta)
train_acc = format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100)
test_acc = 100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100
arr_cost[i,:] = [i,cost(X,Y_train,beta,lam),train_acc,test_acc]
temp_beta = np.zeros(len(beta))
''' main code from below '''
for j in range(n):
temp_beta[0] = temp_beta[0] + yinfer(X[j,:], beta) - Y_train[j]
temp_beta[1:] = temp_beta[1:] + (yinfer(X[j,:], beta) - Y_train[j])*X[j,:]
for k in range(0, len(beta)):
temp_beta[k] = temp_beta[k] + lam * beta[k] #regularization here
temp_beta= temp_beta / (1.0*n)
beta = beta - alpha*temp_beta
กราฟของการสูญเสีย
กราฟความแม่นยำในการฝึก
กราฟความแม่นยำในการทดสอบ
ใครช่วยบอกหน่อยได้ไหมว่าทำไมถึงเกิดเหตุการณ์นี้ขึ้น ค่า L2 = 0.1
คำตอบ
คำถามที่ดี ฉันขุดเอกสารมากมายในPyTorchและพบคำตอบ คำตอบคือมากหากิน โดยทั่วไปมีสองวิธีในการคำนวณregulalarization (สำหรับฤดูร้อนให้ข้ามไปที่ส่วนสุดท้าย)

PyTorchใช้ประเภทแรก (ซึ่งปัจจัยกูไม่ได้แบ่งตามขนาด batch)
นี่คือตัวอย่างโค้ดที่แสดงให้เห็นว่า:
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import torch.optim as optim
class model(nn.Module):
def __init__(self):
super().__init__()
self.linear = nn.Linear(1, 1)
self.linear.weight.data.fill_(1.0)
self.linear.bias.data.fill_(1.0)
def forward(self, x):
return self.linear(x)
model = model()
optimizer = optim.SGD(model.parameters(), lr=0.1, weight_decay=1.0)
input = torch.tensor([[2], [4]], dtype=torch.float32)
target = torch.tensor([[7], [11]], dtype=torch.float32)
optimizer.zero_grad()
pred = model(input)
loss = F.mse_loss(pred, target)
print(f'input: {input[0].data, input[1].data}')
print(f'prediction: {pred[0].data, pred[1].data}')
print(f'target: {target[0].data, target[1].data}')
print(f'\nMSEloss: {loss.item()}\n')
loss.backward()
print('Before updation:')
print('--------------------------------------------------------------------------')
print(f'weight [data, gradient]: {model.linear.weight.data, model.linear.weight.grad}')
print(f'bias [data, gradient]: {model.linear.bias.data, model.linear.bias.grad}')
print('--------------------------------------------------------------------------')
optimizer.step()
print('After updation:')
print('--------------------------------------------------------------------------')
print(f'weight [data]: {model.linear.weight.data}')
print(f'bias [data]: {model.linear.bias.data}')
print('--------------------------------------------------------------------------')
ซึ่งผลลัพธ์ :
input: (tensor([2.]), tensor([4.]))
prediction: (tensor([3.]), tensor([5.]))
target: (tensor([7.]), tensor([11.]))
MSEloss: 26.0
Before updation:
--------------------------------------------------------------------------
weight [data, gradient]: (tensor([[1.]]), tensor([[-32.]]))
bias [data, gradient]: (tensor([1.]), tensor([-10.]))
--------------------------------------------------------------------------
After updation:
--------------------------------------------------------------------------
weight [data]: tensor([[4.1000]])
bias [data]: tensor([1.9000])
--------------------------------------------------------------------------
นี่m = ชุดขนาด = 2, LR = อัลฟา = 0.1, แลมบ์ดา = weight_decay = 1
ตอนนี้ให้พิจารณาน้ำหนักเทนเซอร์ซึ่งมีค่า = 1 และ grad = -32
case1 (การทำให้เป็นมาตรฐาน type1):
weight = weight - lr(grad + weight_decay.weight)
weight = 1 - 0.1(-32 + 1(1))
weight = 4.1
case2 (การทำให้เป็นมาตรฐาน type2):
weight = weight - lr(grad + (weight_decay/batch size).weight)
weight = 1 - 0.1(-32 + (1/2)(1))
weight = 4.15
จากการส่งออกเราจะเห็นว่าการปรับปรุงน้ำหนัก = 4.1000 สรุปได้ว่าPyTorchใช้การทำให้เป็นมาตรฐานtype1
ดังนั้นในที่สุดในรหัสของคุณคุณกำลังติดตามtype2กู ดังนั้นเพียงแค่เปลี่ยนบรรทัดสุดท้ายเป็นสิ่งนี้:
# for k in range(0, len(beta)):
# temp_beta[k] = temp_beta[k] + lam * beta[k] #regularization here
temp_beta= temp_beta / (1.0*n)
beta = beta - alpha*(temp_beta + lam * beta)
และฟังก์ชันการสูญเสีย PyTorch ยังไม่รวมเงื่อนไขการทำให้เป็นมาตรฐาน (ใช้งานในเครื่องมือเพิ่มประสิทธิภาพ ) ดังนั้นให้ลบเงื่อนไขการทำให้เป็นมาตรฐานในฟังก์ชันต้นทุนที่กำหนดเอง
สรุป:
Pytorchใช้ฟังก์ชันRegularizationนี้:
การทำให้เป็นมาตรฐานถูกนำไปใช้ภายในเครื่องมือเพิ่มประสิทธิภาพ (พารามิเตอร์ weight_decay)
ฟังก์ชันPyTorch Loss ไม่รวมเงื่อนไขการทำให้เป็นระเบียบ
อคติยังถูกทำให้เป็นมาตรฐานหากใช้ Regularization
ในการใช้การทำให้เป็นมาตรฐานลอง:
torch.nn.optim.optimiser_name (model.parameters (), LR, weight_decay = แลมบ์ดา)