-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Errors for gradient and hessian of logabsdet of sparse matrix #719
Comments
Note that it returns a tuple, you need to keep just one element: julia> gradient(logabsdet, diagm(1:3))
ERROR: Output should be scalar; gradients are not defined for output (1.791759469228055, 1.0)
julia> gradient(x -> logabsdet(x)[1], diagm(1:3))
([1.0 0.0 0.0; 0.0 0.5 0.0; 0.0 0.0 0.3333333333333333],)
julia> gradient(x -> logabsdet(x)[1], spdiagm(1:3))
ERROR: The inverse of a sparse matrix can often be dense and can cause the computer to run out of memory. If you are sure you have enough memory, please either convert your matrix to a dense matrix, e.g. by calling `Matrix` or if `A` can be factorized, use `\` on the dense identity matrix, e.g. `A \ Matrix{eltype(A)}(I, size(A)...)` restrictions of `\` on sparse lhs applies. Altenatively, `A\b` is generally preferable to `inv(A)*b`
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:35
[2] inv(A::SparseMatrixCSC{Int64, Int64})
@ SparseArrays ~/.julia/dev/julia/usr/share/julia/stdlib/v1.10/SparseArrays/src/linalg.jl:1431
[3] (::Zygote.var"#833#836"{SparseMatrixCSC{Int64, Int64}})(Δ::Tuple{Float64, Nothing})
@ Zygote ~/.julia/packages/Zygote/HTsWj/src/lib/array.jl:379
...
[8] gradient(f::Function, args::SparseMatrixCSC{Int64, Int64})
@ Zygote ~/.julia/packages/Zygote/HTsWj/src/compiler/interface.jl:97 Here src/lib/array.jl:379 is Zygote's own rule, which takes precedence -- CR isn't involved. That could probably be deleted. The rule in ChainRules looks like it will have the same problem, but perhaps it can be fixed?
|
In theory, I think this line:
could be rewritten as: ∂x = (x \ g')' |
I was wrong about that line of code above,
In fact, the generic definition for |
The following error:
ERROR: The inverse of a sparse matrix can often be dense...
```julia ERROR: The inverse of a sparse matrix can often be dense and can cause the computer to run out of memory. If you are sure you have enough memory, please either convert your matrix to a dense matrix, e.g. by calling `Matrix` or if `A` can be factorized, use `\` on the dense identity matrix, e.g. `A \ Matrix{eltype(A)}(I, size(A)...)` restrictions of `\` on sparse lhs applies. Altenatively, `A\b` is generally preferable to `inv(A)*b` Stacktrace: [1] error(s::String) @ Base .\error.jl:35 [2] inv(A::SparseMatrixCSC{Float64, Int64}) @ SparseArrays C:\Users\sam.urmy\.julia\juliaup\julia-1.9.0+0.x64.w64.mingw32\share\julia\stdlib\v1.9\SparseArrays\src\linalg.jl:1448 [3] (::Zygote.var"#833#836"{SparseMatrixCSC{Float64, Int64}})(Δ::Tuple{Float64, Float64}) @ Zygote C:\Users\sam.urmy\.julia\packages\Zygote\HTsWj\src\lib\array.jl:379 [4] (::Zygote.var"#3123#back#837"{Zygote.var"#833#836"{SparseMatrixCSC{Float64, Int64}}})(Δ::Tuple{Float64, Float64}) @ Zygote C:\Users\sam.urmy\.julia\packages\ZygoteRules\OgCVT\src\adjoint.jl:71 [5] Pullback @ C:\Users\sam.urmy\.julia\juliaup\julia-1.9.0+0.x64.w64.mingw32\share\julia\stdlib\v1.9\LinearAlgebra\src\generic.jl:1685 [inlined] [6] (::Zygote.Pullback{Tuple{typeof(logdet), SparseMatrixCSC{Float64, Int64}}, Tuple{Zygote.var"#back#241"{Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 1, Zygote.Context{false}, Float64}}}, Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 1, Zygote.Context{false}, Float64}}, Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 2, Zygote.Context{false}, Int64}}, Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 1, Zygote.Context{false}, Float64}}, Zygote.var"#3123#back#837"{Zygote.var"#833#836"{SparseMatrixCSC{Float64, Int64}}}, Zygote.ZBack{ChainRules.var"#log_pullback#1324"{Float64, ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}, Zygote.ZBack{Zygote.var"#plus_pullback#341"{Tuple{Float64, Float64}}}, Zygote.var"#back#242"{Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 2, Zygote.Context{false}, Float64}}}}})(Δ::Float64) @ Zygote C:\Users\sam.urmy\.julia\packages\Zygote\HTsWj\src\compiler\interface2.jl:0 [7] Pullback @ .\Untitled-1:54 [inlined] [8] (::Zygote.Pullback{Tuple{typeof(f), Vector{Float64}, NamedTuple{(:x, :n), Tuple{Vector{Float64}, Int64}}}, Tuple{Zygote.Pullback{Tuple{typeof(spdiagm), Vector{Float64}}, Tuple{Zygote.Pullback{Tuple{typeof(SparseArrays._spdiagm), Nothing, Pair{Int64, Vector{Float64}}}, Any}, Zygote.Pullback{Tuple{Type{Pair}, Int64, Vector{Float64}}, Tuple{Zygote.ZBack{ChainRules.var"#fieldtype_pullback#421"}, Zygote.ZBack{ChainRules.var"#fieldtype_pullback#421"}, Zygote.var"#2198#back#309"{Zygote.Jnew{Pair{Int64, Vector{Float64}}, Nothing, false}}, Zygote.Pullback{Tuple{typeof(Core.convert), Type{Int64}, Int64}, Tuple{}}, Zygote.Pullback{Tuple{typeof(Core.convert), Type{Vector{Float64}}, Vector{Float64}}, Tuple{}}}}}}, Zygote.Pullback{Tuple{typeof(logdet), SparseMatrixCSC{Float64, Int64}}, Tuple{Zygote.var"#back#241"{Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 1, Zygote.Context{false}, Float64}}}, Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 1, Zygote.Context{false}, Float64}}, Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 2, Zygote.Context{false}, Int64}}, Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 1, Zygote.Context{false}, Float64}}, Zygote.var"#3123#back#837"{Zygote.var"#833#836"{SparseMatrixCSC{Float64, Int64}}}, Zygote.ZBack{ChainRules.var"#log_pullback#1324"{Float64, ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}, Zygote.ZBack{Zygote.var"#plus_pullback#341"{Tuple{Float64, Float64}}}, Zygote.var"#back#242"{Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 2, Zygote.Context{false}, Float64}}}}}, Zygote.ZBack{Zygote.var"#literal_pow_pullback#327"{2, Float64}}, Zygote.var"#2168#back#299"{Zygote.var"#back#298"{:n, Zygote.Context{false}, NamedTuple{(:x, :n), Tuple{Vector{Float64}, Int64}}, Int64}}, Zygote.Pullback{Tuple{typeof(Zygote.literal_getindex), Vector{Float64}, Val{2}}, Tuple{Zygote.var"#2594#back#529"{Zygote.var"#539#541"{1, Float64, Vector{Float64}, Tuple{Int64}}}}}, Zygote.ZBack{ChainRules.var"#fill_pullback#1455"{Tuple{NoTangent}, ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}, Zygote.ZBack{ChainRules.var"#exp_pullback#1320"{Float64, ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}, Zygote.Pullback{Tuple{typeof(Zygote.literal_getindex), Vector{Float64}, Val{1}}, Tuple{Zygote.var"#2594#back#529"{Zygote.var"#539#541"{1, Float64, Vector{Float64}, Tuple{Int64}}}}}, Zygote.var"#1910#back#157"{Zygote.var"#153#156"}}})(Δ::Float64) @ Zygote C:\Users\sam.urmy\.julia\packages\Zygote\HTsWj\src\compiler\interface2.jl:0 [9] Pullback @ .\Untitled-1:60 [inlined] [10] (::Zygote.Pullback{Tuple{var"#86#87", Vector{Float64}}, Tuple{Zygote.Pullback{Tuple{typeof(f), Vector{Float64}, NamedTuple{(:x, :n), Tuple{Vector{Float64}, Int64}}}, Tuple{Zygote.Pullback{Tuple{typeof(spdiagm), Vector{Float64}}, Tuple{Zygote.Pullback{Tuple{typeof(SparseArrays._spdiagm), Nothing, Pair{Int64, Vector{Float64}}}, Any}, Zygote.Pullback{Tuple{Type{Pair}, Int64, Vector{Float64}}, Tuple{Zygote.ZBack{ChainRules.var"#fieldtype_pullback#421"}, Zygote.ZBack{ChainRules.var"#fieldtype_pullback#421"}, Zygote.var"#2198#back#309"{Zygote.Jnew{Pair{Int64, Vector{Float64}}, Nothing, false}}, Zygote.Pullback{Tuple{typeof(Core.convert), Type{Int64}, Int64}, Tuple{}}, Zygote.Pullback{Tuple{typeof(Core.convert), Type{Vector{Float64}}, Vector{Float64}}, Tuple{}}}}}}, Zygote.Pullback{Tuple{typeof(logdet), SparseMatrixCSC{Float64, Int64}}, Tuple{Zygote.var"#back#241"{Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 1, Zygote.Context{false}, Float64}}}, Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 1, Zygote.Context{false}, Float64}}, Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 2, Zygote.Context{false}, Int64}}, Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 1, Zygote.Context{false}, Float64}}, Zygote.var"#3123#back#837"{Zygote.var"#833#836"{SparseMatrixCSC{Float64, Int64}}}, Zygote.ZBack{ChainRules.var"#log_pullback#1324"{Float64, ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}, Zygote.ZBack{Zygote.var"#plus_pullback#341"{Tuple{Float64, Float64}}}, Zygote.var"#back#242"{Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 2, Zygote.Context{false}, Float64}}}}}, Zygote.ZBack{Zygote.var"#literal_pow_pullback#327"{2, Float64}}, Zygote.var"#2168#back#299"{Zygote.var"#back#298"{:n, Zygote.Context{false}, NamedTuple{(:x, :n), Tuple{Vector{Float64}, Int64}}, Int64}}, Zygote.Pullback{Tuple{typeof(Zygote.literal_getindex), Vector{Float64}, Val{2}}, Tuple{Zygote.var"#2594#back#529"{Zygote.var"#539#541"{1, Float64, Vector{Float64}, Tuple{Int64}}}}}, Zygote.ZBack{ChainRules.var"#fill_pullback#1455"{Tuple{NoTangent}, ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}, Zygote.ZBack{ChainRules.var"#exp_pullback#1320"{Float64, ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}, Zygote.Pullback{Tuple{typeof(Zygote.literal_getindex), Vector{Float64}, Val{1}}, Tuple{Zygote.var"#2594#back#529"{Zygote.var"#539#541"{1, Float64, Vector{Float64}, Tuple{Int64}}}}}, Zygote.var"#1910#back#157"{Zygote.var"#153#156"}}}, Zygote.var"#1974#back#190"{Zygote.var"#186#189"{Zygote.Context{false}, GlobalRef, NamedTuple{(:x, :n), Tuple{Vector{Float64}, Int64}}}}}})(Δ::Float64) @ Zygote C:\Users\sam.urmy\.julia\packages\Zygote\HTsWj\src\compiler\interface2.jl:0 [11] (::Zygote.var"#75#76"{Zygote.Pullback{Tuple{var"#86#87", Vector{Float64}}, Tuple{Zygote.Pullback{Tuple{typeof(f), Vector{Float64}, NamedTuple{(:x, :n), Tuple{Vector{Float64}, Int64}}}, Tuple{Zygote.Pullback{Tuple{typeof(spdiagm), Vector{Float64}}, Tuple{Zygote.Pullback{Tuple{typeof(SparseArrays._spdiagm), Nothing, Pair{Int64, Vector{Float64}}}, Any}, Zygote.Pullback{Tuple{Type{Pair}, Int64, Vector{Float64}}, Tuple{Zygote.ZBack{ChainRules.var"#fieldtype_pullback#421"}, Zygote.ZBack{ChainRules.var"#fieldtype_pullback#421"}, Zygote.var"#2198#back#309"{Zygote.Jnew{Pair{Int64, Vector{Float64}}, Nothing, false}}, Zygote.Pullback{Tuple{typeof(Core.convert), Type{Int64}, Int64}, Tuple{}}, Zygote.Pullback{Tuple{typeof(Core.convert), Type{Vector{Float64}}, Vector{Float64}}, Tuple{}}}}}}, Zygote.Pullback{Tuple{typeof(logdet), SparseMatrixCSC{Float64, Int64}}, Tuple{Zygote.var"#back#241"{Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 1, Zygote.Context{false}, Float64}}}, Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 1, Zygote.Context{false}, Float64}}, Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 2, Zygote.Context{false}, Int64}}, Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 1, Zygote.Context{false}, Float64}}, Zygote.var"#3123#back#837"{Zygote.var"#833#836"{SparseMatrixCSC{Float64, Int64}}}, Zygote.ZBack{ChainRules.var"#log_pullback#1324"{Float64, ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}, Zygote.ZBack{Zygote.var"#plus_pullback#341"{Tuple{Float64, Float64}}}, Zygote.var"#back#242"{Zygote.var"#2017#back#209"{Zygote.var"#back#207"{2, 2, Zygote.Context{false}, Float64}}}}}, Zygote.ZBack{Zygote.var"#literal_pow_pullback#327"{2, Float64}}, Zygote.var"#2168#back#299"{Zygote.var"#back#298"{:n, Zygote.Context{false}, NamedTuple{(:x, :n), Tuple{Vector{Float64}, Int64}}, Int64}}, Zygote.Pullback{Tuple{typeof(Zygote.literal_getindex), Vector{Float64}, Val{2}}, Tuple{Zygote.var"#2594#back#529"{Zygote.var"#539#541"{1, Float64, Vector{Float64}, Tuple{Int64}}}}}, Zygote.ZBack{ChainRules.var"#fill_pullback#1455"{Tuple{NoTangent}, ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}, Zygote.ZBack{ChainRules.var"#exp_pullback#1320"{Float64, ProjectTo{Float64, NamedTuple{(), Tuple{}}}}}, Zygote.Pullback{Tuple{typeof(Zygote.literal_getindex), Vector{Float64}, Val{1}}, Tuple{Zygote.var"#2594#back#529"{Zygote.var"#539#541"{1, Float64, Vector{Float64}, Tuple{Int64}}}}}, Zygote.var"#1910#back#157"{Zygote.var"#153#156"}}}, Zygote.var"#1974#back#190"{Zygote.var"#186#189"{Zygote.Context{false}, GlobalRef, NamedTuple{(:x, :n), Tuple{Vector{Float64}, Int64}}}}}}})(Δ::Float64) @ Zygote C:\Users\sam.urmy\.julia\packages\Zygote\HTsWj\src\compiler\interface.jl:45 [12] gradient(f::Function, args::Vector{Float64}) @ Zygote C:\Users\sam.urmy\.julia\packages\Zygote\HTsWj\src\compiler\interface.jl:97 [13] top-level scope @ Untitled-1:60 ```And for the Hessian:
ERROR: StackOverflowError:
changing
spdiagm
todiagm
makes both work as expected. I assume this is due to missingrrule
s forlogabsdet(F::SparseArrays.UMFPACK.UmfpackLU)
?Relevant to TuringLang/DistributionsAD.jl#89
The text was updated successfully, but these errors were encountered: