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

Allow jacobian and hessian of arbitrary dimension expressions #1225

Open
ricardoV94 opened this issue Feb 19, 2025 · 0 comments
Open

Allow jacobian and hessian of arbitrary dimension expressions #1225

ricardoV94 opened this issue Feb 19, 2025 · 0 comments
Labels
enhancement New feature or request gradients

Comments

@ricardoV94
Copy link
Member

ricardoV94 commented Feb 19, 2025

Description

from pytensor.gradient import jacobian
import pytensor.tensor as pt

x = pt.vector("x", shape=(3,))
y = pt.outer(x[1:], x[2:])
assert y.type.shape == (2, 1)
try:
    jacobian(y, x)
except Exception as exc:
    print(exc)  # jacobian expects a 1 dimensional variable as `expression`. If not use flatten to make it a vector

jac_y = jacobian(y.ravel(), x).reshape((*y.shape, *x.shape))
assert jac_y.type.shape == (2, 1, 3)

I don't see why we can't do the ravel -> reshape for the users? JAX accepts non-vector jacobian just fine.

The hessian is trickier as it requires also the combinations of the inputs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request gradients
Projects
None yet
Development

No branches or pull requests

1 participant