Skip to content

Commit f3be1f0

Browse files
i-Amoghclaude
andauthored
Add AGNNConv support for HeteroGraphConv (#649)
AGNNConv accepts a tuple (xj, xi) of source and target node features, so it can be used in HeteroGraphConv and on single-relation heterographs (GNNLux). Self loops are only added to relations whose source and target node types coincide. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 56fd000 commit f3be1f0

8 files changed

Lines changed: 95 additions & 8 deletions

File tree

‎GNNLux/docs/src/api/conv.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The table below lists all graph convolutional layers implemented in the *GNNLux.
1717

1818
| Layer |Sparse Ops|Edge Weight|Edge Features| Heterograph | TemporalSnapshotsGNNGraphs |
1919
| :-------- | :---: |:---: |:---: | :---: | :---: |
20-
| [`AGNNConv`](@ref) | | | ✓ | | |
20+
| [`AGNNConv`](@ref) | | | | ✓ | |
2121
| [`CGConv`](@ref) | | | ✓ | ✓ | ✓ |
2222
| [`ChebConv`](@ref) | | | | | ✓ |
2323
| [`EGNNConv`](@ref) | | | ✓ | | |

‎GNNLux/src/layers/conv.jl‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,18 @@ and ``\beta`` a trainable parameter if `trainable=true`.
369369
- `init_beta`: The initial value of ``\beta``. Default 1.0f0.
370370
- `trainable`: If true, ``\beta`` is trainable. Default `true`.
371371
- `add_self_loops`: Add self loops to the graph before performing the convolution. Default `true`.
372+
No self loops are added to a relation of a heterogeneous graph whose source
373+
and target node types differ.
374+
375+
# Forward
376+
377+
(::AGNNConv)(g, x, ps, st) -> (AbstractMatrix, st)
378+
379+
Takes as input a graph `g` and a node feature matrix `x` of size `[in, num_nodes]`, or a tuple
380+
`(xj, xi)` of source and target node features, e.g. for a single relation of a heterogeneous graph.
381+
In the latter case ``\mathbf{x}_j`` is taken from `xj` and ``\mathbf{x}_i`` from `xi` in the
382+
formulas above, so `xj` and `xi` must have the same number of features.
383+
Returns a matrix of size `[in, num_target_nodes]`.
372384
373385
# Examples:
374386
@@ -382,6 +394,7 @@ rng = Random.default_rng()
382394
s = [1,1,2,3]
383395
t = [2,3,1,1]
384396
g = GNNGraph(s, t)
397+
x = randn(rng, Float32, 3, g.num_nodes)
385398
386399
# create layer
387400
l = AGNNConv(init_beta=2.0f0)
@@ -421,7 +434,7 @@ function Base.show(io::IO, l::AGNNConv)
421434
print(io, ")")
422435
end
423436

424-
function (l::AGNNConv)(g, x::AbstractMatrix, ps, st)
437+
function (l::AGNNConv)(g, x, ps, st)
425438
β = l.trainable ? ps.β : l.init_beta
426439
m = (; β, l.add_self_loops)
427440
return GNNlib.agnn_conv(m, g, x), st

‎GNNLux/test/layers/conv.jl‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ end
4343
@testset "AGNNConv" begin
4444
l = AGNNConv(init_beta=1.0f0)
4545
test_lux_layer(rng, l, g, x, sizey=(in_dims, 10))
46+
47+
# bipartite input vs homogeneous layer on the graph with nodes A ∪ B
48+
s, t = [1, 1, 2, 2], [1, 2, 2, 3]
49+
hg = GNNHeteroGraph((:A, :to, :B) => (s, t), num_nodes = Dict(:A => 2, :B => 3))
50+
xA, xB = randn(rng, Float32, in_dims, 2), randn(rng, Float32, in_dims, 3)
51+
ps, st = LuxCore.setup(rng, l)
52+
y, _ = l(hg, (xA, xB), ps, st)
53+
ŷ, _ = AGNNConv(init_beta=1.0f0, add_self_loops=false)(GNNGraph(s, t .+ 2, num_nodes = 5), [xA xB], ps, st)
54+
@test y ≈ ŷ[:, 3:5]
4655
end
4756

4857
@testset "EdgeConv" begin

‎GNNlib/src/layers/conv.jl‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,25 @@ end
341341

342342
####################### AGNNConv ######################################
343343

344-
function agnn_conv(l, g::GNNGraph, x::AbstractMatrix)
344+
function agnn_conv(l, g::AbstractGNNGraph, x)
345345
check_num_nodes(g, x)
346346
if l.add_self_loops
347347
g = add_self_loops(g)
348348
end
349349

350-
xn = x ./ sqrt.(sum(x .^ 2, dims = 1))
351-
cos_dist = apply_edges(xi_dot_xj, g, xi = xn, xj = xn)
350+
xj, xi = expand_srcdst(g, x)
351+
352+
xi_n = xi ./ sqrt.(sum(xi .^ 2, dims = 1))
353+
if xj !== xi
354+
xj_n = xj ./ sqrt.(sum(xj .^ 2, dims = 1))
355+
else
356+
xj_n = xi_n
357+
end
358+
cos_dist = apply_edges(xi_dot_xj, g, xi = xi_n, xj = xj_n)
352359
α = softmax_edge_neighbors(g, l.β .* cos_dist)
353360

354-
x = propagate(g, +; xj = x, e = α) do xi, xj, α
355-
α .* xj
361+
x = propagate(g, +; xj, e = α) do xi, xj, α
362+
α .* xj
356363
end
357364

358365
return x

‎GraphNeuralNetworks/docs/src/api/conv.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The table below lists all graph convolutional layers implemented in the *GraphNe
1818

1919
| Layer |Sparse Ops|Edge Weight|Edge Features| Heterograph | TemporalSnapshotsGNNGraphs |
2020
| :-------- | :---: |:---: |:---: | :---: | :---: |
21-
| [`AGNNConv`](@ref) | | | ✓ | | |
21+
| [`AGNNConv`](@ref) | | | | ✓ | |
2222
| [`CGConv`](@ref) | | | ✓ | ✓ | ✓ |
2323
| [`ChebConv`](@ref) | | | | | ✓ |
2424
| [`EGNNConv`](@ref) | | | ✓ | | |

‎GraphNeuralNetworks/src/layers/conv.jl‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,18 @@ and ``\beta`` a trainable parameter if `trainable=true`.
982982
- `init_beta`: The initial value of ``\beta``. Default 1.0f0.
983983
- `trainable`: If true, ``\beta`` is trainable. Default `true`.
984984
- `add_self_loops`: Add self loops to the graph before performing the convolution. Default `true`.
985+
No self loops are added to a relation of a heterogeneous graph whose source
986+
and target node types differ.
987+
988+
# Forward
989+
990+
(::AGNNConv)(g, x) -> AbstractMatrix
991+
992+
Takes as input a graph `g` and a node feature matrix `x` of size `[in, num_nodes]`, or a tuple
993+
`(xj, xi)` of source and target node features, e.g. for a relation of a heterogeneous graph
994+
(see [`HeteroGraphConv`](@ref)). In the latter case ``\mathbf{x}_j`` is taken from `xj` and
995+
``\mathbf{x}_i`` from `xi` in the formulas above, so `xj` and `xi` must have the same number of features.
996+
Returns a matrix of size `[in, num_target_nodes]`.
985997
986998
# Examples:
987999
@@ -990,6 +1002,7 @@ and ``\beta`` a trainable parameter if `trainable=true`.
9901002
s = [1,1,2,3]
9911003
t = [2,3,1,1]
9921004
g = GNNGraph(s, t)
1005+
x = randn(Float32, 3, g.num_nodes)
9931006
9941007
# create layer
9951008
l = AGNNConv(init_beta=2.0f0)

‎GraphNeuralNetworks/test/layers/conv.jl‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,30 @@ end
424424
@test size(l(g, g.x)) == (D_IN, g.num_nodes)
425425
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
426426
end
427+
428+
function agnn_ref(g, xj, xi, β)
429+
s, t = edge_index(g)
430+
cosine(a, b) = sum(a .* b) / sqrt(sum(abs2, a) * sum(abs2, b))
431+
y = zeros(Float32, size(xj, 1), size(xi, 2))
432+
for i in axes(xi, 2)
433+
js = s[t .== i]
434+
w = [exp(β * cosine(xi[:, i], xj[:, j])) for j in js]
435+
for (wk, j) in zip(w, js)
436+
y[:, i] .+= wk / sum(w) .* xj[:, j]
437+
end
438+
end
439+
return y
440+
end
441+
442+
for g in TEST_GRAPHS
443+
@test l(g, g.x) ≈ agnn_ref(add_self_loops(g), g.x, g.x, 2.0f0)
444+
end
445+
446+
g = TEST_GRAPHS[2]
447+
xj, xi = randn(Float32, D_IN, g.num_nodes), randn(Float32, D_IN, g.num_nodes)
448+
@test l(g, (xj, xi)) ≈ agnn_ref(add_self_loops(g), xj, xi, 2.0f0)
449+
l = AGNNConv(init_beta = 2.0f0, add_self_loops = false)
450+
@test l(g, (xj, xi)) ≈ agnn_ref(g, xj, xi, 2.0f0)
427451
end
428452

429453
@testitem "AGNNConv GPU" setup=[TolSnippet, TestModule] tags=[:gpu] begin

‎GraphNeuralNetworks/test/layers/heteroconv.jl‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,25 @@
158158
y = layers(g, x);
159159
@test size(y.A) == (2,2) && size(y.B) == (2,3)
160160
end
161+
162+
@testset "AGNNConv" begin
163+
x = (A = rand(Float32, 4, 2), B = rand(Float32, 4, 3))
164+
layers = HeteroGraphConv((:A, :to, :B) => AGNNConv(init_beta = 2.0f0),
165+
(:B, :to, :A) => AGNNConv(init_beta = 2.0f0))
166+
y = layers(hg, x)
167+
# reference: homogeneous layer on the graph with nodes A ∪ B;
168+
# no self loops are added to relations between different node types
169+
sAB, tAB = edge_index(hg, (:A, :to, :B))
170+
sBA, tBA = edge_index(hg, (:B, :to, :A))
171+
g = GNNGraph([sAB; sBA .+ 2], [tAB .+ 2; tBA], num_nodes = 5)
172+
ŷ = AGNNConv(init_beta = 2.0f0, add_self_loops = false)(g, [x.A x.B])
173+
@test y.A ≈ ŷ[:, 1:2]
174+
@test y.B ≈ ŷ[:, 3:5]
175+
176+
s, t = [1, 2, 3, 3], [2, 3, 1, 2]
177+
hg_AA = GNNHeteroGraph((:A, :to, :A) => (s, t))
178+
x = rand(Float32, 4, 3)
179+
y = HeteroGraphConv((:A, :to, :A) => AGNNConv())(hg_AA, (; A = x))
180+
@test y.A ≈ AGNNConv()(GNNGraph(s, t), x)
181+
end
161182
end

0 commit comments

Comments
 (0)