Skip to content

Commit

Permalink
Merge pull request #50 from CarloLucibello/cl/fix
Browse files Browse the repository at this point in the history
better handling of chain with only graph input
  • Loading branch information
CarloLucibello authored Oct 3, 2021
2 parents 867cc10 + 6b9c3f5 commit de14ded
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GraphNeuralNetworks"
uuid = "cffab07f-9bc2-4db1-8861-388f63bf7694"
authors = ["Carlo Lucibello and contributors"]
version = "0.2.0"
version = "0.2.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
14 changes: 12 additions & 2 deletions src/layers/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,29 @@ end

Flux.functor(::Type{<:GNNChain}, c) = c.layers, ls -> GNNChain(ls...)

# input from graph
applylayer(l, g::GNNGraph) = GNNGraph(g, ndata=l(node_features(g)))
applylayer(l::GNNLayer, g::GNNGraph) = l(g)

# explicit input
applylayer(l, g::GNNGraph, x) = l(x)
applylayer(l::GNNLayer, g::GNNGraph, x) = l(g, x)

# Handle Flux.Parallel
applylayer(l::Parallel, g::GNNGraph) = GNNGraph(g, ndata=applylayer(l, g, node_features(g)))
applylayer(l::Parallel, g::GNNGraph, x::AbstractArray) = mapreduce(f -> applylayer(f, g, x), l.connection, l.layers)
applylayer(l::Parallel, g::GNNGraph, xs::Vararg{<:AbstractArray}) = mapreduce((f, x) -> applylayer(f, g, x), l.connection, l.layers, xs)
applylayer(l::Parallel, g::GNNGraph, xs::Tuple) = applylayer(l, g, xs...)

# input from graph
applychain(::Tuple{}, g::GNNGraph) = g
applychain(fs::Tuple, g::GNNGraph) = applychain(tail(fs), applylayer(first(fs), g))

# explicit input
applychain(::Tuple{}, g::GNNGraph, x) = x
applychain(fs::Tuple, g::GNNGraph, x) = applychain(tail(fs), g, applylayer(first(fs), g, x))

(c::GNNChain)(g::GNNGraph, x) = applychain(Tuple(c.layers), g, x)
(c::GNNChain)(g::GNNGraph) = applychain(Tuple(c.layers), g)


Base.getindex(c::GNNChain, i::AbstractArray) = GNNChain(c.layers[i]...)
Base.getindex(c::GNNChain{<:NamedTuple}, i::AbstractArray) =
Expand Down
12 changes: 12 additions & 0 deletions test/layers/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@

test_layer(gnn, g, rtol=1e-5, exclude_grad_fields=[, :σ²])
end

@testset "Only graph input" begin
nin, nout = 2, 4
ndata = rand(nin, 3)
edata = rand(nin, 3)
g = GNNGraph([1,1,2], [2, 3, 3], ndata=ndata, edata=edata)
m = NNConv(nin => nout, Dense(2, nin*nout, tanh))
chain = GNNChain(m)
y = m(g, g.ndata.x, g.edata.e)
@test m(g).ndata.x == y
@test chain(g).ndata.x == y
end
end
end

2 comments on commit de14ded

@CarloLucibello
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/46002

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.1 -m "<description of version>" de14ded63f8d9bb19da5f26a8fa5f5b4b62b2834
git push origin v0.2.1

Please sign in to comment.