0

I am trying to code a ML algorithm in Matlab. These are my different functions:

sigmoid.m:

function g = sigmoid(z)
g = zeros(size(z));
g = 1 ./ (1+exp(z));

costFunction.m

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

m = length(y); % number of training examples
z = -X * theta;
g = sigmoid(z);
J = 1/m * ((-y * log(g)') - ((1 - y) * log(1 - g)'));

grad = zeros(size(theta'));
grad = (1/m) * (X' * (g - y));

ex2.m (This is the main file of my project and I put the relative lines I get this error message)

options = optimset('GradObj', 'on', 'MaxIter', 400);

[theta, cost] = ...
    fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);

The error message:

Error using fminunc (line 348) Supplied objective function must return a scalar value.

Error in ex2 (line 97) fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);

I don't know is there enough information above or not? If not, let me know to add extra information.

Hasani
  • 3,543
  • 14
  • 65
  • 125
  • 1
    The error is saying that the objective function (here that's `costFunction()`) needs to return a single value (i.e. a scalar value), not an array or matrix. You don't show what `X` and `y` are, nor your `initial_theta`, so it's hard to say what's getting returned by the function since we don't know what's being passed in. It would be helpful if you gave a [mcve] – Justin Jan 20 '19 at 11:29
  • 1
    Your cost function is returning both `J` and `grad`, so it is returning a vector and not the scalar value `fminunc` expects. I can't remember my neural network maths very well, but I think you need to minimise `J`, so you should edit `costFunction.m` to return only this. – RPM Jan 20 '19 at 11:57
  • @Justin: `X is a 100*1 matrix` , `y is a 100*1 matrix, `initial_theta is a 3*1 matrix`, ` g is a 100*1 matrix` , `J is a 100*100 matrix` and `grad is a 3*1 matrix`. – Hasani Jan 20 '19 at 14:56
  • @RPM: This is an assignment on coursera and costfunction's return values are predefined! So you mean they did mistake in creating this exercise? – Hasani Jan 20 '19 at 15:01
  • @RPM: Maybe I should do the multiplication in `J` , in reverse side? At this time I do multiplying `y = 100*1` to `transpose of log(g) that is a 1*1001 and causes a `100*100 matrix J`. If I transpose `y` instead of `log(g)` I will get a `1*1` matrix! – Hasani Jan 20 '19 at 15:07
  • @RPM, I did what I said above and seems the problem solved. Thanks. – Hasani Jan 20 '19 at 15:09
  • @Justin: Thank you! I got the idea from your comment and changed my `J` as I wrote above and seems the problem solved. – Hasani Jan 20 '19 at 15:10

1 Answers1

0

I changed the following line of code:

J = 1/m * ((-y * log(g)') - ((1 - y) * log(1 - g)'));

To the following line of code:

J = 1/m * (((-y)' * log(g)) - ((1 - y)' * log(1 - g)));

And problem solved! The y and g were 100*1 matrices and with previous code I had J=100*100 matrix, but with new code I have J=1*1 matrix or scalar number and problem solved!

Hasani
  • 3,543
  • 14
  • 65
  • 125