pymc-devs / pymc-devs/pytensor

Allow jacobian and hessian of arbitrary dimension expressions

Open
#1,225 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement gradients
Dominant language
Python
Stars
644
Forks
208
Avg merge
2d 14h
Merged PRs (30d)
16

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the pytensor.gradient.jacobian entry point and reproduce the multidimensional example from the issue. Determine how arbitrary-dimensional expressions should be handled for both jacobian and hessian, including the hessian input combinations; done means the documented cases work without requiring users to ravel and reshape manually.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.