Skip to content

ERROR: LoadError: Need an adjoint for constructor SparseMatrixCSC{Float64,Int64}. Gradient is of type Array{Float64,2}. #742

@pzhanggit

Description

@pzhanggit

In the following case,

using SparseArrays,Zygote

function loss_rd(x::AbstractArray{T,1}) where{T}
    f = ones(T, 3)
    K = sparse(I, J, x)
    u = K \ f
    return sum(abs2, u)
end
# case that does not work
I = [1, 1, 2, 3, 3]
J = [1, 3, 2, 3, 1]
x = [3.0, 1.0, 3.0, 3.0, 1.0]

println("loss_rd=", loss_rd(x))

dldx=Zygote.gradient(loss_rd,x)
println("Zygnote, dl/dx=",dldx)

I got the error,

ERROR: Need an adjoint for constructor SparseMatrixCSC{Float64,Int64}. Gradient is of type Array{Float64,2}
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] (::Zygote.Jnew{SparseMatrixCSC{Float64,Int64},Nothing,false})(::Array{Float64,2}) at /Users/6pz/.julia/packages/Zygote/1GXzF/src/lib/lib.jl:306
 [3] (::Zygote.var"#380#back#194"{Zygote.Jnew{SparseMatrixCSC{Float64,Int64},Nothing,false}})(::Array{Float64,2}) at /Users/6pz/.julia/packages/ZygoteRules/6nssF/src/adjoint.jl:49
 [4] SparseMatrixCSC at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/SparseArrays/src/sparsematrix.jl:32 [inlined]
 [5] (::typeof(∂(SparseMatrixCSC{Float64,Int64})))(::Array{Float64,2}) at /Users/6pz/.julia/packages/Zygote/1GXzF/src/compiler/interface2.jl:0
 [6] SparseMatrixCSC at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/SparseArrays/src/sparsematrix.jl:45 [inlined]
 [7] (::typeof(∂(SparseMatrixCSC)))(::Array{Float64,2}) at /Users/6pz/.julia/packages/Zygote/1GXzF/src/compiler/interface2.jl:0
 [8] sparse! at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/SparseArrays/src/sparsematrix.jl:824 [inlined]
 [9] (::typeof(∂(sparse!)))(::Array{Float64,2}) at /Users/6pz/.julia/packages/Zygote/1GXzF/src/compiler/interface2.jl:0
 [10] sparse at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/SparseArrays/src/sparsematrix.jl:661 [inlined]
 [11] (::typeof(∂(sparse)))(::Array{Float64,2}) at /Users/6pz/.julia/packages/Zygote/1GXzF/src/compiler/interface2.jl:0
 [12] sparse at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/SparseArrays/src/sparsematrix.jl:850 [inlined]
 [13] (::typeof(∂(sparse)))(::Array{Float64,2}) at /Users/6pz/.julia/packages/Zygote/1GXzF/src/compiler/interface2.jl:0
 [14] sparse at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/SparseArrays/src/sparsematrix.jl:846 [inlined]
 [15] (::typeof(∂(sparse)))(::Array{Float64,2}) at /Users/6pz/.julia/packages/Zygote/1GXzF/src/compiler/interface2.jl:0
 [16] loss_rd at ./REPL[10]:3 [inlined]
 [17] (::typeof(∂(loss_rd)))(::Float64) at /Users/6pz/.julia/packages/Zygote/1GXzF/src/compiler/interface2.jl:0
 [18] (::Zygote.var"#37#38"{typeof(∂(loss_rd))})(::Float64) at /Users/6pz/.julia/packages/Zygote/1GXzF/src/compiler/interface.jl:45
 [19] gradient(::Function, ::Array{Float64,1}) at /Users/6pz/.julia/packages/Zygote/1GXzF/src/compiler/interface.jl:54
 [20] top-level scope at REPL[15]:1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions