cubearth
cubearth

Reputation: 1153

matlab: fminunc returns 'User objective function returned complex; trying a new point...'

fminunc is returning 'User objective function returned complex; trying a new point...' when 'iter-detailed' is on. I know it is because of my cost function, for some reason it is returning a complex component (to be precise it's returning J = NaN + NaNi). Any ideas as to why it is doing this, and how I can fix it? P.S. I have a very large set features(about 1000), could it be that it's going over the max value or accuracy? P.s. cost function & gradient:

hyp = sigmoid(X * theta);
reg = (lambda/(2*m))*sum((theta(2:end).^2));

J = (1/m * sum((-y .* log(hyp)) - ((1-y) .* log(1-hyp)))) + reg;

grad(1) = (1/m * ((hyp-y)' * X(:,1)))';
grad(2:end) = (1/m * ((hyp-y)' * X(:,2:end)))' + (lambda/m * theta(2:end));

Upvotes: 0

Views: 1298

Answers (1)

Matt Krause
Matt Krause

Reputation: 1143

Any chance you're hitting a weird confluence of bugs where:

  • sigmoid(X*theta) is negative OR greater than one (which would give you the complex part when you take its log in line 3 ); and
  • either m or y is NaN?

I hadn't noticed this before, but if a NaN interacts with a complex number, you get a "complex" NaN.

>> (1+2i) * nan

ans =

      NaN +    NaNi

Upvotes: 1

Related Questions