简体   繁体   English

Matlab中的Logistic回归梯度下降

[英]Logistic Regression Gradient Descent in Matlab

Here is the code 这是代码

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

The problem I have is that the log likelihood ratio first decreases, and then starts increasing. 我的问题是对数似然比先降低,然后开始增加。 Is this a problem with the Gradient Descent algorithm or with the code. 这是梯度下降算法或代码的问题吗?

It looks like there could be a problem with your objective function. 您的目标函数似乎有问题。

If the labels ( C ) are in {0,1} , then you should be using the loss C.*LLR(X*theta')+(1-C).*(LLR(X*theta')+X*theta') 如果标签( C )在{0,1} ,则应该使用损耗C.*LLR(X*theta')+(1-C).*(LLR(X*theta')+X*theta')

If your labels are in {-1,1} , then the loss should be LLR(C.*X*theta') . 如果您的标签位于{-1,1} ,则损失应为LLR(C.*X*theta')

You seem to be using only the first part of the first type of loss function. 您似乎只使用第一类损失函数的第一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM