Skip to content

Commit ebf7253

Browse files
Review fixes for trainable GINConv epsilon
- GNNLux: keep ϵ a scalar in the layer and create a fresh `Float32[ϵ]` in `initialparameters`, so parameters don't alias the layer struct. - Document both positional and keyword ϵ; define `x` in the Flux example. - Test trainable-ϵ gradients against finite differences, and the forward pass against (1 + ϵ) xᵢ + ∑ⱼ xⱼ in both frontends. - Bump GraphNeuralNetworks to 1.2.0-DEV; require GNNlib 1.5 in the frontends (array ϵ in `gin_conv`). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 032f519 commit ebf7253

6 files changed

Lines changed: 28 additions & 20 deletions

File tree

‎GNNLux/Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ GNNlib = {path = "../GNNlib"}
2626
[compat]
2727
ConcreteStructs = "0.2.3"
2828
GNNGraphs = "1.4"
29-
GNNlib = "1.4"
29+
GNNlib = "1.5"
3030
Lux = "1"
3131
LuxCore = "1"
3232
MLUtils = "0.4.4"

‎GNNLux/src/layers/conv.jl‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,8 @@ function Base.show(io::IO, l::GatedGraphConv)
12871287
end
12881288

12891289
@doc raw"""
1290-
GINConv(f, [ϵ]; aggr=+, train_eps=false)
1290+
GINConv(f, ϵ = 0; aggr = +, train_eps = false)
1291+
GINConv(f; ϵ = 0, aggr = +, train_eps = false)
12911292
12921293
Graph Isomorphism convolutional layer from paper [How Powerful are Graph Neural Networks?](https://arxiv.org/pdf/1810.00826.pdf).
12931294
@@ -1300,8 +1301,8 @@ where ``f_\Theta`` typically denotes a learnable function, e.g. a linear layer o
13001301
# Arguments
13011302
13021303
- `f`: A (possibly learnable) function acting on node features.
1303-
- `ϵ`: Initial value of the weighting factor ``\epsilon``. Default `0.0f0`.
1304-
- `train_eps`: If `true`, ``\epsilon`` is trainable. Default `false`.
1304+
- `ϵ`: Weighting factor ``\epsilon``, or its initial value if `train_eps = true`. Default `0`.
1305+
- `train_eps`: If `true`, ``\epsilon`` is a trainable parameter. Default `false`.
13051306
- `aggr`: Neighborhood aggregation function. Default `+`.
13061307
13071308
# Examples:
@@ -1335,13 +1336,12 @@ y, st = l(g, x, ps, st) # size: out_channel × num_nodes
13351336
"""
13361337
@concrete struct GINConv <: GNNContainerLayer{(:nn,)}
13371338
nn <: AbstractLuxLayer
1338-
ϵ
1339+
ϵ <: Real
13391340
aggr
13401341
train_eps::Bool
13411342
end
13421343

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

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

@@ -1350,7 +1350,7 @@ GINConv(nn; ϵ::Real = 0.0f0, aggr = +, train_eps::Bool = false) =
13501350

13511351
function LuxCore.initialparameters(rng::AbstractRNG, l::GINConv)
13521352
nn = LuxCore.initialparameters(rng, l.nn)
1353-
return l.train_eps ? (; nn, ϵ = l.ϵ) : (; nn)
1353+
return l.train_eps ? (; nn, ϵ = Float32[l.ϵ]) : (; nn)
13541354
end
13551355

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

‎GNNLux/test/layers/conv.jl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ end
121121
@test ps.ϵ == Float32[0.5]
122122

123123
@test GINConv(nn, 0.5).ϵ == 0.5
124+
125+
y = 1.5f0 .* x .+ x * adjacency_matrix(g, Float32)
126+
for l in (GINConv(NoOpLayer(), 0.5), GINConv(NoOpLayer(); ϵ = 0.5, train_eps = true))
127+
ps, st = LuxCore.setup(rng, l)
128+
@test first(l(g, x, ps, st)) ≈ y
129+
end
124130
end
125131

