Giter Club home page Giter Club logo

Comments (6)

MikeInnes avatar MikeInnes commented on May 22, 2024

Problem seems to be this odd definition, which is called by 0.5x'. Until we have a better fix, 0.5(x'*H*x) will work.

from zygote.jl.

jekbradbury avatar jekbradbury commented on May 22, 2024

Those two definitions predate the new broadcast interface and feel like they might be inconsistent with its spirit (although I can’t quite see what the right replacement is). I’m going to be slightly obnoxious and tag @Sacha0 and @mbauman, because they’ll probably know pretty quickly if the definitions are wrong.

from zygote.jl.

Sacha0 avatar Sacha0 commented on May 22, 2024

Those two definitions predate the new broadcast interface and feel like they might be inconsistent with its spirit (although I can’t quite see what the right replacement is).

You have the right of it I think :). While semantically correct, those definitions predate the broadcast interface overhaul by a few months, and likely should be expressed differently now. Best!

from zygote.jl.

baggepinnen avatar baggepinnen commented on May 22, 2024
f(x)   = 0.5*(x'*(H*x))

does not fix the issue on latest master

julia> f(x)   = 0.5*(x'*(H*x))
f (generic function with 1 method)

julia> fp  = Zygote.gradient(f,x)
ERROR: MethodError: no method matching exprtype(::Core.Compiler.IRCode, ::String)
Closest candidates are:
  exprtype(::Core.Compiler.IRCode, ::Expr) at /local/home/fredrikb/.julia/dev/Zygote/src/tools/ir.jl:71
  exprtype(::Core.Compiler.IRCode, ::QuoteNode) at /local/home/fredrikb/.julia/dev/Zygote/src/tools/ir.jl:68
  exprtype(::Core.Compiler.IRCode, ::GlobalRef) at /local/home/fredrikb/.julia/dev/Zygote/src/tools/ir.jl:67
  ...
Stacktrace:
 [1] _broadcast_getindex_evalf at ./broadcast.jl:574 [inlined]
 [2] _broadcast_getindex at ./broadcast.jl:547 [inlined]
 [3] getindex at ./broadcast.jl:507 [inlined]
 [4] copyto_nonleaf!(::Array{DataType,1}, ::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(Zygote.exprtype),Tuple{Base.RefValue{Core.Compiler.IRCode},Base.Broadcast.Extruded{Array{Any,1},Tuple{Bool},Tuple{Int64}}}}, ::Base.OneTo{Int64}, ::Int64, ::Int64) at ./broadcast.jl:899
 [5] copy(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(Zygote.exprtype),Tuple{Base.RefValue{Core.Compiler.IRCode},Array{Any,1}}}) at ./broadcast.jl:762
 [6] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(Zygote.exprtype),Tuple{Base.RefValue{Core.Compiler.IRCode},Array{Any,1}}}) at ./broadcast.jl:724
 [7] record!(::Core.Compiler.IRCode) at /local/home/fredrikb/.julia/dev/Zygote/src/compiler/reverse.jl:132
 [8] #Primal#46(::Int64, ::Type, ::Core.Compiler.IRCode) at /local/home/fredrikb/.julia/dev/Zygote/src/compiler/reverse.jl:177
 [9] Type at ./none:0 [inlined]
 [10] #Adjoint#72 at /local/home/fredrikb/.julia/dev/Zygote/src/compiler/reverse.jl:375 [inlined]
 [11] (::getfield(Core, Symbol("#kw#Type")))(::NamedTuple{(:varargs,),Tuple{Int64}}, ::Type{Zygote.Adjoint}, ::Core.Compiler.IRCode) at ./none:0
 [12] _lookup_grad(::Type) at /local/home/fredrikb/.julia/dev/Zygote/src/compiler/emit.jl:121
 [13] #s18#627 at /local/home/fredrikb/.julia/dev/Zygote/src/compiler/interface2.jl:19 [inlined]
 [14] #s18#627(::Any, ::Any, ::Any) at ./none:0
 [15] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at ./boot.jl:506
 [16] f at ./REPL[9]:1 [inlined]
 [17] (::Zygote.J{Tuple{typeof(f),Array{Float64,1}},Tuple{typeof(f),Array{Float64,1},getfield(Zygote, Symbol("##1010#back2#569")){getfield(Zygote, Symbol("##567#568")){Transpose{Float64,Array{Float64,1}},Array{Float64,1}}},getfield(Zygote, Symbol("##1010#back2#569")){getfield(Zygote, Symbol("##567#568")){Array{Float64,2},Array{Float64,1}}},getfield(Zygote, Symbol("##1016#back2#572")){getfield(Zygote, Symbol("##570#571"))},Zygote.J{Tuple{typeof(hvcat),Tuple{Int64,Int64,Int64},Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64},Tuple{typeof(hvcat)}},getfield(Zygote, Symbol("##143#back2#115")){typeof(identity)}}})(::Int64) at /local/home/fredrikb/.julia/dev/Zygote/src/compiler/interface2.jl:0
 [18] (::getfield(Zygote, Symbol("##73#74")){Zygote.J{Tuple{typeof(f),Array{Float64,1}},Tuple{typeof(f),Array{Float64,1},getfield(Zygote, Symbol("##1010#back2#569")){getfield(Zygote, Symbol("##567#568")){Transpose{Float64,Array{Float64,1}},Array{Float64,1}}},getfield(Zygote, Symbol("##1010#back2#569")){getfield(Zygote, Symbol("##567#568")){Array{Float64,2},Array{Float64,1}}},getfield(Zygote, Symbol("##1016#back2#572")){getfield(Zygote, Symbol("##570#571"))},Zygote.J{Tuple{typeof(hvcat),Tuple{Int64,Int64,Int64},Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64},Tuple{typeof(hvcat)}},getfield(Zygote, Symbol("##143#back2#115")){typeof(identity)}}}})(::Int64) at /local/home/fredrikb/.julia/dev/Zygote/src/compiler/interface.jl:28
 [19] gradient(::Function, ::Array{Float64,1}) at /local/home/fredrikb/.julia/dev/Zygote/src/compiler/interface.jl:34

