CarloLucibello / GraphNeuralNetworks.jl

Graph Neural Networks in Julia
https://carlolucibello.github.io/GraphNeuralNetworks.jl/dev/
MIT License
215 stars 46 forks source link

Problem with GNNChain and NNConv #49

Closed oysteinsolheim closed 2 years ago

oysteinsolheim commented 2 years ago

The following example raises a MethodError:

using Flux
using GraphNeuralNetworks

ndata = rand(2,3)
edata = rand(2,3)

n_in=2
n_out=4

g = GNNGraph([1,1,2], [2, 3, 3], ndata=ndata, edata=edata)
edge_model = Dense(2,n_in*n_out, relu)
gnn_model = GNNChain(
    NNConv(n_in=>n_out, edge_model, relu)
)
gnn_model(g)

with error message

ERROR: MethodError: no method matching (::NNConv)(::GNNGraph{Tuple{Vector{Int64}, Vector{Int64}, Nothing}}, ::Matrix{Float64}) Closest candidates are: (::NNConv)(::GNNGraph, ::AbstractMatrix{T} where T, ::Any) at /home/oystein/JuliaProjects/GraphNeuralNetworks.jl/src/layers/conv.jl:480 (::NNConv)(::GNNGraph) at /home/oystein/JuliaProjects/GraphNeuralNetworks.jl/src/layers/conv.jl:495 (::GNNLayer)(::GNNGraph) at /home/oystein/JuliaProjects/GraphNeuralNetworks.jl/src/layers/basic.jl:12

Am I missing something here? Calling an individual layer with g is ok.

CarloLucibello commented 2 years ago

NNConv has trickier interaction with the GNNChain since it also necessitates the edge features. Should be solved by #50. Will merge and tag new release as soon as tests pass.

oysteinsolheim commented 2 years ago

I also got an error if I included a GlobalPool layer after the NNConv. I thought I managed to work around it by including (l::GlobalPool)(g::GNNGraph) = l(g, g.ndata.x) and GraphNeuralNetworks.applychain(::Tuple{},x) = x. but after further inspection it's not working.

CarloLucibello commented 2 years ago

Thanks for the heads up, keep them coming. It will be fixed in #52. It is a bit tricky to orchestrate node, edge, and graph features at the same time with a GNNChain. I'd like to push the functionality of GNNChain as far as I can, but in some complex scenarios explicitly defining the model (cf. https://carlolucibello.github.io/GraphNeuralNetworks.jl/dev/models/) could be the only option.

oysteinsolheim commented 2 years ago

Thanks a lot! Yes, I'll have to revisit the explicit modelling, I forgot about that opportunity.