126132
@testset "MEGNetConv" begin

‎GraphNeuralNetworks/Project.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GraphNeuralNetworks"
22
uuid = "cffab07f-9bc2-4db1-8861-388f63bf7694"
33
authors = ["Carlo Lucibello and contributors"]
4-
version = "1.1.1"
4+
version = "1.2.0-DEV"
55

66
[workspace]
77
projects = ["test", "docs"]
@@ -25,7 +25,7 @@ ChainRulesCore = "1"
2525
ConcreteStructs = "0.2.3"
2626
Flux = "0.16.0"
2727
GNNGraphs = "1.4"
28-
GNNlib = "1.4"
28+
GNNlib = "1.5"
2929
LinearAlgebra = "1"
3030
MLUtils = "0.4"
3131
MacroTools = "0.5"

‎GraphNeuralNetworks/src/layers/conv.jl‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,8 @@ function Base.show(io::IO, l::EdgeConv)
590590
end
591591

592592
@doc raw"""
593-
GINConv(f, [ϵ]; aggr=+, train_eps=false)
593+
GINConv(f, ϵ = 0; aggr = +, train_eps = false)
594+
GINConv(f; ϵ = 0, aggr = +, train_eps = false)
594595
595596
Graph Isomorphism convolutional layer from paper [How Powerful are Graph Neural Networks?](https://arxiv.org/pdf/1810.00826.pdf).
596597
@@ -603,8 +604,8 @@ where ``f_\Theta`` typically denotes a learnable function, e.g. a linear layer o
603604
# Arguments
604605
605606
- `f`: A (possibly learnable) function acting on node features.
606-
- `ϵ`: Initial value of the weighting factor ``\epsilon``. Default `0.0f0`.
607-
- `train_eps`: If `true`, ``\epsilon`` is trainable. Default `false`.
607+
- `ϵ`: Weighting factor ``\epsilon``, or its initial value if `train_eps = true`. Default `0`.
608+
- `train_eps`: If `true`, ``\epsilon`` is a trainable parameter. Default `false`.
608609
- `aggr`: Neighborhood aggregation function. Default `+`.
609610
610611
# Examples:
@@ -616,6 +617,7 @@ t = [2,3,1,1]
616617
in_channel = 3
617618
out_channel = 5
618619
g = GNNGraph(s, t)
620+
x = randn(Float32, in_channel, g.num_nodes)
619621
620622
# create dense layer
621623
nn = Dense(in_channel, out_channel)

‎GraphNeuralNetworks/test/layers/conv.jl‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,18 @@ end
285285
end
286286
@test keys(Flux.trainable(l)) == (:nn,)
287287

288-
l = GINConv(Dense(D_IN, D_OUT); ϵ = 0.5, train_eps = true, aggr = mean)
288+
l = GINConv(nn; ϵ = 0.5, train_eps = true, aggr = mean)
289289
@test keys(Flux.trainable(l)) == (:nn, :ϵ)
290290
@test l.ϵ == Float32[0.5]
291-
for g in TEST_GRAPHS
292-
@test size(l(g, g.x)) == (D_OUT, g.num_nodes)
293-
end
294291
g = first(TEST_GRAPHS)
295-
grad = Flux.gradient(l -> mean(l(g, g.x)), l)[1]
296-
@test grad.ϵ isa AbstractVector
297-
@test length(grad.ϵ) == 1
292+
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
298293

299294
@test GINConv(nn, 0.5).ϵ == 0.5
295+
296+
x = g.x
297+
y = 1.5f0 .* x .+ x * adjacency_matrix(g, Float32)
298+
@test GINConv(identity, 0.5)(g, x) ≈ y
299+
@test GINConv(identity; ϵ = 0.5, train_eps = true)(g, x) ≈ y
300300
end
301301

302302
@testitem "GINConv GPU" setup=[TolSnippet, TestModule] tags=[:gpu] begin

0 commit comments

Comments
 (0)