【发布时间】:2016-05-26 23:36:46
【问题描述】:
我正在使用批量梯度下降实现逻辑回归。输入样本将分为两类。类是 1 和 0。在训练数据时,我使用了以下 sigmoid 函数:
t = 1 ./ (1 + exp(-z));
在哪里
z = x*theta
我正在使用以下成本函数来计算成本,以确定何时停止训练。
function cost = computeCost(x, y, theta)
htheta = sigmoid(x*theta);
cost = sum(-y .* log(htheta) - (1-y) .* log(1-htheta));
end
我将每一步的成本设为 NaN,因为在大多数情况下,htheta 的值要么为 1,要么为零。我应该怎么做才能确定每次迭代的成本值?
这是逻辑回归的梯度下降代码:
function [theta,cost_history] = batchGD(x,y,theta,alpha)
cost_history = zeros(1000,1);
for iter=1:1000
htheta = sigmoid(x*theta);
new_theta = zeros(size(theta,1),1);
for feature=1:size(theta,1)
new_theta(feature) = theta(feature) - alpha * sum((htheta - y) .*x(:,feature))
end
theta = new_theta;
cost_history(iter) = computeCost(x,y,theta);
end
end
【问题讨论】:
-
您使用什么语言进行编码?您能否提供一个最小的可重现示例以及数据?
-
数据由 57 个特征组成,标签为 1 或 0,即 y 向量
-
我可以提供更多详细信息吗?
-
如果您能提供您的数据文件的链接,那就太好了。您是否通过
cost_history变量验证 NaN 值?请注意,此变量的大小为 1000,但您正在运行 5000000 次迭代。所以cost_history(iter) = computeCost(x,y,theta);可能定义了超出范围的值。 -
这高度依赖于您忽略包含的输入数据。你的数据矩阵
x是什么样的?
标签: matlab machine-learning classification logistic-regression gradient-descent