2

下面是代碼Logistic迴歸梯度下降在Matlab

function [theta] = LR(D) 
% D is the data having feature variables and class labels 

% Now decompose D into X and C 
%Note that dimensions of X = , C = 

C = D(:,1); 
C = C'; 
size(C) 
X = D(:,2:size(D,2)); 
size(X) 
alpha = .00001; 

theta_old = zeros(1,34); 
theta_new = .001.*ones(1,34); 
count = 1; 
for count = 1:100000 
    theta_old = theta_new; 
    theta_new = theta_new + alpha*(C-sigmoid(X*theta_new')')*X; 
    llr = sum(LLR((X*theta_new').*(C'))) 
end 
thetaopt = theta_new 


end 


function a = LLR(z) 
a= 1.*log(1.0 + exp(-z)); 
end 

function a = sigmoid(z) 
a = 1.0 ./ (1.0 + exp(-z)); 
end 

我的問題是,對數似然比先減小,然後開始上升。這是漸變下降算法還是代碼問題?

+0

是標籤是D(:,1)0/1或-1/1。可以向我們顯示重量(theta_new)梯度llr,在每10個iter中,似乎該模型過度擬合,因爲您需要100000次,這是很多的。您應該在漸變達到某個值時退出(例如:1e-4)。 – michaeltang

+0

你可以嘗試正則化,在l2正則化中,grad =(C-sigmoid(X * theta_new')')* X + thread_new – michaeltang

回答

1

看起來你的目標函數可能有問題。

如果標籤(C)是{0,1},那麼你就應該使用損失C.*LLR(X*theta')+(1-C).*(LLR(X*theta')+X*theta')

如果您的標籤都是在{-1,1},那麼損失應爲LLR(C.*X*theta')

您似乎只使用第一種損失函數的第一部分。