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
2 changes: 1 addition & 1 deletion GNNLux/docs/src/api/conv.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The table below lists all graph convolutional layers implemented in the *GNNLux.

| Layer |Sparse Ops|Edge Weight|Edge Features| Heterograph | TemporalSnapshotsGNNGraphs |
| :-------- | :---: |:---: |:---: | :---: | :---: |
| [`AGNNConv`](@ref) | | | ✓ | | |
| [`AGNNConv`](@ref) | | | | ✓ | |
| [`CGConv`](@ref) | | | ✓ | ✓ | ✓ |
| [`ChebConv`](@ref) | | | | | ✓ |
| [`EGNNConv`](@ref) | | | ✓ | | |
Expand Down
15 changes: 14 additions & 1 deletion GNNLux/src/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,18 @@ and ``\beta`` a trainable parameter if `trainable=true`.
- `init_beta`: The initial value of ``\beta``. Default 1.0f0.
- `trainable`: If true, ``\beta`` is trainable. Default `true`.
- `add_self_loops`: Add self loops to the graph before performing the convolution. Default `true`.
No self loops are added to a relation of a heterogeneous graph whose source
and target node types differ.

# Forward

(::AGNNConv)(g, x, ps, st) -> (AbstractMatrix, st)

Takes as input a graph `g` and a node feature matrix `x` of size `[in, num_nodes]`, or a tuple
`(xj, xi)` of source and target node features, e.g. for a single relation of a heterogeneous graph.
In the latter case ``\mathbf{x}_j`` is taken from `xj` and ``\mathbf{x}_i`` from `xi` in the
formulas above, so `xj` and `xi` must have the same number of features.
Returns a matrix of size `[in, num_target_nodes]`.

# Examples:

Expand All @@ -382,6 +394,7 @@ rng = Random.default_rng()
s = [1,1,2,3]
t = [2,3,1,1]
g = GNNGraph(s, t)
x = randn(rng, Float32, 3, g.num_nodes)

# create layer
l = AGNNConv(init_beta=2.0f0)
Expand Down Expand Up @@ -421,7 +434,7 @@ function Base.show(io::IO, l::AGNNConv)
print(io, ")")
end

function (l::AGNNConv)(g, x::AbstractMatrix, ps, st)
function (l::AGNNConv)(g, x, ps, st)
β = l.trainable ? ps.β : l.init_beta
m = (; β, l.add_self_loops)
return GNNlib.agnn_conv(m, g, x), st
Expand Down
9 changes: 9 additions & 0 deletions GNNLux/test/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ end
@testset "AGNNConv" begin
l = AGNNConv(init_beta=1.0f0)
test_lux_layer(rng, l, g, x, sizey=(in_dims, 10))

# bipartite input vs homogeneous layer on the graph with nodes A ∪ B
s, t = [1, 1, 2, 2], [1, 2, 2, 3]
hg = GNNHeteroGraph((:A, :to, :B) => (s, t), num_nodes = Dict(:A => 2, :B => 3))
xA, xB = randn(rng, Float32, in_dims, 2), randn(rng, Float32, in_dims, 3)
ps, st = LuxCore.setup(rng, l)
y, _ = l(hg, (xA, xB), ps, st)
ŷ, _ = AGNNConv(init_beta=1.0f0, add_self_loops=false)(GNNGraph(s, t .+ 2, num_nodes = 5), [xA xB], ps, st)
@test y ≈ ŷ[:, 3:5]
end

@testset "EdgeConv" begin
Expand Down
17 changes: 12 additions & 5 deletions GNNlib/src/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 +340,25 @@ end

####################### AGNNConv ######################################

function agnn_conv(l, g::GNNGraph, x::AbstractMatrix)
function agnn_conv(l, g::AbstractGNNGraph, x)
check_num_nodes(g, x)
if l.add_self_loops
g = add_self_loops(g)
end

xn = x ./ sqrt.(sum(x .^ 2, dims = 1))
cos_dist = apply_edges(xi_dot_xj, g, xi = xn, xj = xn)
xj, xi = expand_srcdst(g, x)

xi_n = xi ./ sqrt.(sum(xi .^ 2, dims = 1))
if xj !== xi
xj_n = xj ./ sqrt.(sum(xj .^ 2, dims = 1))
else
xj_n = xi_n
end
cos_dist = apply_edges(xi_dot_xj, g, xi = xi_n, xj = xj_n)
α = softmax_edge_neighbors(g, l.β .* cos_dist)

x = propagate(g, +; xj = x, e = α) do xi, xj, α
α .* xj
x = propagate(g, +; xj, e = α) do xi, xj, α
α .* xj
end

return x
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
2 changes: 1 addition & 1 deletion GraphNeuralNetworks/docs/src/api/conv.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The table below lists all graph convolutional layers implemented in the *GraphNe

| Layer |Sparse Ops|Edge Weight|Edge Features| Heterograph | TemporalSnapshotsGNNGraphs |
| :-------- | :---: |:---: |:---: | :---: | :---: |
| [`AGNNConv`](@ref) | | | ✓ | | |
| [`AGNNConv`](@ref) | | | | ✓ | |
| [`CGConv`](@ref) | | | ✓ | ✓ | ✓ |
| [`ChebConv`](@ref) | | | | | ✓ |
| [`EGNNConv`](@ref) | | | ✓ | | |
Expand Down
13 changes: 13 additions & 0 deletions GraphNeuralNetworks/src/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,18 @@ and ``\beta`` a trainable parameter if `trainable=true`.
- `init_beta`: The initial value of ``\beta``. Default 1.0f0.
- `trainable`: If true, ``\beta`` is trainable. Default `true`.
- `add_self_loops`: Add self loops to the graph before performing the convolution. Default `true`.
No self loops are added to a relation of a heterogeneous graph whose source
and target node types differ.

# Forward

(::AGNNConv)(g, x) -> AbstractMatrix

Takes as input a graph `g` and a node feature matrix `x` of size `[in, num_nodes]`, or a tuple
`(xj, xi)` of source and target node features, e.g. for a relation of a heterogeneous graph
(see [`HeteroGraphConv`](@ref)). In the latter case ``\mathbf{x}_j`` is taken from `xj` and
``\mathbf{x}_i`` from `xi` in the formulas above, so `xj` and `xi` must have the same number of features.
Returns a matrix of size `[in, num_target_nodes]`.

# Examples:

Expand All @@ -977,6 +989,7 @@ and ``\beta`` a trainable parameter if `trainable=true`.
s = [1,1,2,3]
t = [2,3,1,1]
g = GNNGraph(s, t)
x = randn(Float32, 3, g.num_nodes)

# create layer
l = AGNNConv(init_beta=2.0f0)
Expand Down
24 changes: 24 additions & 0 deletions GraphNeuralNetworks/test/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,30 @@ end
@test size(l(g, g.x)) == (D_IN, g.num_nodes)
test_gradients(l, g, g.x, rtol = RTOL_HIGH)
end

function agnn_ref(g, xj, xi, β)
s, t = edge_index(g)
cosine(a, b) = sum(a .* b) / sqrt(sum(abs2, a) * sum(abs2, b))
y = zeros(Float32, size(xj, 1), size(xi, 2))
for i in axes(xi, 2)
js = s[t .== i]
w = [exp(β * cosine(xi[:, i], xj[:, j])) for j in js]
for (wk, j) in zip(w, js)
y[:, i] .+= wk / sum(w) .* xj[:, j]
end
end
return y
end

for g in TEST_GRAPHS
@test l(g, g.x) ≈ agnn_ref(add_self_loops(g), g.x, g.x, 2.0f0)
end

g = TEST_GRAPHS[2]
xj, xi = randn(Float32, D_IN, g.num_nodes), randn(Float32, D_IN, g.num_nodes)
@test l(g, (xj, xi)) ≈ agnn_ref(add_self_loops(g), xj, xi, 2.0f0)
l = AGNNConv(init_beta = 2.0f0, add_self_loops = false)
@test l(g, (xj, xi)) ≈ agnn_ref(g, xj, xi, 2.0f0)
end

@testitem "AGNNConv GPU" setup=[TolSnippet, TestModule] tags=[:gpu] begin
Expand Down
21 changes: 21 additions & 0 deletions GraphNeuralNetworks/test/layers/heteroconv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,25 @@
y = layers(g, x);
@test size(y.A) == (2,2) && size(y.B) == (2,3)
end

@testset "AGNNConv" begin
x = (A = rand(Float32, 4, 2), B = rand(Float32, 4, 3))
layers = HeteroGraphConv((:A, :to, :B) => AGNNConv(init_beta = 2.0f0),
(:B, :to, :A) => AGNNConv(init_beta = 2.0f0))
y = layers(hg, x)
# reference: homogeneous layer on the graph with nodes A ∪ B;
# no self loops are added to relations between different node types
sAB, tAB = edge_index(hg, (:A, :to, :B))
sBA, tBA = edge_index(hg, (:B, :to, :A))
g = GNNGraph([sAB; sBA .+ 2], [tAB .+ 2; tBA], num_nodes = 5)
ŷ = AGNNConv(init_beta = 2.0f0, add_self_loops = false)(g, [x.A x.B])
@test y.A ≈ ŷ[:, 1:2]
@test y.B ≈ ŷ[:, 3:5]

s, t = [1, 2, 3, 3], [2, 3, 1, 2]
hg_AA = GNNHeteroGraph((:A, :to, :A) => (s, t))
x = rand(Float32, 4, 3)
y = HeteroGraphConv((:A, :to, :A) => AGNNConv())(hg_AA, (; A = x))
@test y.A ≈ AGNNConv()(GNNGraph(s, t), x)
end
end