Comments (6)
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.
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.
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.
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.
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.
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)
- `repeat(X; outer, inner)` triggers scalar indexing error with CUDA HOT 1
- Missing support for muladd in case of brodcasting with a complex argument HOT 1
- `nothing` in output of a `pullback` HOT 2
- Assignment to multiple arrays is not differentiable on GPU since Zygote.jl 0.6.67 HOT 5
- Spurious "Output is complex, so the gradient is not defined" error HOT 2
- NaN in gradient of abs() on complex 0 HOT 1
- Pullback on mean() gives illegal memory access code 700 HOT 31
- test
- Type unstable gradients (@code_warntype) HOT 1
- Type unstable gradients HOT 1
- Zygote gradients different from ForwardDiff/ReverseDiff on Julia 1.10-rc2 HOT 3
- try/catch is not supported when attempting to use `remake` with Zygote HOT 1
- gradient of SVD not working for complex input HOT 1
- `Zygote` doesn't properly work with `Metal.jl` and half precision. HOT 4
- `gradient` broken for `(*)(::Diagonal{Real}, ::Matrix{Complex}, ::Diagonal{Real})` when updating Julia 1.8 -> 1.9 HOT 6
- Method ambiguities reported by Aqua
- slow/high allocation gradient with mapreduce and iterators HOT 11
- error in summation of product iterator HOT 2
- `sort(x; rev=true)` is not supported HOT 1
- Incorrect gradients for `plan_rfft(x) * x` HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zygote.jl.