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
Multi-level derivatives (and seeding) #21
Comments
I think you should avoid this usage. Define your function Why do you need to call |
Because my energy calculations are based on derivatives of other functions. I’m coding up finite elements. I’m calculating the strain energy of the system, which is based on the strain which is the partial derivative of displacement! |
How would you compute I also happen to define functions that compute thermodynamic properties which themselves require derivatives of other properties. I have to implement these "internal" derivatives manually (in general not difficult) to keep a consistent usage of automatic differentiation types. |
Forgot to reply this. Seeding set the |
I can do them manually. I was hoping to not have to :) So nested derivatives are out in forward mode? In one of your reverse mode examples you calculate a derivative and then a further derivative of that. Does this restriction only apply to forward autodiff? |
Not necessarily - but I would always recommend a consistent usage of
You speak about higher order derivatives I guess. This is also possible with |
In your first reply above you said I’m mixing dual and double. Where do I do that in the code I posted? |
Here: dual energy(VectorXdual u)
{
double dfdx = derivative(f, wrt(x), u);
// ...
} Since |
Do you know newton raphson? It’s a technique for finding the zero of a function. My purpose is to find the set of degrees of freedom which minimizes my objective function. Here I call the objective energy. My degrees of freedom are my duals(or vars). And to solve I take the derivative in terms of the vars - this gives me a vector. Then I take the derivatives of this vector - giving a matrix. Solving for Ax = b where A is this matrix and b the aforementioned vector, you should get a new vector x which shows how much to modify your original degrees of freedom to get to the minimum energy. Do this iteratively and you have a generic minimization procedure for any function based on a set of variables. Does this all sound plausible / possible with autodiff? I’m just worried because of our discussing nested derivatives (though what I describe does seem like your example of taking derivatives of a derivative) |
Since dfdx depends on u, any usage of dfdx afterwards will not be autodiff-enabled. Ok - I see what you mean here now |
It's in the core of everything I do! ;) So, you have a minimization problem; you know the objective function; and you want to go up to the Hessian of this objective function to perform the minimization with a Newton-based numerical optimization algorithm? Can you easily implement the gradient of energy function using This is what I do to minimize Gibbs energy when computing chemical equilibrium - the gradient of the Gibbs energy are the chemical potentials, which I can define directly, instead of defining the Gibbs energy, and then using autodiff to compute them. From the chemical potentials, I can compute their gradient, which is the Hessian of Gibbs energy. Important message -- all of this using only first-order automatic differentiation. |
Yes, as you describe, I’m doing a minimization. Good to hear you are using it for similar purposes. I just need to calculate my inner derivatives manually and I should be all set. |
How do you mean “all only first order autodiff” - surely something like the hessian is second order? |
By the way - I actually typedef dual and var as ‘real’ and use that throughout so that I can switch between forward and reverse easily. Works well. |
Sorry I meant define, not typedef. Though I guess typedef would works too? |
Hessian are second-order derivatives of the energy function. However, they are also the first-order derivatives of the gradient of the energy function. If you can implement these gradients using |
This is perfectly fine. |
I'll close this issue, but feel free to reopen it in the future if further discussions are still needed. Thanks. |
I'm getting strange behavior when trying to calculate a gradient. I wonder if it's because in my objective function I use calculate derivatives.
To explain: I see the way one calculates involves seeding autodiff with the target variable:
So if I understand, seeding is a way to (globally?) track the variable of interest. But what happens if my target function also calculates derivatives?
I see in
forward.hpp
thatderivative()
also uses seeding. Do the two clash with one another? Can a nested derivative calculation like this work?The text was updated successfully, but these errors were encountered: