diff --git a/GNNLux/Project.toml b/GNNLux/Project.toml index 06eed6dfd..893b2782c 100644 --- a/GNNLux/Project.toml +++ b/GNNLux/Project.toml @@ -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" diff --git a/GNNLux/docs/src/api/conv.md b/GNNLux/docs/src/api/conv.md index 146510d05..2e4a00b9f 100644 --- a/GNNLux/docs/src/api/conv.md +++ b/GNNLux/docs/src/api/conv.md @@ -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) | | | ✓ | | | diff --git a/GNNLux/src/layers/conv.jl b/GNNLux/src/layers/conv.jl index 050fc18a8..9c8187a7b 100644 --- a/GNNLux/src/layers/conv.jl +++ b/GNNLux/src/layers/conv.jl @@ -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: @@ -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) @@ -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 diff --git a/GNNLux/test/layers/conv.jl b/GNNLux/test/layers/conv.jl index 49c857446..4e7c9113e 100644 --- a/GNNLux/test/layers/conv.jl +++ b/GNNLux/test/layers/conv.jl @@ -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 diff --git a/GNNlib/src/layers/conv.jl b/GNNlib/src/layers/conv.jl index ab40f13f2..5ae152ba4 100644 --- a/GNNlib/src/layers/conv.jl +++ b/GNNlib/src/layers/conv.jl @@ -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 diff --git a/GraphNeuralNetworks/Project.toml b/GraphNeuralNetworks/Project.toml index 61a35a518..eff1c2b24 100644 --- a/GraphNeuralNetworks/Project.toml +++ b/GraphNeuralNetworks/Project.toml @@ -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"] @@ -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" diff --git a/GraphNeuralNetworks/docs/src/api/conv.md b/GraphNeuralNetworks/docs/src/api/conv.md index 0418dc6e2..41836f136 100644 --- a/GraphNeuralNetworks/docs/src/api/conv.md +++ b/GraphNeuralNetworks/docs/src/api/conv.md @@ -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) | | | ✓ | | | diff --git a/GraphNeuralNetworks/src/layers/conv.jl b/GraphNeuralNetworks/src/layers/conv.jl index 10a340e90..d1af5df1d 100644 --- a/GraphNeuralNetworks/src/layers/conv.jl +++ b/GraphNeuralNetworks/src/layers/conv.jl @@ -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: @@ -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) diff --git a/GraphNeuralNetworks/test/layers/conv.jl b/GraphNeuralNetworks/test/layers/conv.jl index 7585b817d..2add3b992 100644 --- a/GraphNeuralNetworks/test/layers/conv.jl +++ b/GraphNeuralNetworks/test/layers/conv.jl @@ -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 diff --git a/GraphNeuralNetworks/test/layers/heteroconv.jl b/GraphNeuralNetworks/test/layers/heteroconv.jl index 4a6d40ca3..8b3b1b58f 100644 --- a/GraphNeuralNetworks/test/layers/heteroconv.jl +++ b/GraphNeuralNetworks/test/layers/heteroconv.jl @@ -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