【发布时间】:2020-07-23 03:58:31
【问题描述】:
我正在学习 Ng 教授的机器学习课程。 有一个作业需要实现逻辑回归梯度下降。 这是我的代码:
function [J, grad] = costFunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
% parameter for logistic regression and the gradient of the cost
% w.r.t. to the parameters.
% Initialize some useful values
m = length(y); % number of training examples
[~,n] = size(X);
% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
% You should set J to the cost.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
%
J = ((-y'*log(sigmoid(X*theta)))-((1-y)'*log(1-sigmoid(X*theta))))/m;
for j = 1:n
temp_sum = 0;
for i = 1:m
temp_sum+=(sigmoid(X(i,:)*theta)-y(i))*X(i,j);
endfor
grad(j) = theta(j)-temp_sum;
endfor
% =============================================================
end
其中 x 的 h 代表 sigmoid 函数。 我检查了 sigmoid 函数是否正确,但我仍然无法理解该算法的错误之处。 如果您发现任何问题,请告诉我。
【问题讨论】:
标签: octave logistic-regression gradient-descent