tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.43k stars 1.92k forks source link

Can't take second derivative with respect to tanh, sigmoid, or exp #3252

Open roman-amici opened 4 years ago

roman-amici commented 4 years ago

To get help from the community, we encourage using Stack Overflow and the tensorflow.js tag.

TensorFlow.js version

1.7.4

Browser version

Chrome Version 81.0.4044.138 (Official Build) (64-bit)

Describe the problem or feature request

Tensorflowjs appears to be able to take second derivatives with respect to most common functions. However, when exp, sigmoid, tanh, and presumably others are tried, it fails with the error

Cannot compute gradient of y=f(x) with respect to x. Make sure that the f you passed encloses all operations that lead from x to y.

Code to reproduce the bug / link to feature request

Consider a slight modification of the example code used for tf.grad in the api doc.

const f = x => tf.tanh(x); //or x.tanh()
const g = tf.grad(f);
const gg = tf.grad(g);

const x = tf.tensor1d([2, 3]);
gg(x).print();

This gives the error above. With gg(x).print() line as the line causing the error.

Note that if we just wanted the first derivative

g(x).print();

works just fine. In the above code, if you change tanh to sinh or cosh or a number of other functions, this does work. The functions sigmoid and even exp all exhibit the same behavior.

If you would like to get help from the community, we encourage using Stack Overflow and the tensorflow.js tag.

GitHub issues for this repository are tracked in the tfjs union repository.

Please file your issue there, following the guidance in that issue template.

gaikwadrahul8 commented 1 year ago

Hi, @roman-amici

Apologize for the delayed response and I was trying to replicate the same issue from my end with latest @tensorflow/tfjs@4.10.0 and still it's throwing the same error message so we'll have dig more into this issue and will update you soon. Thank you!

CC :@pyu10055

Please refer below error screenshot :

image