rsokl / MyGrad

Drop-in autodiff for NumPy.
https://rsokl.github.io/MyGrad/
MIT License
195 stars 21 forks source link

Revise constant=False semantics #356

Closed rsokl closed 3 years ago

rsokl commented 3 years ago

Previously, specifying constant=False in a mygrad function did not actually mean that the function would necessarily produce a non-constant tensor. Rather, it simply meant that the output would not be forced to be a constant – whether or not the result was a constant depended on the inputs (i.e. a function whose inputs were all constants would thus produce a constant).

This was a very bad design decision! Now, specifying constant=False guarantees that the output of a function is a non-constant (meaning that it facilitates backpropagation through a computational graph).

That being said, we usually do want constant information to propagate through functions. Thus constant=None is now the default value – its behavior matches that of constant=False from MyGrad 1.X – for all functions that accept the argument.

It is also now standard to require that this argument be a keyword-only argument.

>>> t1 = mg.tensor(1., constant=True)
>>> t2 = mg.tensor(1., constant=True)

# old behavior
>>> out = mg.add(t1, t2, constant=False)
>>> out.constant        
True    

# new behavior
>>> out = mg.add(t1, t2, constant=False)
>>> out.constant        
False

>>> out = mg.add(t1, t2, constant=None)
>>> out.constant        
True