Skip to content

Should to_vec(::Integer) return an empty vector #188

Open

Description

@mzgubic

Integers can't be perturbed, so their to_vec should be empty. Originally suggested in #187

function to_vec(x::Integer)
     Integer_from_vec(v) = x
     return Bool[], Integer_from_vec
end

Would also fix (a part of) chrisbrahms/Hankel.jl#27

Did not test yet whether it messes up any existing ChainRules tests.

Activity

  1. willtebbutt commented on Aug 4, 2021

    @willtebbutt
    Member

    This would make sense to me.

  2. oxinabox commented on Oct 1, 2021

    @oxinabox
    Member

    The practical problem with this, is that sometimes people want to test special case methods that take integers.
    Which is just all round hard, since when finite diferencing they don't get integers.

    But they might still want to test it against custom rules that do.
    So I wonder if we really need to handle this better in CRTU or somewhere in FiniteDifferences.

    Right now we often (always?) error for it

  3. willtebbutt commented on Oct 1, 2021

    @willtebbutt
    Member

    Could you provide an example @oxinabox ? I'm not really clear what sort of thing you're imagining.

  4. oxinabox commented on Oct 1, 2021

    @oxinabox
    Member

    For example

    function rrule(::typeof(sinpi), x)
        sinpi_pullback(dy) = -pi*cospi(x)
        foo(x), sinpi_pullback
    end
    
    function rrule(::typeof(sinpi), x::Integer)
        sinpi_pullback(dy) = Zero()
        foo(x), sinpi_pullback
    end
    
  5. willtebbutt commented on Oct 1, 2021

    @willtebbutt
    Member

    And what we're proposing to do here would solve this problem, yes?

  6. oxinabox commented on Oct 1, 2021

    @oxinabox
    Member

    Would it? Cool!

  7. mcabbott commented on Oct 1, 2021

    @mcabbott
    Member

    Isn't this example the thing which already works? Integer taken to mean non-differentiable:

    julia> mysinpi(x) = sinpi(x)
    mysinpi (generic function with 1 method)
    
    julia> function ChainRulesCore.rrule(::typeof(mysinpi), x)
               sinpi_pullback(dy) = NoTangent(), pi*cospi(x)*dy
               sinpi(x), sinpi_pullback
           end
    
    julia> function ChainRulesCore.rrule(::typeof(mysinpi), x::Integer)
               sinpi_pullback(dy) = NoTangent(), NoTangent()
               sinpi(x), sinpi_pullback
           end
    
    julia> test_rrule(mysinpi, 1.23)
    Test Summary:                  | Pass  Total
    test_rrule: mysinpi on Float64 |    7      7
    Test.DefaultTestSet("test_rrule: mysinpi on Float64", Any[], 7, false, false)
    
    julia> test_rrule(mysinpi, 1)
    Test Summary:                | Pass  Total
    test_rrule: mysinpi on Int64 |    6      6
    Test.DefaultTestSet("test_rrule: mysinpi on Int64", Any[], 6, false, false)
    
  8. willtebbutt commented on Oct 1, 2021

    @willtebbutt
    Member

    Oh, yeah, this particular example is. I think stuff would need to be nested in order to break it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions