HIPS / autograd

Efficiently computes derivatives of NumPy code.
MIT License
7.03k stars 915 forks source link

Is it possible to see gradient function? #587

Open Samuel-Bachorik opened 1 year ago

Samuel-Bachorik commented 1 year ago

Hi, when I use autograd, it is possible to see its gradient function? Or in other words, it is possible to see derivative of that function? Or is it possible to see computational graph?

For example, I want to see grad_tanh function

import autograd.numpy as np  # Thinly-wrapped numpy
from autograd import grad    # The only autograd function you may ever need

def tanh(x):                 # Define a function
       y = np.exp(-2.0 * x)
       return (1.0 - y) / (1.0 + y)

grad_tanh = grad(tanh)       # Obtain its gradient function

Thank you

Bornesf commented 1 year ago

Have you got the answer? I have the same problem. I need to get the formula of the first derivative of the function

99991 commented 4 months ago

I do not know whether this is possible with autograd, but if your function is simple enough, https://www.matrixcalculus.org/ can give you both the mathematical formula and a Python program to compute the gradient.

Using your function (vector(1) - exp(-2 * x)) ./ (vector(1) + exp(-2 * x)):

Screenshot 2024-07-25 at 10-27-54 Matrix Calculus

I used the Jacobian here since grad only works for scalar-valued functions, but I wanted to show a more general case. You can also select x is a [scalar] from the list on the website to get the gradient for a scalar value.

import autograd.numpy as np
from autograd import jacobian

# Output from https://matrixcalculus.org
def fAndG(x):
    assert isinstance(x, np.ndarray)
    dim = x.shape
    assert len(dim) == 1
    x_rows = dim[0]

    t_0 = np.exp(-(2 * x))
    t_1 = (np.ones(x_rows) + t_0)
    t_2 = (np.ones(x_rows) - t_0)
    functionValue = (t_2 / t_1)
    gradient = ((2 * np.diag((t_0 / t_1))) + (2 * np.diag(((t_2 * t_0) / (t_1 * t_1)))))

    return functionValue, gradient

def tanh(x):
   y = np.exp(-2.0 * x)
   return (1.0 - y) / (1.0 + y)

x = np.array([0.1, 0.2, 0.3])

tanh_x, jacobian_tanh_x = fAndG(x)

# tanh(x) matches
assert np.allclose(tanh_x, tanh(x))

# Jacobian matches
assert np.allclose(jacobian_tanh_x, jacobian(tanh)(x))

You could also visualize the computational graph with Jax, but that is a bit more involved: https://bnikolic.co.uk/blog/python/jax/2020/10/20/jax-outputgraph.html