【问题标题】:Logistic Regression Gradient Descent in MatlabMatlab中的逻辑回归梯度下降
【发布时间】:2014-03-21 23:01:48
【问题描述】:

这里是代码

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

我遇到的问题是对数似然比首先降低,然后开始增加。这是梯度下降算法的问题还是代码的问题。

【问题讨论】:

  • 标签是 D(:,1) 0/1 还是 -1/1。可以向我们展示每 10 次迭代的权重(theta_new)梯度 llr,看起来模型过度拟合了,因为你迭代了 100000 次,这很多。当梯度达到某个值(例如:1e-4)时,您应该退出。
  • 你可以尝试正则化,在l2正则化中,grad = (C-sigmoid(X*theta_new')')*X + thread_new

标签: matlab machine-learning logistic-regression gradient-descent


【解决方案1】:

您的目标函数似乎有问题。

如果标签(C)在{0,1},那么你应该使用损失C.*LLR(X*theta')+(1-C).*(LLR(X*theta')+X*theta')

如果你的标签在{-1,1},那么损失应该是LLR(C.*X*theta')。

您似乎只使用了第一类损失函数的第一部分。

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2015-07-15
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 2015-05-03
    • 2014-08-26
    相关资源
    最近更新 更多