diff --git a/GNNlib/src/layers/conv.jl b/GNNlib/src/layers/conv.jl index aff802a0b..ba70ac8bb 100644 --- a/GNNlib/src/layers/conv.jl +++ b/GNNlib/src/layers/conv.jl @@ -664,6 +664,9 @@ end ######################## TAGConv ###################################### +# TAGConv, Du et al. (2017), https://arxiv.org/abs/1710.10370: +# H = Σ_{k=0}^{K} Θ_k Âᵏ x, Â = D^{-1/2} A D^{-1/2} +# with one weight matrix per hop, `l.weight[:, :, k + 1]` for `Θ_k`, as in PyG's `TAGConv`. function tag_conv(l, g::GNNGraph, x::AbstractMatrix{T}, edge_weight::EW = nothing) where {T, EW <: Union{Nothing, AbstractVector}} @@ -672,6 +675,7 @@ function tag_conv(l, g::GNNGraph, x::AbstractMatrix{T}, if edge_weight !== nothing @assert length(edge_weight)==g.num_edges "Wrong number of edge weights (expected $(g.num_edges) but given $(length(edge_weight)))" end + @assert size(l.weight, 3) == l.k + 1 "TAGConv needs one weight matrix for each hop 0:k, i.e. a weight of size (out, in, k + 1)" if l.add_self_loops g = add_self_loops(g) @@ -680,7 +684,6 @@ function tag_conv(l, g::GNNGraph, x::AbstractMatrix{T}, @assert length(edge_weight) == g.num_edges end end - Dout, Din = size(l.weight) if edge_weight !== nothing d = degree(g, T; dir = :in, edge_weight) else @@ -688,9 +691,8 @@ function tag_conv(l, g::GNNGraph, x::AbstractMatrix{T}, end c = 1 ./ sqrt.(d) - sum_pow = 0 - sum_total = 0 - for iter in 1:(l.k) + h = l.weight[:, :, 1] * x # hop 0 + for k in 1:(l.k) x = x .* c' if edge_weight !== nothing x = propagate(e_mul_xj, g, +, xj = x, e = edge_weight) @@ -699,22 +701,11 @@ function tag_conv(l, g::GNNGraph, x::AbstractMatrix{T}, else x = propagate(copy_xj, g, +, xj = x) end - x = x .* c' - - # On the first iteration, initialize sum_pow with the first propagated features - # On subsequent iterations, accumulate propagated features - if iter == 1 - sum_pow = x - sum_total = l.weight * sum_pow - else - sum_pow += x - # Weighted sum of features for each power of adjacency matrix - # This applies the weight matrix to the accumulated sum of propagated features - sum_total += l.weight * sum_pow - end + x = x .* c' # x = Âᵏ x + h = h + l.weight[:, :, k + 1] * x end - return (sum_total .+ l.bias) + return h .+ l.bias end # when we also have edge_weight we need to convert the graph to COO diff --git a/GraphNeuralNetworks/src/layers/conv.jl b/GraphNeuralNetworks/src/layers/conv.jl index 7e5d190e5..e55be1f75 100644 --- a/GraphNeuralNetworks/src/layers/conv.jl +++ b/GraphNeuralNetworks/src/layers/conv.jl @@ -1303,6 +1303,8 @@ H^{K} = {\sum}_{k=0}^K (D^{-1/2} A D^{-1/2})^{k} X {\Theta}_{k} ``` where `A` is the adjacency matrix of the graph, `D` is the degree matrix, `X` is the input feature matrix, and ``{\Theta}_{k}`` is a unique weight matrix for each hop `k`. +With the `in × num_nodes` layout of `X` used here, the layer computes `Σ_k Θ_k Âᵏ X`, where `Θ_k = l.weight[:, :, k + 1]`: +the weight is an array of size `(out, in, k + 1)`. # Arguments - `in`: Number of input features. @@ -1323,13 +1325,13 @@ g = GNNGraph(s, t) # Create a graph x = randn(Float32, 3, g.num_nodes) # Random features for each node # Create a TAGConv layer -l = TAGConv(3 => 5, k=3; add_self_loops=true) +l = TAGConv(3 => 5, 3; add_self_loops=true) # Apply the TAGConv layer y = l(g, x) # Output size: 5 × num_nodes ``` """ -struct TAGConv{A <: AbstractMatrix, B} <: GNNLayer +struct TAGConv{A <: AbstractArray{<:Any, 3}, B} <: GNNLayer weight::A bias::B k::Int @@ -1345,7 +1347,8 @@ function TAGConv(ch::Pair{Int, Int}, k = 3; add_self_loops = true, use_edge_weight = false) in, out = ch - W = init(out, in) + # one `(out, in)` matrix for each hop `0:k`, each initialized like a dense layer's weight + W = stack(init(out, in) for _ in 0:k) b = bias ? Flux.create_bias(W, true, out) : false return TAGConv(W, b, k, add_self_loops, use_edge_weight) end @@ -1353,7 +1356,7 @@ end (l::TAGConv)(g, x, edge_weight = nothing) = GNNlib.tag_conv(l, g, x, edge_weight) function Base.show(io::IO, l::TAGConv) - out, in = size(l.weight) + out, in = size(l.weight, 1), size(l.weight, 2) print(io, "TAGConv($in => $out") l.k == 1 || print(io, ", ", l.k) print(io, ")") diff --git a/GraphNeuralNetworks/test/layers/conv.jl b/GraphNeuralNetworks/test/layers/conv.jl index 4704a2921..271b4b553 100644 --- a/GraphNeuralNetworks/test/layers/conv.jl +++ b/GraphNeuralNetworks/test/layers/conv.jl @@ -631,6 +631,44 @@ end end end +@testitem "TAGConv values" setup=[TolSnippet, TestModule] begin + using .TestModule + using LinearAlgebra + # https://github.com/JuliaGraphs/GraphNeuralNetworks.jl/issues/752 : + # y = Σ_{k=0}^K Θ_k Âᵏ x + b, with one weight matrix per hop + g = GNNGraph([1, 2, 2, 3, 3, 4, 1], [2, 1, 3, 2, 4, 3, 3]) + x = randn(3, g.num_nodes) + w = 0.5 .+ rand(g.num_edges) + function Â(g, w = ones(g.num_edges); self_loops = true) + s, t = edge_index(g) + A = zeros(g.num_nodes, g.num_nodes) + for (i, j, wij) in zip(s, t, w) + A[i, j] += wij + end + self_loops && (A += I) + d = vec(sum(A; dims = 1)) # in-degree + return Diagonal(1 ./ sqrt.(d)) * A * Diagonal(1 ./ sqrt.(d)) + end + # features are columns and `A[i, j]` is the edge `i → j`, so Âᵏ x in the paper's notation + # (rows of x aggregated over the in-neighbors) is `x * Âᵏ` here + dense(l, A, x) = sum(l.weight[:, :, k + 1] * x * A^k for k in 0:(l.k)) .+ l.bias + for k in (1, 2, 3) + l = Flux.f64(TAGConv(3 => 2, k)) + @test size(l.weight) == (2, 3, k + 1) + l.bias .= randn(2) + @test l(g, x) ≈ dense(l, Â(g), x) + lw = Flux.f64(TAGConv(3 => 2, k; use_edge_weight = true)) + @test lw(set_edge_weight(g, w), x) ≈ dense(lw, Â(g, w), x) + @test lw(g, x, w) ≈ dense(lw, Â(g, w), x) + l0 = Flux.f64(TAGConv(3 => 2, k; add_self_loops = false)) + @test l0(g, x) ≈ dense(l0, Â(g; self_loops = false), x) + # the hop-0 term is the plain linear map + l.weight[:, :, 2:end] .= 0 + @test l(g, x) ≈ l.weight[:, :, 1] * x .+ l.bias + end + @test sprint(show, TAGConv(3 => 2, 2)) == "TAGConv(3 => 2, 2)" +end + @testitem "TAGConv GPU" setup=[TolSnippet, TestModule] tags=[:gpu] begin using .TestModule k = 2