Why DerivativeCheck would be allowed to be on while there is no GradObj?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
When GradObj is off, and DerivativeCheck is on, fmincon starts to run without any error or warning. Why would this be the case? That is, if there is no gradient supplied, there cannot be derivattive check. So why would MATLAB allow the DerivativeCheck to be on if there is no gradient available?
options = optimset(...
'MaxFunEvals',10000,...
'TolFun',1e-4,...
'TolX',1e-4,...
'Algorithm','sqp',...
'GradObj','off',...
'DerivativeCheck','on',...
'Display','iter',...
'FunValCheck','off');
0 Kommentare
Akzeptierte Antwort
Matt J
am 29 Jan. 2023
Here's a reason why you might want to allow DerivativeCheck to be on even when the analytical gradient computations are all off. Suppose the user has coded 6 different implementations of the gradient calculation and wants to compare them all for speed. Suppose also that she wants to run the optimization with finite difference gradient computations as a baseline for the comparison.
In this situation, you would want to have DerivativeCheck='on' for 6 different runs. To run the finite differencing baseline, however, your proposal would force the user to set both SpecifyObjectiveGradient=false and DerivativeCheck='off'. Some people would prefer just to set SpecifyObjectiveGradient=false and not to have to fuss with additional settings for just that one case.
1 Kommentar
Weitere Antworten (3)
John D'Errico
am 27 Jan. 2023
Verschoben: Walter Roberson
am 27 Jan. 2023
You can set other options that will be ignored if they are not applicable. Would you rather have the code fail for no reason on a problem that has a solution?
3 Kommentare
John D'Errico
am 29 Jan. 2023
But why does it matter in the least? The options were not set incorrectly in any way that would cause a problem. It is just that one option is not needed, so the state for that property is irrelevant.
This is consistent with all of the optimization toolbox codes. For example, look at fzero.
optimset('fzero')
Here, for example, there are many options that are completely ignored. You can set them as you wish. Only 5 options in that list will be looked at, and there is no flag issued if you set one of the others.
help fzero
Similarly, that one of the options that fmincon can use under some circumstances is ignored under some combination of the other options, seems irrelevant. You might easily wish to leave that option set, and when a gradient is actually supplied, then it will become active.
Could a warning message be issued? Yes. I suppose the code could check for any out of place options in that list. But why is it remotely an issue?
Matt J
am 28 Jan. 2023
Bearbeitet: Matt J
am 28 Jan. 2023
I would say that no warning is strictly necessary because one of two cases are possible:
(1) DerivativeCheck='on' was intentional, but GradObj='off' was unintentional. If so, you will be warned that something is wrong by the fact that no derivative check output appears.
(2) GradObj='off' was intentional, but DerivativeCheck='on' was unintentional. If so, it would be your wish that the optimization proceed without a derivative check and, indeed, that is what happens.
That said, I do agree that an explicit warning message in case (1) would be more aesthetic.
6 Kommentare
Matt J
am 29 Jan. 2023
Bearbeitet: Matt J
am 29 Jan. 2023
The user sees that things still run fine with nonsense settings.
It might be overstating things slightly to say that the settings are "nonsense". The main purpose of DerivativeCheck is to prevent the optimization from using analytical gradient calculations that are faulty. However, if SpecifyObjectiveGradient is turned off, the user is not supplying the gradient, and so there is no danger this will occur. So, arguably, the purpose of DerivativeCheck has been fulfilled and the optimization should just proceed care-free.
As I mention in my other answer, it would be nice if Matlab could warn you when you shut off analytical gradient calculations accidentally, or forget to turn them back on. But that is simply not always something Matlab can detect, and in any case is not something DerivativeCheck was primarily designed to protect against.
Matt J
am 28 Jan. 2023
Bearbeitet: Matt J
am 28 Jan. 2023
So why would MATLAB allow the DerivativeCheck to be on if there is no gradient available?
Because maybe SpecifyConstraintGradient=true, even if SpecyObjectiveGradient=false. In your example, this is not the case. In general however, there are 4 possible combinations of settings for SpecifyObjectiveGradient and SpecifyConstraintGradient and only one of these combinations is suspicious when DerivativeCheck='on'. Therefore, there is very limited protection a warning message could provide against users accidentally turning off analytical gradient computation.
3 Kommentare
Matt J
am 29 Jan. 2023
Bearbeitet: Matt J
am 29 Jan. 2023
My OP has nothing to do with SpecifyConstraintGradient.
It does, and I explained why. At this point, I'm not sure if you're actually giving any thought to any of the answers being given to you. However, I'll give it one more try...
The following settings are not nonsense. They could have been given intentionally by the user and Matlab has no reason to issue a warning or think anything is wrong:
options = optimoptions('fmincon','DerivativeCheck','on',...
'SpecifyObjectiveGradient',false,...
'SpecifyConstraintGradient',true);
Likewise, the following settings are not nonsense. No warnings are issued and no reason to think there should be.
options = optimoptions('fmincon','DerivativeCheck','on',...
'SpecifyObjectiveGradient',true,...
'SpecifyConstraintGradient',false);
While both of these settings may be innocuous, however, there is also a hazard. Possibly, the user meant to turn both SpecifyObjectiveGradient and SpecifyConstraintGradient on, but mis-entered one of the settings. If so, the optimization will run slower because finite differencing is used instead of an analytical calculation. But Matlab has no way to intercept this hazard. The only way to intercept it is for the user to pay attention to the details of the derivative check output.
Now let's look at the scenario your OP raised:
options = optimoptions('fmincon','DerivativeCheck','on',...
'SpecifyObjectiveGradient',false,...
'SpecifyConstraintGradient',false);
This case has the same hazard as the first two combinations. There is a possibility that one or both of SpecifyObjectiveGradient and SpecifyConstraintGradient were supposed to be turned on. In this case, however, Matlab can intercept the hazard because it is suspicious for both gradient calculations to be turned off yet to have DerivativeCheck='on'. So, what you propose would improve things. However, since the user still has to be alert to the DerivativeCheck output in the other cases, it is a very partial solution to the problem.
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!