-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Thoughts on Interface Design #4
Comments
If possible, it would be better if grad() could recognize x is scalar and call different routines appropriately. The grad() function of numDeriv seems to work OK. However, recognize this might be quite difficult. Example: cat("\nNOW VECTORS\n") |
It seems to be possible, just call Should we do similar things with higher-order like a hypothetical |
If possible, we should make our codes "just work" because most R users don't even know what a gradient is. Seriously, I have had quite advanced researchers who were not in the math/stat field ask that. Moreover, we may call the functions from within optim, optimr or other functions that users don't actually see. |
Yes, I think it's totally reasonable to do this, and we can even make But the thing that if So maybe just the way in the first paragraph of this comment, make |
BTW, I updated the first post with some new thought on arguments of the high-level interface APIs. The default options are for the most general case without any assumption. But in cases like Is there any mechanism for |
Currently, there are functions All the functions
|
Dimensions
The package need to deal with
And the package will provide separate functions, like
deriv
for derivatives,and
grad
for gradients.It is expected that users understand the difference and use corresponding functions when dealing with problems in different dimensionality.
Maybe we can include some error messages like "
grad
is not for R1 -> R1 functions, maybe you want to usederiv
?"if the user uses
grad
for a scalar function of one variable.Arguments
The arguments to the interface functions will look like:
grad(func, x = NULL, mode = c("forward", "reverse"), ...)
where
func
is the original function.x
is where the gradient is calculated, and it should be the first argument tofunc
except the arguments matched by...
. If it isNULL
, then a gradient function will be returned.mode
is whether forward or reverse mode automatic differentiation is used,...
are other arguments to the functionfunc
.Update
After wrapping the APIs of
ForwardDiff
andReverseDiff
, I have some new ideas about the arguments to the interface functions, it might be like:grad(func, x = NULL, mode = c("forward", "reverse"), xsize = x, use_tape = FALSE, use_compiled_tape = c("NO", "YES", "FORCE"), ...)
where
func
is the original function.x
is where the gradient is calculated, or a list of inputs where you want to calculate the gradient w.r.t.. Ifx
isNULL
, then a gradient function will be returned.mode
is whether forward or reverse mode automatic differentiation is used.xsize
is some vector of the same size ofx
, or a list with every element the same size of the corresponding element inx
. Ifx
is not given andxsize
is given, then the return will be a more performant gradient function specialized for the same size inputs withxsize
.use_tape
,use_compiled_tape
are whether to use tape or compiled tape in reverse mode automatic differentiation. They will only have effect whenmode
isreverse
,x
is not given andxsize
is given, because this is the only time thatgrad
can return a much more performant gradient function specialized for the input sizexsize
and the functionfunc
. Note that the resulting function will be more performant when use compiled tape, but the compiling can take substantial time to finish depending on the function and the size of input. Settinguse_compiled_tape
to "YES" will carry on a check to find out whether the tape compiling will take a very loooooong time and it will fall back to not compiling tape if the time is "very long" and it will give a warning about this, but settinguse_compiled_tape
toFORCE
will carry on compiling the tape anyway....
are other arguments to the functionfunc
.The text was updated successfully, but these errors were encountered: