这是代码
function [theta] = LR(D)% D 是包含特征变量和类标签的数据% 现在将 D 分解为 X 和 C %注意 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'))) endthetaopt = theta_newendfunction a = LLR( z )a= 1.*log(1.0 + exp(-z));endfunction 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')
。
你似乎只使用了第一种损失函数的第一部分。