1
votes

I have met a problem when practicing coding logistic regression for machine learning course using a Mac version of Matlab_R2016a.

First,this is my code of costFunction which works fine with returning the cost and the gradient:

function [J,grad] = costFunction(X, y, theta)


    % Initialize some useful values
    h=sigmoid(X*theta);
    m = length(y); % number of training examples  

    grad = m^(-1) * ((h-y)'*X)'; 
    J=sum(-y.*log(h)-(1-y).*log(1-h))/m;

end

And this is the sigmoid function:

function h=sigmoid(z)

h = (1 + exp(-1 *z)).^(-1); 

end

Finally, I applied costFunction to fminunc with the form like this in the following picture(enter image description here). But, the strange thing was that it said my Inner matrix dimensions of X*theta in costFunction was wrong(It was fine before). I appreciate some solutions or ideas from you.

1

1 Answers

1
votes

Please control the order of the parameters in your anonymous function call inside fminunc.

In your function "costFunction" they are X,y,theta; when you call fminunc(@(t) costFunction(t,X,y) ...) you have X and y as second and third parameter, respectively.

Hope this helps.