簡體   English   中英

Matlab中的Logistic回歸梯度下降

[英]Logistic Regression Gradient Descent in 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

我的問題是對數似然比先降低,然后開始增加。 這是梯度下降算法或代碼的問題嗎?

您的目標函數似乎有問題。

如果標簽( C )在{0,1} ,則應該使用損耗C.*LLR(X*theta')+(1-C).*(LLR(X*theta')+X*theta')

如果您的標簽位於{-1,1} ,則損失應為LLR(C.*X*theta')

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM