Code Monkey home page Code Monkey logo

Comments (4)

MikeInnes avatar MikeInnes commented on July 22, 2024 1

You can fix this by writing something like sum(x.^2) instead. Ideally we should just catch sum(f, x) and implement it with broadcast.

from zygote.jl.

tpapp avatar tpapp commented on July 22, 2024

I get the same problem on Julia v1.0 (stable release), Zygote#master.

Information on whether this is a bug or just incorrect usage would help a lot.

from zygote.jl.

DilumAluthge avatar DilumAluthge commented on July 22, 2024

I can confirm that I get the same bug on Julia version 1.0.0, Zygote#master:

julia> using Zygote
[ Info: Precompiling Zygote [e88e6eb3-aa80-5325-afca-941959d7151f]
[ Info: Precompiling IRTools [7869d1d1-7146-5819-86e3-90919afe41df]

julia> f(x) = -sum(abs2, x)
f (generic function with 1 method)

julia> a = ones(10)
10-element Array{Float64,1}:
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0

julia> f(a)
-10.0

julia> f'(a)
ERROR: BoundsError: attempt to access 4-element Array{Int32,1} at index [5]
Stacktrace:
 [1] getindex at ./array.jl:731 [inlined]
 [2] reverse_ir(::Zygote.Primal) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/reverse.jl:296
 [3] Zygote.Adjoint(::Zygote.Primal) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/reverse.jl:378
 [4] #Adjoint#65 at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/reverse.jl:382 [inlined]
 [5] (::getfield(Core, Symbol("#kw#Type")))(::NamedTuple{(:varargs,),Tuple{Nothing}}, ::Type{Zygote.Adjoint}, ::Core.Compiler.IRCode) at ./none:0
 [6] _lookup_grad(::Type) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/emit.jl:121
 [7] #s18#630 at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface2.jl:17 [inlined]
 [8] #s18#630(::Any, ::Any, ::Any) at ./none:0
 [9] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at ./boot.jl:506
 [10] f at ./REPL[2]:1 [inlined]
 [11] (::Zygote.J{Tuple{typeof(f),Array{Float64,1}},Tuple{typeof(f),Array{Float64,1},getfield(Zygote, Symbol("##701#back2#402")){getfield(Zygote, Symbol("##400#401"))},Zygote.J{Tuple{typeof(sum),typeof(abs2),Array{Float64,1}},Tuple{typeof(sum)}}}})(::Int64) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface2.jl:0
 [12] #66 at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface.jl:28 [inlined]
 [13] gradient at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface.jl:34 [inlined]
 [14] derivative at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface.jl:37 [inlined]
 [15] (::getfield(Zygote, Symbol("##68#69")){typeof(f)})(::Array{Float64,1}) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface.jl:39
 [16] top-level scope at none:0

julia> Zygote.gradient(f, a)
ERROR: BoundsError: attempt to access 4-element Array{Int32,1} at index [5]
Stacktrace:
 [1] getindex at ./array.jl:731 [inlined]
 [2] reverse_ir(::Zygote.Primal) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/reverse.jl:296
 [3] Zygote.Adjoint(::Zygote.Primal) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/reverse.jl:378
 [4] #Adjoint#65 at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/reverse.jl:382 [inlined]
 [5] (::getfield(Core, Symbol("#kw#Type")))(::NamedTuple{(:varargs,),Tuple{Nothing}}, ::Type{Zygote.Adjoint}, ::Core.Compiler.IRCode) at ./none:0
 [6] _lookup_grad(::Type) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/emit.jl:121
 [7] #s18#630 at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface2.jl:17 [inlined]
 [8] #s18#630(::Any, ::Any, ::Any) at ./none:0
 [9] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at ./boot.jl:506
 [10] f at ./REPL[2]:1 [inlined]
 [11] (::Zygote.J{Tuple{typeof(f),Array{Float64,1}},Tuple{typeof(f),Array{Float64,1},getfield(Zygote, Symbol("##701#back2#402")){getfield(Zygote, Symbol("##400#401"))},Zygote.J{Tuple{typeof(sum),typeof(abs2),Array{Float64,1}},Tuple{typeof(sum)}}}})(::Int64) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface2.jl:0
 [12] (::getfield(Zygote, Symbol("##66#67")){Zygote.J{Tuple{typeof(f),Array{Float64,1}},Tuple{typeof(f),Array{Float64,1},getfield(Zygote, Symbol("##701#back2#402")){getfield(Zygote, Symbol("##400#401"))},Zygote.J{Tuple{typeof(sum),typeof(abs2),Array{Float64,1}},Tuple{typeof(sum)}}}}})(::Int64) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface.jl:28
 [13] gradient(::Function, ::Array{Float64,1}) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface.jl:34
 [14] top-level scope at none:0

julia> Zygote.derivative(f, a)
ERROR: BoundsError: attempt to access 4-element Array{Int32,1} at index [5]
Stacktrace:
 [1] getindex at ./array.jl:731 [inlined]
 [2] reverse_ir(::Zygote.Primal) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/reverse.jl:296
 [3] Zygote.Adjoint(::Zygote.Primal) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/reverse.jl:378
 [4] #Adjoint#65 at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/reverse.jl:382 [inlined]
 [5] (::getfield(Core, Symbol("#kw#Type")))(::NamedTuple{(:varargs,),Tuple{Nothing}}, ::Type{Zygote.Adjoint}, ::Core.Compiler.IRCode) at ./none:0
 [6] _lookup_grad(::Type) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/emit.jl:121
 [7] #s18#630 at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface2.jl:17 [inlined]
 [8] #s18#630(::Any, ::Any, ::Any) at ./none:0
 [9] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at ./boot.jl:506
 [10] f at ./REPL[2]:1 [inlined]
 [11] (::Zygote.J{Tuple{typeof(f),Array{Float64,1}},Tuple{typeof(f),Array{Float64,1},getfield(Zygote, Symbol("##701#back2#402")){getfield(Zygote, Symbol("##400#401"))},Zygote.J{Tuple{typeof(sum),typeof(abs2),Array{Float64,1}},Tuple{typeof(sum)}}}})(::Int64) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface2.jl:0
 [12] (::getfield(Zygote, Symbol("##66#67")){Zygote.J{Tuple{typeof(f),Array{Float64,1}},Tuple{typeof(f),Array{Float64,1},getfield(Zygote, Symbol("##701#back2#402")){getfield(Zygote, Symbol("##400#401"))},Zygote.J{Tuple{typeof(sum),typeof(abs2),Array{Float64,1}},Tuple{typeof(sum)}}}}})(::Int64) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface.jl:28
 [13] gradient(::Function, ::Array{Float64,1}) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface.jl:34
 [14] derivative(::Function, ::Array{Float64,1}) at /Users/dilum/.julia/packages/Zygote/tOamD/src/compiler/interface.jl:37
 [15] top-level scope at none:0

from zygote.jl.

tpapp avatar tpapp commented on July 22, 2024

Thanks for confirming this. Is this something that is supposed to work though? I am not sure I am using the package right.

from zygote.jl.

Related Issues (20)

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.