deshanadesai / acorns

https://arxiv.org/pdf/2007.05094.pdf An Easy-To-Use Code Generator for Gradients and Hessians
MIT License
30 stars 6 forks source link

Differentiating nested `pow` calls #6

Open SergeiOssokine opened 3 years ago

SergeiOssokine commented 3 years ago

Dear devs,

recently I have been trying to differentiate complicated expressions that eventually result in nested calls to pow. I find that the forward mode derivative for these cases is not taken correctly. Here is the MWE:

c_function = """
function_test(double x){
double x2 = pow(x,2);
double H = pow(x2,2);
}

deriv = acorns.autodiff(c_function, 'H', ['x'], func = 'function_test', output_filename = 'test',
     output_func = 'compute_grad_nested_pow')

This gives as the derivative (pow(pow,(2-1)) * (2 * 0 + pow * 0 * log(pow))) which looks like it's using pow as a variable instead of a function.

Computing directly the derivative of

c_function = """
function_test(double x){
double H = pow(x,4);
}

gives the correct result, (pow(x,(4-1)) * (4 * 1 + x * 0 * log(x))). Unfortunately my expressions are complicated enough that no such simplifications are possible.

SergeiOssokine commented 3 years ago

Dear devs,

have you had a chance to look at this?

Thanks again!

deshanadesai commented 3 years ago

Hi @SergeiOssokine , I sincerely apologize for the delay. You are right, this needs an extension to the current code to include catching FuncCall expressions. We are working on it and should be updating this soon. My apologies!

Also, please feel free to contribute: https://github.com/deshanadesai/acorns/blob/master/CONTRIBUTING.md

We would highly appreciate any help!