Open twhughes opened 5 years ago
update: I managed to get something working but it isn't very robust, the code is below
@unary_to_nary
def jacobian_forward(fun, x):
""" Compute jacobian of fun with respect to x using forward mode differentiation"""
jvp = make_jvp(fun, x)
ans = fun(x)
grads = map(lambda b: jvp(b)[1], vspace(x).standard_basis())
return np.reshape(np.stack(grads), jac_shape(x, ans)) # jac_shape just uses x and ans to determine the jacobian shape
Following Issue #439, I'm interested in implementing a version of the
jacobian
function fromdifferential_operators.py
but using forward mode differentiation.From this comment from @j-towns, it seems to make this work one would need
To get the full Jacobian of f you just need to write a loop to evaluate make_jvp(f)(x)(v) for each v in the standard basis of f's domain.
I'm trying to implement this below but running into some issues and would really appreciate help.
The reverse mode
jacobian
function defined indifferential_operators.py
is copied below:I am trying to implement a forward mode version of this function using
make_jvp
instead ofmake_vjp
, which is pasted belowHowever, this gives errors when run. Here is a very simple code sample to reproduce, using these two functions:
Which gives the following output
As you can see, the reverse mode works fine but the forward mode gives an error when trying to make the jvp.
Do you have any ideas on how to construct a forward version of the standard
jacobian
function?Thanks for your help