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

前端 未结 2 1428
鱼传尺愫
鱼传尺愫 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:34

    Your sigmoid function is incorrect. The incoming data type is a vector but the operations you are using are performing matrix division. This needs to be element-wise.

    function g = sigmoid(z)
        g = 1.0 ./ (1.0 + exp(-z));
    end
    

    By doing 1 / A where A is an expression, you are in fact compute the inverse of A Since inverses only exist for square matrices, this will compute the pseudo-inverse which is definitely not what you want.

    You can keep most of your costFunction code the same as you're using the dot product. I would get rid of the sum since that is implied with the dot product. I'll mark my changes with comments:

    function [J, grad] = costFunction(theta, X, y)
    
    m = length(y); % number of training examples
    
    % You need to return the following variables correctly 
    %J = 0; %#ok <-- Don't need to declare this as you'll create the variables later
    %grad = zeros(size(theta)); %#ok
    
    hx = sigmoid(X * theta);  % <-- Remove transpose
    m = length(X);
    
    J = (-y' * log(hx) - (1 - y')*log(1 - hx)) / m; % <-- Remove sum
    
    grad = X' * (hx - y) / m;
    
    end
    

提交回复
热议问题