Skip to content
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

Bug in AutoDiff? #19

Open
bharswami opened this issue Jun 13, 2024 · 9 comments
Open

Bug in AutoDiff? #19

bharswami opened this issue Jun 13, 2024 · 9 comments

Comments

@bharswami
Copy link

bharswami commented Jun 13, 2024

No description provided.

@bharswami
Copy link
Author

Withdraw the issue. It seems fixed.

@bharswami
Copy link
Author

I would however like to know this:
Do we have to call AutoDiffJacobianAutoDiff every time to get the Jacobian at different points? Is the auto differentiated code available so that that can be called instead?

@martinResearch
Copy link
Owner

Yes we have to call AutoDiffJacobianAutoDiff every time to get the Jacobian at different points. There is no differentiated code generated that could be reuse. This library implements forward autodiff through operator overloading. You would need to use a library that does autodiff through source code transformation if you want to have differentiated code (see implementation section here https://en.wikipedia.org/wiki/Automatic_differentiation).

@bharswami
Copy link
Author

Can you suggest a similar AutoDiff package for C++? Something that calculates Jacobian.

@bharswami
Copy link
Author

bharswami commented Jun 15, 2024

I am looking to save on computation time. Or is there a way to make AutoDiff in Matlab faster?

@bharswami
Copy link
Author

Also, when calling the function to be differentiated, do we need to set the intermediary variables to type AutoDiff? If I don't, I get the error, cannot convert from type "AutoDiff" to "double".

@martinResearch
Copy link
Owner

"Can you suggest a similar AutoDiff package for C++? Something that calculates Jacobian."
There a many C++ packages to do autodiff with various pro and cons. I have not experience with autodiff in C++ a quick seqrch on google lead to https://github.com/autodiff/autodiff, mayeb worth lookin into?

"is there a way to make AutoDiff in Matlab faster?". Not sure, you can give a try to atlernative libraries mentioned in the readme. As said in the readme It is likely that the speed could be improved by representing Jacobian matrices by their transpose, due to the way Matlab represents internally sparse matrices. That would necessitate quite a bit of work. It is also sometimes possible to get a significative speed up by rewriting diffrently the function that you want to differentiate (see #1 (comment)).

"Also, when calling the function to be differentiated, do we need to set the intermediary variables to type AutoDiff?" that should be done automatically. Have a look at the examples to see that. Do you have a small example you can share to reproduce the problem you are getting?

@bharswami
Copy link
Author

bharswami commented Jun 15, 2024

"Can you suggest a similar AutoDiff package for C++? Something that calculates Jacobian." There a many C++ packages to do autodiff with various pro and cons. I have not experience with autodiff in C++ a quick seqrch on google lead to https://github.com/autodiff/autodiff, mayeb worth lookin into?
I checked this. It is way too slow for my liking.
"is there a way to make AutoDiff in Matlab faster?". Not sure, you can give a try to atlernative libraries mentioned in the readme. As said in the readme It is likely that the speed could be improved by representing Jacobian matrices by their transpose, due to the way Matlab represents internally sparse matrices. That would necessitate quite a bit of work. It is also sometimes possible to get a significative speed up by rewriting diffrently the function that you want to differentiate (see #1 (comment)).
I modified the AutoDiff code to treat derivatives as "sparse". Also I am using the Jacobians as sparse, since the optimization toolbox I use needs it so. I will have to see if I can write my function differently.
"Also, when calling the function to be differentiated, do we need to set the intermediary variables to type AutoDiff?" that should be done automatically. Have a look at the examples to see that. Do you have a small example you can share to reproduce the problem you are getting?
This doesn't happen automatically when I use it. Suppose a is initialised as double(zeros(10,1)), b as AutoDiff(zeros(10,1)). "a=a + b" doesn't work.

@martinResearch
Copy link
Owner

In the tests I have the lines

a = randn(3, 3);
f = @(x) a + x ;
CheckAutoDiffJacobian(f, randn(3, 3), 1e-7);

The array a is a normal matlab array and it works. I don't have matlab installed to test your line of code but it seems to do the same operation as the lines above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants