probcomp / Venturecxx

Primary implementation of the Venture probabilistic programming system
http://probcomp.csail.mit.edu/venture/
GNU General Public License v3.0
28 stars 6 forks source link

What gradient should the `**` operator produce for the exponent when the base is negative? #661

Closed axch closed 7 years ago

axch commented 7 years ago

The "correct" answer is that exponentiation with negative base and real-valued exponent is undefined, so crash. However, a relatively common use case of ** is to spell polynomials, as with x**3. In this case, the gradient of ** should clearly be [3x**2, 0], even for negative x, as the exponent is intended to be a non-varying integer.

If we had first-class integers (#436, #570) in Venture I would be comfortable defining the gradient of ** to be generic in the argument type, and produce the latter answer if the exponent came in as an integer. That policy would also suggest doing the same thing for other type-generic numerical operations, and/or teaching integers to accept "gradient" steps but not move.

riastradh-probcomp commented 7 years ago

In commit 09876c9f75f512b8c2887d0ba5258414f09b81d9 I replaced math.log by np.log in the code to compute the gradient of pow, so that for the partial wrt the exponent it gives NaN instead of raising an exception when the base is a negative real. I have not confirmed that this change doesn't have bad consequences, but it does seem to get around the immediate problem I had with gradient methods, which now give a sensible result.

axch commented 7 years ago

I think actually solves this.