fminunc step size too small
Show older comments
Hello Matlab community,
I am using fminunc to fit a function to a set of datapoints. The optimizer stops due to small step size however I have noticed the steps it is taking are way too small. I don't want to set the minimum step size lower than 1e-3 but if I do so it just stops immediately.
The solution I have found is to provide the function with my own calculated gradient and manually multiplying it by 1e6.
Is there an optimizer option that can rescale the step size to gradient ratio in a similar way?
My optimizer options are:
options = optimoptions('fminunc', ...
'Display', 'iter', ...
'Algorithm', 'trust-region', ...
'HessianFcn','objective', ...
'SpecifyObjectiveGradient',true, ...
'StepTolerance', 5e-3, ...,
"FiniteDifferenceStepSize", 0.1, ...,
"FunctionTolerance",0.00001, ...
"OptimalityTolerance",2e-6/factor, ...
'MaxFunctionEvaluations', 10000, ...
'MaxIterations', 10000, ...
'OutputFcn', @saveIterations);
Thank you in advance for your help!
Accepted Answer
More Answers (1)
The solution I have found is to provide the function with my own calculated gradient and manually multiplying it by 1e6.
That might mean your initial point is poorly chosen. In particular, it is chosen in a region where the objective function is almost, but not perfectly flat. This is similar to what is happening in the following example. Notice that the gradient is never exactly zero anywhere except at the global minimum x=0, but for numerical purposes, it may as well be. The optimization cannot move until the initial point is chosen in an area where it has substantial magnitude -
fun=@(x)1-exp(-x.^2/2);
opts=optimoptions('fminunc','Display','none');
x1 = fminunc(fun,3000,opts)
x2 = fminunc(fun,30,opts)
x3 = fminunc(fun,3,opts)
4 Comments
The function has a random threshold but I fix it during the optimization by fixing the random seed meaning that the result is deterministic.
Do you set the random seed inside the objective function? If so, that is one more reason why your initial guess x0 is probably poor. It means you are choosing it without knowing the random threshold that determines your objective function, making x0 essentially an arbitrary guess. An initial guess should not be chosen randomly.
Matilda
on 7 Feb 2025
Matt J
on 7 Feb 2025
What I think the issue might be is the fact that the gradient gets much less steep around the minimum creating a kind of minimum valley.
If so, you have an ill-conditioned problem (essentially a case of non-unique solutions), and need to regularize it. No algorithm can be guaranteed to reach a particular solution when a problem is ill-conditioned. You can also expect different results for different x0 and on different CPUs.
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!