from zygote.jl.

baggepinnen avatar baggepinnen commented on May 22, 2024

Code in my previous post now works on latest master. But the extended example below still produce the same error

using Zygote, LinearAlgebra
x        = randn(3)             # Input
v        = randn(3)             # Vector
H        = randn(3,3); H = H+H' # Hessian
f(x)     = 0.5*(x'*(H*x))

hvp    = H*v                  # True Hessian vector product
gg     = H*x                # True gradient
ggvp   = gg'v                 # True gradient vector product

@assert f'(x)  gg # Works on latest master
gvp(x) = f'(x)'v # Gradient vector product
@assert gvp(x)  ggvp # Works
Hvp(x) = gvp'(x) # This contains nested differentiation
@assert Hvp(x)  hvp # Error

MethodError: no method matching exprtype(::Core.Compiler.IRCode, ::String)
Closest candidates are:
  exprtype(::Core.Compiler.IRCode, !Matched::Expr) at /local/home/fredrikb/.julia/dev/Zygote/src/tools/ir.jl:54
  exprtype(::Core.Compiler.IRCode, !Matched::QuoteNode) at /local/home/fredrikb/.julia/dev/Zygote/src/tools/ir.jl:51
  exprtype(::Core.Compiler.IRCode, !Matched::GlobalRef) at /local/home/fredrikb/.julia/dev/Zygote/src/tools/ir.jl:50
  ...
_broadcast_getindex_evalf at broadcast.jl:574 [inlined]
_broadcast_getindex at broadcast.jl:547 [inlined]
getindex at broadcast.jl:507 [inlined]
copyto_nonleaf!(::Array{DataType,1}, ::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(Zygote.exprtype),Tuple{Base.RefValue{Core.Compiler.IRCode},Base.Broadcast.Extruded{Array{Any,1},Tuple{Bool},Tuple{Int64}}}}, ::Base.OneTo{Int64}, ::Int64, ::Int64) at broadcast.jl:899
copy(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Tuple{Base.OneTo{Int64}},typeof(Zygote.exprtype),Tuple{Base.RefValue{Core.Compiler.IRCode},Array{Any,1}}}) at broadcast.jl:762
materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(Zygote.exprtype),Tuple{Base.RefValue{Core.Compiler.IRCode},Array{Any,1}}}) at broadcast.jl:724
...

from zygote.jl.

MikeInnes avatar MikeInnes commented on May 22, 2024

Fixed the original issue:

julia> derivative(x -> sum(0.5x'), [1, 2, 3])
3-element Array{Float64,1}:
 0.5
 0.5
 0.5

Nested differentiation is still ropey, but we'll figure that out separately.

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.