pymc-devs / pytensor

PyTensor allows you to define, optimize, and efficiently evaluate mathematical expressions involving multi-dimensional arrays.
https://pytensor.readthedocs.io
Other
341 stars 101 forks source link

Add support for multiple output functions to `gradient.verify_grad` #644

Open jessegrabowski opened 8 months ago

jessegrabowski commented 8 months ago

Description

This is marked as a to-do in the code, along with a suggestion of how to handle it.

The whole function could probably be cleaned up while we're at it. It's not easy to read because it's doing so much all in a single function. It would be better to split up the construction of the tests from their execution. This would make extending to multiple outputs very straightforward -- just add one more outer loop.

I don't know how we feel about another dependency, but farming out the actual numerical gradients to a specialized package like numdifftools might be good too.

ricardoV94 commented 7 months ago

Multiple outputs can be tested with this utility by using a callable instead of a single Op, as illustrated here: https://github.com/pymc-devs/pytensor/pull/614#discussion_r1489278068

I think it's reasonable to fail, because Ops may have odd outputs that can't be safely combined by default (and OTOH testing each output separately may mask bugs when an Op has to mix gradients wrt to multiple outputs). Perhaps the error message should just nudge users to the possibility of testing Ops with a callable as in that example?

This is also useful for only testing gradients wrt to some inputs. For instance in the new pymc-example: https://github.com/pymc-devs/pymc-examples/pull/631 we only implement grads wrt to some inputs and the utility would fail if you passed the Op and a pt for all inputs because it would test them all.