Skip to content
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

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

Closed
pzhanggit opened this issue Jul 24, 2020 · 4 comments

Comments

@pzhanggit
Copy link

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
@Yansf677
Copy link

I have encountered the same problem, have you finished yet?

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

@pzhanggit
Copy link
Author

I have encountered the same problem, have you finished yet?

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

No, I tried to define a custom adjoint like,

@adjoint sparse(a,b,c) = sparse(a,b,c), Δ->( 0.0, 0.0, sparse(a,b,Δ))
Zygote.refresh()

But it did not work.

@Yansf677
Copy link

you can refer to #570

bors bot added a commit that referenced this issue Dec 23, 2020
762: basic sparse handling r=DhairyaLGandhi a=DhairyaLGandhi

Relates to #570, SciML/DifferentialEquations.jl#649, #742

Co-authored-by: Dhairya Gandhi <[email protected]>
Co-authored-by: Dhairya Gandhi <[email protected]>
@CarloLucibello
Copy link
Member

this has been fixed in JuliaDiff/ChainRules.jl#579

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants