How to write cost function formula from Andrew Ng assignment in Octave?

前端 未结 2 1464
鱼传尺愫
鱼传尺愫 2021-01-14 16:14

My implementation (see below) gives the scalar value 3.18, which is not the right answer. The value should be 0.693. Where does my code deviate from the equation?

Her

2条回答
  •  忘掉有多难
    2021-01-14 16:41

    This is the code for the sigmoid function which I think you have made mistake in:

    function g = sigmoid(z)
       g = zeros(size(z));
       temp=1+exp(-1.*z);
       g=1./temp;
    end
    
    
    function [J, grad] = costFunction(theta, X, y)
       m = length(y); 
       J = 0;
       grad = zeros(size(theta));
       h=X*theta;
       xtemp=sigmoid(h);
       temp1=(-y'*log(xtemp));
       temp2=(1-y)'*log(1-xtemp);
       J=1/m*sum(temp1-temp2);
       grad=1/m*(X'*(xtemp-y));
    end
    

    And I think it should be (1-y)' as shown in temp2=(1-y)'

提交回复
热议问题