Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reshape jacobian of real input and complex output to ensure complex output #594

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions autograd/differential_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,22 @@ def jacobian(fun, x):
(out1, out2, ...) then the Jacobian has shape (out1, out2, ..., in1, in2, ...).
"""
vjp, ans = _make_vjp(fun, x)
x_vspace = vspace(x)
ans_vspace = vspace(ans)
jacobian_shape = ans_vspace.shape + vspace(x).shape
jacobian_shape = ans_vspace.shape + x_vspace.shape
grads = map(vjp, ans_vspace.standard_basis())
return np.reshape(np.stack(grads), jacobian_shape)
grads = np.stack(grads)

if (ans_vspace.iscomplex) and (not x_vspace.iscomplex):
# Reshape with doubled size of flattened grad (2*ans.size,x.size) due to complex fun and real x
jacobian_reshape = ans_vspace.shape + (2,) + x_vspace.shape
jacobian_moveaxis = (ans_vspace.ndim,0)
grads = np.moveaxis(np.reshape(grads, jacobian_reshape), *jacobian_moveaxis)

# Return complex components of jacobian
return grads[0] - 1j*grads[1]
else:
return np.reshape(grads, jacobian_shape)

@unary_to_nary
def holomorphic_grad(fun, x):
Expand Down