逻辑回归成本函数 [英] Logisitic Regression Cost Function

查看:121
本文介绍了逻辑回归成本函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function [J, grad] = costFunction(theta, X, y)
m = length(y);
h = sigmoid(X*theta);
sh = sigmoid(h);
grad = (1/m)*X'*(sh - y);
J = (1/m)*sum(-y.*log(sh) - (1 - y).*log(1 - sh));

end

我正在尝试为Logistic回归计算成本函数.有人可以告诉我为什么这不正确吗?

I'm trying to compute the cost function for logistic regression. Can someone please tell me why this isn't accurate?

更新:乙状结肠功能

function g = sigmoid(z)

g = zeros(size(z));
g = 1./(1 + exp(1).^(-z));

end

推荐答案

如Dan所述,您的costFunction调用了Sigmoid两次.首先,它在X*theta上执行sigmoid函数;然后它对sigmoid(X*theta)的结果再次执行S型函数.因此,sh = sigmoid(sigmoid(X*theta)).您的cost函数应该只调用一次sigmoid函数.

As Dan stated, your costFunction calls sigmoid twice. First, it performs the sigmoid function on X*theta; then it performs the sigmoid function again on the result of sigmoid(X*theta). Thus, sh = sigmoid(sigmoid(X*theta)). Your cost function should only call the sigmoid function once.

请参见下面的代码,我删除了sh变量,并在其他所有地方都将其替换为h.这样会使Sigmoid函数仅被调用一次.

See the code below, I removed the sh variable and replaced it with h everywhere else. This causes the sigmoid function to only be called once.

function [J, grad] = costFunction(theta, X, y)
m = length(y);
h = sigmoid(X*theta);
grad = (1/m)*X'*(h - y);
J = (1/m)*sum(-y.*log(h) - (1 - y).*log(1 - h));

end

这篇关于逻辑回归成本函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