Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GNNLux/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ GNNlib = {path = "../GNNlib"}
[compat]
ConcreteStructs = "0.2.3"
GNNGraphs = "1.4"
GNNlib = "1.4"
GNNlib = "1.5"
Lux = "1"
LuxCore = "1"
MLUtils = "0.4.4"
Expand Down
34 changes: 27 additions & 7 deletions GNNLux/src/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,8 @@ function Base.show(io::IO, l::GatedGraphConv)
end

@doc raw"""
GINConv(f, ϵ; aggr=+)
GINConv(f, ϵ = 0; aggr = +, train_eps = false)
GINConv(f; ϵ = 0, aggr = +, train_eps = false)

Graph Isomorphism convolutional layer from paper [How Powerful are Graph Neural Networks?](https://arxiv.org/pdf/1810.00826.pdf).

Expand All @@ -1300,7 +1301,9 @@ where ``f_\Theta`` typically denotes a learnable function, e.g. a linear layer o
# Arguments

- `f`: A (possibly learnable) function acting on node features.
- `ϵ`: Weighting factor.
- `ϵ`: Weighting factor ``\epsilon``, or its initial value if `train_eps = true`. Default `0`.
- `train_eps`: If `true`, ``\epsilon`` is a trainable parameter. Default `false`.
- `aggr`: Neighborhood aggregation function. Default `+`.

# Examples:

Expand All @@ -1322,7 +1325,7 @@ x = randn(rng, Float32, in_channel, g.num_nodes)
nn = Dense(in_channel, out_channel)

# create layer
l = GINConv(nn, 0.01f0, aggr = mean)
l = GINConv(nn; ϵ = 0.01f0, train_eps = true, aggr = mean)

# setup layer
ps, st = LuxCore.setup(rng, l)
Expand All @@ -1335,21 +1338,38 @@ y, st = l(g, x, ps, st) # size: out_channel × num_nodes
nn <: AbstractLuxLayer
ϵ <: Real
aggr
train_eps::Bool
end

GINConv(nn, ϵ; aggr = +) = GINConv(nn, ϵ, aggr)
GINConv(nn, ϵ::Real; aggr = +, train_eps::Bool = false) = GINConv(nn, ϵ, aggr, train_eps)

GINConv(nn, ϵ::Real, aggr) = GINConv(nn, ϵ; aggr)

GINConv(nn; ϵ::Real = 0.0f0, aggr = +, train_eps::Bool = false) =
GINConv(nn, ϵ; aggr, train_eps)

function LuxCore.initialparameters(rng::AbstractRNG, l::GINConv)
nn = LuxCore.initialparameters(rng, l.nn)
return l.train_eps ? (; nn, ϵ = Float32[l.ϵ]) : (; nn)
end

LuxCore.parameterlength(l::GINConv) = parameterlength(l.nn) + l.train_eps
LuxCore.statelength(l::GINConv) = statelength(l.nn)

function (l::GINConv)(g, x, ps, st)
nn = StatefulLuxLayer{true}(l.nn, ps.nn, st.nn)
m = (; nn, l.ϵ, l.aggr)
ϵ = l.train_eps ? ps.ϵ : l.ϵ
m = (; nn, ϵ, l.aggr)
y = GNNlib.gin_conv(m, g, x)
stnew = (; nn = _getstate(nn))
return y, stnew
end

function Base.show(io::IO, l::GINConv)
print(io, "GINConv($(l.nn)")
print(io, ", $(l.ϵ)")
print(io, ", ϵ=$(l.ϵ)")
l.train_eps && print(io, ", train_eps=true")
l.aggr == (+) || print(io, ", aggr=$(l.aggr)")
print(io, ")")
end

Expand Down Expand Up @@ -1881,4 +1901,4 @@ function (l::SAGEConv)(g, x, ps, st)
m = (; ps.weight, bias = _getbias(ps),
l.σ, l.aggr)
return GNNlib.sage_conv(m, g, x), st
end
end
17 changes: 16 additions & 1 deletion GNNLux/test/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,23 @@ end

@testset "GINConv" begin
nn = Chain(Dense(in_dims => out_dims, tanh), Dense(out_dims => out_dims))
l = GINConv(nn, 0.5)
l = GINConv(nn; ϵ = 0.5)
test_lux_layer(rng, l, g, x, sizey=(out_dims,g.num_nodes), container=true)
ps = LuxCore.initialparameters(rng, l)
@test !hasproperty(ps, :ϵ)

l = GINConv(nn; ϵ = 0.5, train_eps = true)
test_lux_layer(rng, l, g, x, sizey=(out_dims,g.num_nodes), container=true)
ps = LuxCore.initialparameters(rng, l)
@test ps.ϵ == Float32[0.5]

@test GINConv(nn, 0.5).ϵ == 0.5

y = 1.5f0 .* x .+ x * adjacency_matrix(g, Float32)
for l in (GINConv(NoOpLayer(), 0.5), GINConv(NoOpLayer(); ϵ = 0.5, train_eps = true))
ps, st = LuxCore.setup(rng, l)
@test first(l(g, x, ps, st)) ≈ y
end
end

@testset "MEGNetConv" begin
Expand Down
3 changes: 2 additions & 1 deletion GNNlib/src/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ function gin_conv(l, g::AbstractGNNGraph, x)
xj, xi = expand_srcdst(g, x)

m = propagate(copy_xj, g, l.aggr, xj = xj)
return l.nn((1 .+ ofeltype(xi, l.ϵ)) .* xi .+ m)
ϵ = l.ϵ isa Real ? ofeltype(xi, l.ϵ) : l.ϵ
Comment thread
CarloLucibello marked this conversation as resolved.
Resolved
return l.nn((1 .+ ϵ) .* xi .+ m)
end

####################### NNConv ######################################
Expand Down
4 changes: 2 additions & 2 deletions GraphNeuralNetworks/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 = "1.1.1"
version = "1.2.0-DEV"

[workspace]
projects = ["test", "docs"]
Expand All @@ -25,7 +25,7 @@ ChainRulesCore = "1"
ConcreteStructs = "0.2.3"
Flux = "0.16.0"
GNNGraphs = "1.4"
GNNlib = "1.4"
GNNlib = "1.5"
LinearAlgebra = "1"
MLUtils = "0.4"
MacroTools = "0.5"
Expand Down
29 changes: 21 additions & 8 deletions GraphNeuralNetworks/src/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ function Base.show(io::IO, l::EdgeConv)
end

@doc raw"""
GINConv(f, ϵ; aggr=+)
GINConv(f, ϵ = 0; aggr = +, train_eps = false)
GINConv(f; ϵ = 0, aggr = +, train_eps = false)

