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

GCNConv layer fails when the GNNGraph comes from an adjacency matrix #486

Closed
gdalle opened this issue Aug 26, 2024 · 1 comment
Closed

Comments

@gdalle
Copy link
Member

gdalle commented Aug 26, 2024

MWE:

using GraphNeuralNetworks, Flux, Zygote
using LinearAlgebra, Random, SparseArrays, Statistics

rng = Random.seed!(63)

x = randn(rng, Float32, 5, 10)
y = randn(10)

A = sparse(Symmetric(sprand(rng, Bool, 10, 10, 0.2)))

g1 = rand_graph(10, 20; ndata=(; x, y))  # random graph
g2 = GNNGraph(A; ndata=(; x, y))  # from sparse adjacency matrix

model1 = GNNChain(Dense(5 => 1))  # without GCNConv
model2 = GNNChain(GCNConv(5 => 5), Dense(5 => 1))  # with GCNConv

myloss(model, g::GNNGraph) = mean(abs2, vec(model(g, g.x)) .- g.y)

model1(g1, g1.x), model2(g1, g1.x), model2(g2, g2.x), model2(g2, g2.x);  # all works

gradient(model -> myloss(model, g1), model1)  # works
gradient(model -> myloss(model, g1), model2)  # works
gradient(model -> myloss(model, g2), model1)  # works
gradient(model -> myloss(model, g2), model2)  # fails

Error:

julia> gradient(model -> myloss(model, g2), model2)  # fails
ERROR: MethodError: no method matching zero(::Nothing)

Closest candidates are:
  zero(::Type{Union{}}, Any...)
   @ Base number.jl:310
  zero(::Type{Dates.Time})
   @ Dates ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/Dates/src/types.jl:440
  zero(::Type{Pkg.Resolve.FieldValue})
   @ Pkg ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/Pkg/src/Resolve/fieldvalues.jl:38
  ...

Stacktrace:
  [1] iszero(x::Nothing)
    @ Base ./number.jl:42
  [2] _iszero(x::Nothing)
    @ SparseArrays ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/SparseArrays/src/SparseArrays.jl:41
  [3] _noshapecheck_map(::typeof(Zygote.wrap_chainrules_output), ::SparseMatrixCSC{ChainRulesCore.NoTangent, Int64})
    @ SparseArrays.HigherOrderFns ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/SparseArrays/src/higherorderfns.jl:181
  [4] map
    @ ~/.julia/juliaup/julia-1.10.4+0.x64.linux.gnu/share/julia/stdlib/v1.10/SparseArrays/src/higherorderfns.jl:1187 [inlined]
  [5] wrap_chainrules_output
    @ ~/.julia/packages/Zygote/nsBv0/src/compiler/chainrules.jl:127 [inlined]
  [6] wrap_chainrules_output
    @ ~/.julia/packages/Zygote/nsBv0/src/compiler/chainrules.jl:110 [inlined]
  [7] map
    @ ./tuple.jl:293 [inlined]
  [8] wrap_chainrules_output
    @ ~/.julia/packages/Zygote/nsBv0/src/compiler/chainrules.jl:111 [inlined]
  [9] ZBack
    @ ~/.julia/packages/Zygote/nsBv0/src/compiler/chainrules.jl:211 [inlined]
 [10] propagate
    @ ~/.julia/packages/GraphNeuralNetworks/AxIf2/src/msgpass.jl:251 [inlined]
 [11] #propagate#128
    @ ~/.julia/packages/GraphNeuralNetworks/AxIf2/src/msgpass.jl:77 [inlined]
 [12] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Matrix{Float32})
    @ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
 [13] propagate
    @ ~/.julia/packages/GraphNeuralNetworks/AxIf2/src/msgpass.jl:76 [inlined]
 [14] (::Zygote.Pullback{Tuple{…}, Any})(Δ::Matrix{Float32})
    @ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
 [15] #_#24
    @ ~/.julia/packages/GraphNeuralNetworks/AxIf2/src/layers/conv.jl:160 [inlined]
 [16] (::Zygote.Pullback{Tuple{…}, Any})(Δ::Matrix{Float32})
    @ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
 [17] GCNConv (repeats 2 times)
    @ ~/.julia/packages/GraphNeuralNetworks/AxIf2/src/layers/conv.jl:103 [inlined]
 [18] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Matrix{Float32})
    @ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
 [19] _applylayer
    @ ~/.julia/packages/GraphNeuralNetworks/AxIf2/src/layers/basic.jl:158 [inlined]
 [20] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Matrix{Float32})
    @ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
 [21] _applychain
    @ ~/.julia/packages/GraphNeuralNetworks/AxIf2/src/layers/basic.jl:144 [inlined]
 [22] (::Zygote.Pullback{Tuple{…}, Any})(Δ::Matrix{Float32})
    @ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
 [23] GNNChain
    @ ~/.julia/packages/GraphNeuralNetworks/AxIf2/src/layers/basic.jl:130 [inlined]
 [24] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Matrix{Float32})
    @ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
 [25] myloss
    @ ~/Work/GitHub/Julia/Oversmoothing.jl/experiments/gnn.jl:43 [inlined]
 [26] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
 [27] #25
    @ ~/Work/GitHub/Julia/Oversmoothing.jl/experiments/gnn.jl:50 [inlined]
 [28] (::Zygote.Pullback{Tuple{…}, Tuple{…}})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface2.jl:0
 [29] (::Zygote.var"#75#76"{Zygote.Pullback{Tuple{}, Tuple{}}})(Δ::Float64)
    @ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface.jl:91
 [30] gradient(f::Function, args::GNNChain{Tuple{GCNConv{…}, Dense{…}}})
    @ Zygote ~/.julia/packages/Zygote/nsBv0/src/compiler/interface.jl:148
 [31] top-level scope
    @ ~/Work/GitHub/Julia/Oversmoothing.jl/experiments/gnn.jl:50
Some type information was truncated. Use `show(err)` to see complete types.
@gdalle
Copy link
Member Author

gdalle commented Aug 26, 2024

My bad, I had not specified graph_type=:sparse

@gdalle gdalle closed this as completed Aug 26, 2024
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

1 participant