【问题标题】:Logistic Regression, Gradient Descent Octave implementation逻辑回归,梯度下降 Octave 实现
【发布时间】: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


    【解决方案1】:

    我相信你也应该得到梯度的平均值grad = grad / m,就像成本J一样。但是自从我上次上 Andrew Ng 的课程以来已经有一段时间了,所以我可能错了。

    【讨论】:

      【解决方案2】:

      试试这个..这就是我为我的任务所做的。我认为你错过了 m 的除法。

      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
      
      % 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
      %
      h=sigmoid(X*theta);
      t1=(y'*log(h));
      t2=(1-y)'*log(1-h);
      J=-(t1+t2)/m;
      
      for i =1:length(theta)
      grad(i)=((h-y)'*X(:,i))/m;
      endfor
      
      % =============================================================
      
      end
      

      【讨论】:

      • 您好,感谢您的回复。您的代码工作正常,但我只想知道我误解了公式的哪一部分。我已经尝试过除以 m。像这样“grad(j) = theta(j)-temp_sum/m;”但仍然得到错误的答案。
      • 基本上你做错的是 grad(i) = theta(i) - temp_sum。您必须了解 temp_sum 中的值实际上是梯度。成本函数的导数称为梯度。用 grad(i) = temp_sum/ m 替换它,你的代码就可以正常工作了
      猜你喜欢
      • 2014-08-26
      • 2021-12-22
      • 2015-05-03
      • 2015-07-15
      • 2014-03-21
      • 2020-05-01
      • 1970-01-01
      • 2020-02-25
      相关资源
      最近更新 更多