Graph Isomorphism convolutional layer from paper [How Powerful are Graph Neural Networks?](https://arxiv.org/pdf/1810.00826.pdf).

Expand All @@ -603,7 +604,9 @@ where ``f_\Theta`` typically denotes a learnable function, e.g. a linear layer o
# Arguments

- `f`: A (possibly learnable) function acting on node features.
- `ϵ`: Weighting factor.
- `ϵ`: Weighting factor ``\epsilon``, or its initial value if `train_eps = true`. Default `0`.
- `train_eps`: If `true`, ``\epsilon`` is a trainable parameter. Default `false`.
- `aggr`: Neighborhood aggregation function. Default `+`.

# Examples:

Expand All @@ -614,33 +617,43 @@ t = [2,3,1,1]
in_channel = 3
out_channel = 5
g = GNNGraph(s, t)
x = randn(Float32, in_channel, g.num_nodes)

# create dense layer
nn = Dense(in_channel, out_channel)

# create layer
l = GINConv(nn, 0.01f0, aggr = mean)
l = GINConv(nn; ϵ = 0.01f0, train_eps = true, aggr = mean)

# forward pass
y = l(g, x)
```
"""
struct GINConv{R <: Real, NN, A} <: GNNLayer
struct GINConv{E, NN, A} <: GNNLayer
nn::NN
ϵ::R
ϵ::E
aggr::A
train_eps::Bool
end

Flux.@layer :expand GINConv
Flux.trainable(l::GINConv) = (nn = l.nn,)
Flux.trainable(l::GINConv) = l.train_eps ? (; l.nn, l.ϵ) : (; l.nn)

GINConv(nn, ϵ; aggr = +) = GINConv(nn, ϵ, aggr)
GINConv(nn, ϵ::Real; aggr = +, train_eps::Bool = false) =
GINConv(nn, train_eps ? Float32[ϵ] : ϵ, aggr, train_eps)

GINConv(nn, ϵ::Real, aggr) = GINConv(nn, ϵ; aggr)

GINConv(nn; ϵ::Real = 0.0f0, aggr = +, train_eps::Bool = false) =
GINConv(nn, ϵ; aggr, train_eps)

(l::GINConv)(g, x) = GNNlib.gin_conv(l, g, x)

function Base.show(io::IO, l::GINConv)
print(io, "GINConv($(l.nn)")
print(io, ", $(l.ϵ)")
print(io, ", ϵ=$(l.ϵ)")
l.train_eps && print(io, ", train_eps=true")
l.aggr == (+) || print(io, ", aggr=$(l.aggr)")
print(io, ")")
end

Expand Down
18 changes: 15 additions & 3 deletions GraphNeuralNetworks/test/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,31 @@ end
using .TestModule
nn = Dense(D_IN, D_OUT)

l = GINConv(nn, 0.01, aggr = mean)
l = GINConv(nn; ϵ = 0.01, aggr = mean)
for g in TEST_GRAPHS
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
end
@test keys(Flux.trainable(l)) == (:nn,)

@test !in(:eps, Flux.trainable(l))
l = GINConv(nn; ϵ = 0.5, train_eps = true, aggr = mean)
@test keys(Flux.trainable(l)) == (:nn, :ϵ)
@test l.ϵ == Float32[0.5]
g = first(TEST_GRAPHS)
test_gradients(l, g, g.x, rtol = RTOL_HIGH)

@test GINConv(nn, 0.5).ϵ == 0.5

x = g.x
y = 1.5f0 .* x .+ x * adjacency_matrix(g, Float32)
@test GINConv(identity, 0.5)(g, x) ≈ y
@test GINConv(identity; ϵ = 0.5, train_eps = true)(g, x) ≈ y
end

@testitem "GINConv GPU" setup=[TolSnippet, TestModule] tags=[:gpu] begin
using .TestModule
nn = Dense(D_IN, D_OUT)
l = GINConv(nn, 0.01, aggr = mean)
l = GINConv(nn; train_eps = true, aggr = mean)
for g in TEST_GRAPHS
g.graph isa AbstractSparseMatrix && continue
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
Expand Down