Hi, I can’t get Zygote.jl to work with the adjoint of NamedVectors:
using Zygote
using NamedArrays
foo(a, b, c) = a' * b * c
function bar()
a = NamedArray([1.0, 2.0], ["x", "y"])
b = NamedArray([1.0, 2.0], ["x", "y"])
foo(a, b, 1.0)
Zygote.gradient(x -> foo(a, b, x[1]), [1.0])
end
bar()
julia> show(err)
1-element ExceptionStack:
LoadError: MethodError: no method matching NamedArray(::Vector{Float64}, ::Tuple{OrderedCollections.OrderedDict{String, Int64}, OrderedCollections.OrderedDict{String, Int64}}, ::Tuple{Symbol, String})
Closest candidates are:
NamedArray(::AbstractVector{T}, ::AbstractVector{VT}, ::Any) where {T, VT}
@ NamedArrays ~/.julia/packages/NamedArrays/73Upr/src/constructors.jl:121
NamedArray(::AbstractArray{T, N}, ::Tuple{}, ::Tuple{Vararg{Any, N}}) where {T, N}
@ NamedArrays ~/.julia/packages/NamedArrays/73Upr/src/constructors.jl:27
NamedArray(::AbstractArray{T, N}, ::Tuple{Vararg{OrderedCollections.OrderedDict, N}}, ::Tuple{Vararg{T, N}} where T) where {T, N}
@ NamedArrays ~/.julia/packages/NamedArrays/73Upr/src/constructors.jl:37
...
Stacktrace:
[1] adjoint(a::NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}})
@ NamedArrays ~/.julia/packages/NamedArrays/73Upr/src/rearrange.jl:15
[2] (::ChainRules.var"#1468#1471"{Float64, NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}}}}})()
@ ChainRules ~/.julia/packages/ChainRules/GkbFV/src/rulesets/Base/arraymath.jl:37
[3] unthunk
@ ~/.julia/packages/ChainRulesCore/zgT0R/src/tangent_types/thunks.jl:204 [inlined]
[4] wrap_chainrules_output
@ ~/.julia/packages/Zygote/nsBv0/src/compiler/chainrules.jl:110 [inlined]
[5] map
@ ./tuple.jl:293 [inlined]
[6] wrap_chainrules_output
@ ~/.julia/packages/Zygote/nsBv0/src/compiler/chainrules.jl:111 [inlined]
[7] ZBack
@ ~/.julia/packages/Zygote/nsBv0/src/compiler/chainrules.jl:211 [inlined]
[8] mat_vec_scalar
@ ~/.julia/juliaup/julia-1.10.0+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/matmul.jl:1178 [inlined]
[9] (::Zygote.Pullback{Tuple{typeof(LinearAlgebra.mat_vec_scalar), NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64}, Tuple{Zygote.ZBack{ChainRules.var"#times_pullback#1469"{NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}}}}, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}}}}, Zygote.ZBack{ChainRules.var"#times_pullback#1500"{NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}}}}, ProjectTo{Float64, @NamedTuple{}}}}}})(Δ::Float64)
@ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
[10] *
@ ~/.julia/juliaup/julia-1.10.0+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/matmul.jl:1149 [inlined]
[11] (::Zygote.Pullback{Tuple{typeof(*), NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64}, Tuple{Zygote.Pullback{Tuple{typeof(LinearAlgebra.mat_vec_scalar), NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64}, Tuple{Zygote.ZBack{ChainRules.var"#times_pullback#1469"{NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}}}}, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}}}}, Zygote.ZBack{ChainRules.var"#times_pullback#1500"{NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}}}}, ProjectTo{Float64, @NamedTuple{}}}}}}}})(Δ::Float64)
@ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
[12] foo
@ ~/foo.jl:57 [inlined]
[13] (::Zygote.Pullback{Tuple{typeof(foo), NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64}, Tuple{Zygote.Pullback{Tuple{typeof(*), NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64}, Tuple{Zygote.Pullback{Tuple{typeof(LinearAlgebra.mat_vec_scalar), NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64}, Tuple{Zygote.ZBack{ChainRules.var"#times_pullback#1469"{NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}}}}, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}}}}, Zygote.ZBack{ChainRules.var"#times_pullback#1500"{NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}}}}, ProjectTo{Float64, @NamedTuple{}}}}}}}}, Zygote.ZBack{typeof(ChainRules._adjoint_vec_pullback)}}})(Δ::Float64)
@ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
[14] #204
@ ~/foo.jl:63 [inlined]
[15] (::Zygote.Pullback{Tuple{var"#204#205"{NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}}, Vector{Float64}}, Tuple{Zygote.Pullback{Tuple{typeof(Zygote.literal_getindex), Vector{Float64}, Val{1}}, Tuple{Zygote.ZBack{ChainRules.var"#getindex_pullback#1592"{Vector{Float64}, Tuple{Int64}, Tuple{NoTangent}}}}}, Zygote.var"#2180#back#303"{Zygote.var"#back#302"{:a, Zygote.Context{false}, var"#204#205"{NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}}}, Zygote.var"#2180#back#303"{Zygote.var"#back#302"{:b, Zygote.Context{false}, var"#204#205"{NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}}}, Zygote.Pullback{Tuple{typeof(foo), NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64}, Tuple{Zygote.Pullback{Tuple{typeof(*), NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64}, Tuple{Zygote.Pullback{Tuple{typeof(LinearAlgebra.mat_vec_scalar), NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64}, Tuple{Zygote.ZBack{ChainRules.var"#times_pullback#1469"{NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}}}}, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}}}}, Zygote.ZBack{ChainRules.var"#times_pullback#1500"{NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}}}}, ProjectTo{Float64, @NamedTuple{}}}}}}}}, Zygote.ZBack{typeof(ChainRules._adjoint_vec_pullback)}}}}})(Δ::Float64)
@ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
[16] (::Zygote.var"#75#76"{Zygote.Pullback{Tuple{var"#204#205"{NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}}, Vector{Float64}}, Tuple{Zygote.Pullback{Tuple{typeof(Zygote.literal_getindex), Vector{Float64}, Val{1}}, Tuple{Zygote.ZBack{ChainRules.var"#getindex_pullback#1592"{Vector{Float64}, Tuple{Int64}, Tuple{NoTangent}}}}}, Zygote.var"#2180#back#303"{Zygote.var"#back#302"{:a, Zygote.Context{false}, var"#204#205"{NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}}}, Zygote.var"#2180#back#303"{Zygote.var"#back#302"{:b, Zygote.Context{false}, var"#204#205"{NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}}}, Zygote.Pullback{Tuple{typeof(foo), NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64}, Tuple{Zygote.Pullback{Tuple{typeof(*), NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64}, Tuple{Zygote.Pullback{Tuple{typeof(LinearAlgebra.mat_vec_scalar), NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64}, Tuple{Zygote.ZBack{ChainRules.var"#times_pullback#1469"{NamedMatrix{Float64, LinearAlgebra.Adjoint{Float64, Vector{Float64}}, Tuple{OrderedDict{String, Int64}, OrderedDict{String, Int64}}}, NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}}}}, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}}}}}, Zygote.ZBack{ChainRules.var"#times_pullback#1500"{NamedVector{Float64, Vector{Float64}, Tuple{OrderedDict{String, Int64}}}, Float64, ProjectTo{AbstractArray, @NamedTuple{element::ProjectTo{Float64, @NamedTuple{}}, axes::Tuple{Base.OneTo{Int64}}}}, ProjectTo{Float64, @NamedTuple{}}}}}}}}, Zygote.ZBack{typeof(ChainRules._adjoint_vec_pullback)}}}}}})(Δ::Float64)
@ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface.jl:91
[17] gradient(f::Function, args::Vector{Float64})
@ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface.jl:148
[18] bar()
@ Main ~/foo.jl:63
[19] top-level scope
@ ~/foo.jl:66
[20] eval
@ ./boot.jl:385 [inlined]
[21] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:2070
[22] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::@Kwargs{})
@ Base ./essentials.jl:887
[23] invokelatest(::Any, ::Any, ::Vararg{Any})
@ Base ./essentials.jl:884
[24] inlineeval(m::Module, code::String, code_line::Int64, code_column::Int64, file::String; softscope::Bool)
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:271
[25] (::VSCodeServer.var"#69#74"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:181
[26] withpath(f::VSCodeServer.var"#69#74"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams}, path::String)
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/repl.jl:276
[27] (::VSCodeServer.var"#68#73"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:179
[28] hideprompt(f::VSCodeServer.var"#68#73"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/repl.jl:38
[29] (::VSCodeServer.var"#67#72"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:150
[30] with_logstate(f::Function, logstate::Any)
@ Base.CoreLogging ./logging.jl:515
[31] with_logger
@ ./logging.jl:627 [inlined]
[32] (::VSCodeServer.var"#66#71"{VSCodeServer.ReplRunCodeRequestParams})()
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:263
[33] #invokelatest#2
@ Base ./essentials.jl:887 [inlined]
[34] invokelatest(::Any)
@ Base ./essentials.jl:884
[35] (::VSCodeServer.var"#64#65")()
@ VSCodeServer ~/.vscode/extensions/julialang.language-julia-1.79.2/scripts/packages/VSCodeServer/src/eval.jl:34
in expression starting at /Users/johannes/foo.jl:66
My code heavily depends on the NamedArray
structure and therefore I would prefer not to use ordinary arrays.