I was wondering how we could use jax (https://github.com/google/jax) to compute a mapping of the derivative.
That is to say :
we have a vector and we want to apply (with the jax framework) a function to it, we call it
and it's a function
My question is : how can we easily retrieve the vector :
For exemple :
from jax import random
from jax import jacfwd, jacrev
import jax.numpy as jnp
key = random.PRNGKey(0)
key, W_key, b_key, input_key = random.split(key, 4)
W = random.normal(W_key, (10, 10))
b = random.normal(b_key, (10, ))
input = random.normal(input_key, (10, ))
One easy way to do that will be to take diagonal of the jacobian, but this method is very slow for high dimensional vector (> 10000). I am only interested in the diagonal of the jacobian ...
def f(input):
return jnp.dot(W, input) + b
J = jacfwd(f, argnums=0)(input)
result = jnp.diagonal(J)
For recall the jabobian matrix is :