forum.alglib.net
http://forum.alglib.net/

Trying to get optimization to work with custom cost function
http://forum.alglib.net/viewtopic.php?f=2&t=618
Page 1 of 1

Author:  GrazyMos [ Tue Sep 25, 2012 8:24 pm ]
Post subject:  Trying to get optimization to work with custom cost function

I wish to replace my custom gradient descent algorithm with an optimized algorithm. The solution contains a cost function which can calculate the cost and the gradient.

Code:
public void function1_grad(double[] x, ref double func, double[] grad, object obj)
{
   NNCostFunction costFunction = (NNCostFunction)obj;
   ILArray<double> parameters = ILMath.array(x);
   ResultCostFunction result = costFunction.costFunction(parameters);
   func = result.Cost; //double
   grad = result.Gradient.ToArray(); //double[]
}


One concept I don't understand is why the gradient is not set as output. When trying to optimize the function it runs the functions one time only (while not changing the initial parameters), while the stopping criteria is set to 50 iterations.

Code:
double epsg = 0;
double epsf = 0;
double epsx = 0;
int maxits = 50;
alglib.minlbfgsstate state;
alglib.minlbfgsreport rep;

alglib.minlbfgscreate(x.Length, 3, x,  out state);
alglib.minlbfgssetcond(state, epsg, epsf, epsx, maxits);
alglib.minlbfgsoptimize(state, function1_grad, null, costFunction); //cost function only is called once
alglib.minlbfgsresults(state, out x, out rep);
System.Console.WriteLine(parameters[r(0,20),0]);
parameters.a = ILMath.array(x);
System.Console.WriteLine(parameters[r(0, 20),0]); //results are similar while they should differ


My guess is that the gradient from the cost function is not used. The question is how can I modify my code so it does use the gradient?

Thanks

Author:  Sergey.Bochkanov [ Thu Sep 27, 2012 6:43 am ]
Post subject:  Re: Trying to get optimization to work with custom cost func

GrazyMos wrote:
Code:
public void function1_grad(double[] x, ref double func, double[] grad, object obj)
{
   NNCostFunction costFunction = (NNCostFunction)obj;
   ILArray<double> parameters = ILMath.array(x);
   ResultCostFunction result = costFunction.costFunction(parameters);
   func = result.Cost; //double
   grad = result.Gradient.ToArray(); //double[]
}


One concept I don't understand is why the gradient is not set as output. When trying to optimize the function it runs the functions one time only (while not changing the initial parameters), while the stopping criteria is set to 50 iterations.
Thanks

Gradient is not set to out in order to avoid excessive reallocation of dynamic arrays. You have preallocated array grad, and you should write into it - without reallocating its memory. BTW, that's the reason why algorithm stops immediately - you function allocates new array and stores gradient into it, but optimizer is not aware of this change and still looks to the old place (filled with default zeros).

I know that C# programmers tend to think that you can easily allocate dynamic structures due to efficient garbage collector :) But optimizers should be as efficient as possible, and memory allocation is a bottleneck even in C#.

Author:  GrazyMos [ Thu Sep 27, 2012 9:38 am ]
Post subject:  Re: Trying to get optimization to work with custom cost func

Thanks, spot on!

Adding a simple array copy solved the problem
Code:
Array.Copy(result.Gradient.ToArray(),  grad, grad.Length);

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/