sbuercklin / unitfulchainrules.jl Goto Github PK
View Code? Open in Web Editor NEWChainRules.jl integration for Unitful.jl
License: MIT License
ChainRules.jl integration for Unitful.jl
License: MIT License
Sometimes one ends up using Quantities with fractional powers, but they don't currently work with UnitfulChainRules.jl
:
julia> Zygote.pullback(x -> x^(1//3), 3.0u"W^3")[2](1)
ERROR: MethodError: no method matching log(::Quantity{ComplexF64, ๐ ^6 ๐ ^3 ๐ ^-9, Unitful.FreeUnits{(W^3,), ๐ ^6 ๐ ^3 ๐^-9, nothing}})
Closest candidates are:
log(::T, ::T) where T<:Number at C:\Program Files\Julia-1.7.0\share\julia\base\math.jl:315
log(::Number, ::Number) at C:\Program Files\Julia-1.7.0\share\julia\base\math.jl:358
log(::RoundingMode, ::ForwardDiff.Dual{Ty}) where Ty at C:\Users\bks1\.julia\packages\ForwardDiff\wAaVJ\src\dual.jl:145
...
Stacktrace:
[1] _pow_grad_p(x::Quantity{Float64, ๐^6 ๐^3 ๐^-9, Unitful.FreeUnits{(W^3,), ๐^6 ๐^3 ๐^-9, nothing}} , p::Rational{Int64}, y::Quantity{Float64, ๐^2 ๐ ๐^-3, Unitful.FreeUnits{(W,), ๐^2 ๐ ๐^-3, nothing}} )
@ ChainRules C:\Users\bks1\.julia\packages\ChainRules\EyLkg\src\rulesets\Base\fastmath_able.jl:320
[2] (::ChainRules.var"#1244#1277"{Int64, Quantity{Float64, ๐^6 ๐^3 ๐^-9, Unitful.FreeUnits{(W^3,), ๐ ^6 ๐^3 ๐^-9,
nothing}}, Rational{Int64}, ProjectTo{Real, NamedTuple{(), Tuple{}}}, Quantity{Float64, ๐^2 ๐ ๐^ -3, Unitful.FreeUnits{(W,), ๐^2 ๐ ๐^-3, nothing}}}) ()
@ ChainRules C:\Users\bks1\.julia\packages\ChainRules\EyLkg\src\rulesets\Base\fastmath_able.jl:196
[3] unthunk
@ C:\Users\bks1\.julia\packages\ChainRulesCore\ctmSK\src\tangent_types\thunks.jl:199 [inlined]
[4] wrap_chainrules_output
@ C:\Users\bks1\.julia\packages\Zygote\IoW2g\src\compiler\chainrules.jl:104 [inlined]
[5] map
@ .\tuple.jl:223 [inlined]
[6] wrap_chainrules_output
@ C:\Users\bks1\.julia\packages\Zygote\IoW2g\src\compiler\chainrules.jl:105 [inlined]
[7] ZBack
@ C:\Users\bks1\.julia\packages\Zygote\IoW2g\src\compiler\chainrules.jl:205 [inlined]
[8] Pullback
@ .\REPL[51]:1 [inlined]
[9] (::typeof(โ(#111)))(ฮ::Int64)
@ Zygote C:\Users\bks1\.julia\packages\Zygote\IoW2g\src\compiler\interface2.jl:0
[10] (::Zygote.var"#60#61"{typeof(โ(#111))})(ฮ::Int64)
@ Zygote C:\Users\bks1\.julia\packages\Zygote\IoW2g\src\compiler\interface.jl:41
[11] top-level scope
@ REPL[51]:1
abs
's rrule
returns a deconstructed Quantity
as a NamedTuple
, not a Quantity
. We should add a manual rule to fix this and upstream to ChainRules.jl
if it generalizes the existing behavior
julia> Zygote.gradient(abs, 1u"W")
((val = 1.0 W,),)
This issue is used to trigger TagBot; feel free to unsubscribe.
If you haven't already, you should update your TagBot.yml
to include issue comment triggers.
Please see this post on Discourse for instructions and more details.
If you'd like for me to do this for you, comment TagBot fix
on this issue.
I'll open a PR within a few hours, please be patient!
Unitful.Quantity
s are not compatible with most array-based math rules in ChainRules.jl
right now. Relevant issues discussing why this is are here and here.
We could solve this problem by duplicating the array rules from ChainRules.jl
for properly typed Quanitity
s. A better solution would be to solve the aforementioned issues upstream in such a way that we can adopt the rules automatically
It'd be useful to have really basic *(::AbstractArray, ::Unitful.Quantity)
and \(...)
methods rules. These don't take much to add, unlike the general case of #5, so I think it's reasonable to add them
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)
Currently only supports rrule
s, add frule
support
Is it possible to handle these cases with @scalar_rule
instead of manually writing both the frule
s and rrule
s?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.