From: Gael de Lannoy on
Hey everybody

I'm working with fminunc() to optimize a given likelihood function. I
provide the gradient and use the large-scale algorithm. It seems I
have a problem in my gradient, because the CheckDerivative option
tells me that the maximum relative discrepancy between derivatives is
very high.

I suspect the problem happens in only a few items of the parameter
vector, and to debug this, I would like to see all finite-derivate
values and not just the maximum difference. Is there any way to have
fminunc show all finite-difference derivatives in the output ? Or to
compute them myself easily using another function maybe?

Many thanks
From: John D'Errico on
"Gael de Lannoy" <gael.delannoy(a)uclouvain.be> wrote in message <i1mqr8$knh$1(a)fred.mathworks.com>...
> Hey everybody
>
> I'm working with fminunc() to optimize a given likelihood function. I
> provide the gradient and use the large-scale algorithm. It seems I
> have a problem in my gradient, because the CheckDerivative option
> tells me that the maximum relative discrepancy between derivatives is
> very high.
>
> I suspect the problem happens in only a few items of the parameter
> vector, and to debug this, I would like to see all finite-derivate
> values and not just the maximum difference. Is there any way to have
> fminunc show all finite-difference derivatives in the output ? Or to
> compute them myself easily using another function maybe?
>
> Many thanks

Learn to use the debugger!

help dbstop

John
From: Gael on
On Jul 15, 1:24 pm, "John D'Errico" <woodch...(a)rochester.rr.com>
wrote:
> "Gael de Lannoy" <gael.delan...(a)uclouvain.be> wrote in message <i1mqr8$kn....(a)fred.mathworks.com>...
>
> > Hey everybody
>
> > I'm working with fminunc() to optimize a given likelihood function. I
> > provide the gradient and use the large-scale algorithm. It seems I
> > have a problem in my gradient, because the CheckDerivative option
> > tells me that the maximum relative discrepancy between derivatives is
> > very high.
>
> > I suspect the problem happens in only a few items of the parameter
> > vector, and to debug this, I would like to see all finite-derivate
> > values and not just the maximum difference. Is there any way to have
> > fminunc show all finite-difference derivatives in the output ? Or to
> > compute them myself easily using another function maybe?
>
> > Many thanks
>
> Learn to use the debugger!
>
> help dbstop
>
> John

Hello john thanks for your answer

I see how dbstop could help me to see the variables of the function i
am optimizing at each step but how can it reveal the full list of
finite-difference derivatives ? It's not straighforward ?

Cheers

Gael
From: Gael on
On Jul 15, 2:30 pm, Gael <gae...(a)gmail.com> wrote:
> On Jul 15, 1:24 pm, "John D'Errico" <woodch...(a)rochester.rr.com>
> wrote:
>
>
>
> > "Gael de Lannoy" <gael.delan...(a)uclouvain.be> wrote in message <i1mqr8$kn...(a)fred.mathworks.com>...
>
> > > Hey everybody
>
> > > I'm working with fminunc() to optimize a given likelihood function. I
> > > provide the gradient and use the large-scale algorithm. It seems I
> > > have a problem in my gradient, because the CheckDerivative option
> > > tells me that the maximum relative discrepancy between derivatives is
> > > very high.
>
> > > I suspect the problem happens in only a few items of the parameter
> > > vector, and to debug this, I would like to see all finite-derivate
> > > values and not just the maximum difference. Is there any way to have
> > > fminunc show all finite-difference derivatives in the output ? Or to
> > > compute them myself easily using another function maybe?
>
> > > Many thanks
>
> > Learn to use the debugger!
>
> > help dbstop
>
> > John
>
> Hello john thanks for your answer
>
> I see how dbstop could help me to see the variables of the function i
> am optimizing at each step but how can it reveal the full list of
> finite-difference derivatives ? It's not straighforward ?
>
> Cheers
>
> Gael

I found the solution. I simply added disp([finite_diff_deriv
analytic_deriv]) into the graderr() function which is called by
fminunc to show the maximum difference between gradients.
From: James Allison on
The gradient is available to output functions for fminunc. You can
define an output function that displays or saves the gradient. Here is
an example to get you started:

function dispfminuncgrad
f = @(x) x(1)^2 + 2*x(2)^2; % objective function
[xopt, fopt, flag] = fminunc(f,[2 2],optimset('OutputFcn',@myout))

function stop = myout(x, optimValues, state) % output function
stop = false;
grad = optimValues.gradient;
grad % display gradient

The fminunc doucumentation is helpful for learning more about output
functions:

http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/fminunc.html
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/f19175.html#f11022

Best Regards,

-James

Gael wrote:
> On Jul 15, 1:24 pm, "John D'Errico" <woodch...(a)rochester.rr.com>
> wrote:
>> "Gael de Lannoy" <gael.delan...(a)uclouvain.be> wrote in message <i1mqr8$kn...(a)fred.mathworks.com>...
>>
>>> Hey everybody
>>> I'm working with fminunc() to optimize a given likelihood function. I
>>> provide the gradient and use the large-scale algorithm. It seems I
>>> have a problem in my gradient, because the CheckDerivative option
>>> tells me that the maximum relative discrepancy between derivatives is
>>> very high.
>>> I suspect the problem happens in only a few items of the parameter
>>> vector, and to debug this, I would like to see all finite-derivate
>>> values and not just the maximum difference. Is there any way to have
>>> fminunc show all finite-difference derivatives in the output ? Or to
>>> compute them myself easily using another function maybe?
>>> Many thanks
>> Learn to use the debugger!
>>
>> help dbstop
>>
>> John
>
> Hello john thanks for your answer
>
> I see how dbstop could help me to see the variables of the function i
> am optimizing at each step but how can it reveal the full list of
> finite-difference derivatives ? It's not straighforward ?
>
> Cheers
>
> Gael