SBuercklin / UnitfulChainRules.jl

ChainRules.jl integration for Unitful.jl
MIT License
13 stars 1 forks source link

Simplify Units In Pullback #7

Open SBuercklin opened 2 years ago

SBuercklin commented 2 years ago

It's possible to return gradients which are dimensionless, but still have units attached to them as shown below. It might be better to simplify units as we go to drop units that cancel.

Adding upreferred to the pullback after we project and introduce the units should probably fix this.

using Unitful, UnitfulChainRules, Zygote

f(x,y) = (x * u"W" + y * u"mW") / u"W"

f(3.0, 4.0)
# 3.004 kg m^2 s^-3 W^-1

# current behavior
gradient(f, 3.0, 4.0)
# (1.0, 1.0 mW W^-1)

# potential preferred behavior
upreferred.(gradient(f, 3.0, 4.0))
# (1.0, 0.001)