-
Notifications
You must be signed in to change notification settings - Fork 166
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
Derivatives of General Functions #17
Comments
Hi @kewp - thanks for reporting this. Could you please post a complete example to help me test and identify what is going on? |
No problem. Here is the full code: // C++ includes
#include <iostream>
using namespace std;
#include <eigen3/Eigen/Core>
using namespace Eigen;
#include <autodiff/reverse.hpp>
#include <autodiff/reverse/eigen.hpp>
using namespace autodiff;
var get_x()
{
var t = 0.0;
int i; for (i=0; i<2; i++)
t += 1;
return t;
}
var f(var x)
{
return 1 + x + x*x + 1/x + log(x);
}
int main(int argc, char *argv[])
{
var x = get_x(); // the input variable x
var u = f(x); // the output variable u
Derivatives dud = derivatives(u); // evaluate all derivatives of u
var dudx = dud(x); // extract the derivative du/dx
cout << "u = " << u << endl; // print the evaluated output u
cout << "du/dx = " << dudx << endl; // print the evaluated derivative du/dx
return 0;
} I'm getting the output
To "fix" this change |
In the earlier implementations of the reverse algorithm, one could retrieve the derivative of a variable with respect to any intermediate variable in the expression tree. For example, it would be possible to retrieve the derivative of var x = 1.0; // x is a leaf variable (in a leaf node of the expression tree)
var y = 2.0; // y is a leaf variable
var z = x * y; // z is an intermediate variable (in an intermediate node of the expression tree)
var u = sin(z); // u is the root variable (in the node of the expression tree)
Derivatives dud = derivatives(u); However, this is not an appropriate default behavior, because it can dangerously grow the container The decision was then to limit the record of derivatives wrt to leaf variables only (e.g., Maybe what could be envisioned in the future is the implementation of a method, say, I hope this clarifies! :) |
Thank you, that helps me understand. Is this the same behavior you see in forward mode, only tracking the leaf nodes? |
Oh and just to clarify - a leaf variable is one that is not built up from other variables (vars), only things like double and int... ? |
Yes.
In a forward mode, once you define a function To give an example, both codes below are equivalent: dual x = 8.0;
dual y = 10.0;
double dudx = derivative(f, wrt(x), x, y); dual t = 2.0;
dual x = t*t*t;
dual y = 5*t;
double dudx = derivative(f, wrt(x), x, y); // this will work just fine! |
I'm reconsidering what I wrote earlier:
Since the expression tree stored in the In short - I'm starting to think that |
This issue has now been resolved with PRs #111 and #112 . There are some regressions in the API, though, and the following is the modified code that would work with these fixes: // C++ includes
#include <iostream>
using namespace std;
#include <Eigen/Core>
using namespace Eigen;
#include <autodiff/reverse.hpp>
#include <autodiff/reverse/eigen.hpp>
using namespace autodiff;
var get_x()
{
var t = 0.0;
int i; for (i=0; i<2; i++)
t += 1;
return t;
}
var f(var x)
{
return 1 + x + x*x + 1/x + log(x);
}
int main(int argc, char *argv[])
{
var x = get_x(); // the input variable x
var u = f(x); // the output variable u
double [dudx] = derivatives(u, wrt(x)); // evaluate all derivatives of u
cout << "u = " << u << endl; // print the evaluated output u
cout << "du/dx = " << dudx << endl; // print the evaluated derivative du/dx
return 0;
} |
I think I might have made a mistake in my understanding of
autodiff
. Taking the reverse single variable example as a starting point:This outputs
Now look what happens when I replace this with the following:
and then
So I'm getting
x
with a function which is a loop. This is now the output:What happened? Even though
x
is still equal2.0
I can't get a derivative. How did myvar x
suddenly not be usable for the target derivative variable?The text was updated successfully, but these errors were encountered: