Code Monkey home page Code Monkey logo

unitfulchainrules.jl's People

Contributors

sbuercklin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

unitfulchainrules.jl's Issues

Add `frule`s

Currently only supports rrules, add frule support

`abs` rrule for Unitful.Quantity

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,),)

Scalar-Array Arithmetic

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

TagBot trigger issue

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!

General Array Rules

Unitful.Quantitys 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 Quanititys. A better solution would be to solve the aforementioned issues upstream in such a way that we can adopt the rules automatically

Simplify Units In Pullback

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)

Fractional powers not supported

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

Use @scalar_rule

Is it possible to handle these cases with @scalar_rule instead of manually writing both the frules and rrules?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.