diff --git a/GNNLux/docs/src/api/conv.md b/GNNLux/docs/src/api/conv.md index 2e4a00b9f..ae106648a 100644 --- a/GNNLux/docs/src/api/conv.md +++ b/GNNLux/docs/src/api/conv.md @@ -24,7 +24,7 @@ The table below lists all graph convolutional layers implemented in the *GNNLux. | [`EdgeConv`](@ref) | | | | ✓ | | | [`GATConv`](@ref) | | | ✓ | ✓ | ✓ | | [`GATv2Conv`](@ref) | | | ✓ | ✓ | ✓ | -| [`GatedGraphConv`](@ref) | ✓ | | | | ✓ | +| [`GatedGraphConv`](@ref) | ✓ | | | ✓ | ✓ | | [`GCNConv`](@ref) | ✓ | ✓ | | ✓ | | | [`GINConv`](@ref) | ✓ | | | ✓ | ✓ | | [`GMMConv`](@ref) | | | ✓ | | | diff --git a/GNNLux/src/layers/conv.jl b/GNNLux/src/layers/conv.jl index f46cd01e6..6a7be0664 100644 --- a/GNNLux/src/layers/conv.jl +++ b/GNNLux/src/layers/conv.jl @@ -1232,6 +1232,23 @@ where ``\mathbf{h}^{(l)}_i`` denotes the ``l``-th hidden variables passing throu - `aggr`: Aggregation operator for the incoming messages (e.g. `+`, `*`, `max`, `min`, and `mean`). - `init_weight`: Weights' initializer. Default `glorot_uniform`. +# Forward + + (::GatedGraphConv)(g, x, ps, st) -> (y, st) + +- `g`: A `GNNGraph`, or a `GNNHeteroGraph` with a single edge type. +- `x`: A node feature matrix of size `(in, num_nodes)` with `in <= out`, + or a tuple `(xj, xi)` of source and target node features of sizes `(in_j, num_src_nodes)` and `(in_i, num_dst_nodes)`, + with `in_j, in_i <= out`. +- `ps`, `st`: The parameters and state of the layer. + +Returns the target node features `y` of size `(out, num_dst_nodes)` and the state. + +When `xj` and `xi` are distinct arrays (e.g. source and target nodes of different types), +the source nodes have no hidden state evolving in the recursion: their padded features ``[\mathbf{x}_j; \mathbf{0}]`` +are used in place of ``\mathbf{h}^{(l-1)}_j`` at every step, while the target states ``\mathbf{h}^{(l)}_i`` evolve through the GRU. +If `xj` and `xi` are the same array, the source states evolve as well, as in the homogeneous case. + # Examples: ```julia diff --git a/GNNLux/test/layers/conv.jl b/GNNLux/test/layers/conv.jl index 27e5d299d..a44e0df77 100644 --- a/GNNLux/test/layers/conv.jl +++ b/GNNLux/test/layers/conv.jl @@ -115,6 +115,12 @@ end @testset "GatedGraphConv" begin l = GatedGraphConv(in_dims, 3) test_lux_layer(rng, l, g, x, outputsize=(in_dims,)) + + ps, st = LuxCore.setup(rng, l) + @test l(g, (x, x), ps, st)[1] == l(g, x, ps, st)[1] + sg = edge_type_subgraph(rand_bipartite_heterograph(rng, (4, 6), 10), (:A, :to, :B)) + y, _ = l(sg, (randn(rng, Float32, 2, 4), randn(rng, Float32, in_dims, 6)), ps, st) + @test size(y) == (in_dims, 6) end @testset "GINConv" begin diff --git a/GNNlib/src/layers/conv.jl b/GNNlib/src/layers/conv.jl index b62938454..cec87d8e3 100644 --- a/GNNlib/src/layers/conv.jl +++ b/GNNlib/src/layers/conv.jl @@ -215,17 +215,21 @@ end ####################### GatedGraphConv ###################################### -function gated_graph_conv(l, g::GNNGraph, x::AbstractMatrix) - check_num_nodes(g, x) +function _gated_graph_pad(x, dims) m, n = size(x) - @assert m <= l.dims "number of input features must be less or equal to output features." - if m < l.dims - xpad = zeros_like(x, (l.dims - m, n)) - x = vcat(x, xpad) - end - h = x + @assert m <= dims "number of input features must be less or equal to output features." + return m < dims ? vcat(x, zeros_like(x, (dims - m, n))) : x +end + +function gated_graph_conv(l, g::AbstractGNNGraph, x) + check_num_nodes(g, x) + xj, xi = expand_srcdst(g, x) + h = _gated_graph_pad(xi, l.dims) + # Distinct source features (e.g. another node type) have no hidden state in the recursion. + fixed_src = xj !== xi + hj = fixed_src ? _gated_graph_pad(xj, l.dims) : h for i in 1:(l.num_layers) - m = view(l.weight, :, :, i) * h + m = view(l.weight, :, :, i) * (fixed_src ? hj : h) m = propagate(copy_xj, g, l.aggr; xj = m) _, h = l.gru(m, h) end diff --git a/GraphNeuralNetworks/docs/src/api/conv.md b/GraphNeuralNetworks/docs/src/api/conv.md index 41836f136..fc89b8f48 100644 --- a/GraphNeuralNetworks/docs/src/api/conv.md +++ b/GraphNeuralNetworks/docs/src/api/conv.md @@ -25,7 +25,7 @@ The table below lists all graph convolutional layers implemented in the *GraphNe | [`EdgeConv`](@ref) | | | | ✓ | | | [`GATConv`](@ref) | | | ✓ | ✓ | ✓ | | [`GATv2Conv`](@ref) | | | ✓ | ✓ | ✓ | -| [`GatedGraphConv`](@ref) | ✓ | | | | ✓ | +| [`GatedGraphConv`](@ref) | ✓ | | | ✓ | ✓ | | [`GCNConv`](@ref) | ✓ | ✓ | | ✓ | | | [`GINConv`](@ref) | ✓ | | | ✓ | ✓ | | [`GMMConv`](@ref) | | | ✓ | | | diff --git a/GraphNeuralNetworks/src/layers/conv.jl b/GraphNeuralNetworks/src/layers/conv.jl index bd0c78aa6..2bbba36b9 100644 --- a/GraphNeuralNetworks/src/layers/conv.jl +++ b/GraphNeuralNetworks/src/layers/conv.jl @@ -495,6 +495,23 @@ where ``\mathbf{h}^{(l)}_i`` denotes the ``l``-th hidden variables passing throu - `aggr`: Aggregation operator for the incoming messages (e.g. `+`, `*`, `max`, `min`, and `mean`). - `init`: Weight initialization function. +# Forward + + (::GatedGraphConv)(g, x) -> AbstractMatrix + +- `g`: A `GNNGraph`, or a `GNNHeteroGraph` with a single edge type (e.g. within a [`HeteroGraphConv`](@ref)). +- `x`: A node feature matrix of size `(in, num_nodes)` with `in <= out`, + or a tuple `(xj, xi)` of source and target node features of sizes `(in_j, num_src_nodes)` and `(in_i, num_dst_nodes)`, + with `in_j, in_i <= out`. + +Returns the target node features of size `(out, num_dst_nodes)`. + +When `xj` and `xi` are distinct arrays (e.g. source and target nodes of different types), +the source nodes have no hidden state evolving in the recursion: their padded features ``[\mathbf{x}_j; \mathbf{0}]`` +are used in place of ``\mathbf{h}^{(l-1)}_j`` at every step, while the target states ``\mathbf{h}^{(l)}_i`` evolve through the GRU. +If `xj` and `xi` are the same array (as passed by [`HeteroGraphConv`](@ref) for relations whose source and target +node types coincide), the source states evolve as well, as in the homogeneous case. + # Examples: ```julia diff --git a/GraphNeuralNetworks/test/layers/conv.jl b/GraphNeuralNetworks/test/layers/conv.jl index bd3a94318..bcb62f8ca 100644 --- a/GraphNeuralNetworks/test/layers/conv.jl +++ b/GraphNeuralNetworks/test/layers/conv.jl @@ -241,6 +241,21 @@ end @test size(l(g, g.x)) == (D_OUT, g.num_nodes) test_gradients(l, g, g.x, rtol = RTOL_HIGH) end + + @testset "reference" begin + for g in TEST_GRAPHS + s, t = edge_index(g) + h = vcat(g.x, zeros(Float32, D_OUT - D_IN, g.num_nodes)) + for k in 1:num_layers + m = zeros(Float32, D_OUT, g.num_nodes) + for (j, i) in zip(s, t) + m[:, i] .+= l.weight[:, :, k] * h[:, j] + end + _, h = l.gru(m, h) + end + @test l(g, g.x) ≈ h + end + end end diff --git a/GraphNeuralNetworks/test/layers/heteroconv.jl b/GraphNeuralNetworks/test/layers/heteroconv.jl index 8b3b1b58f..5614fcbd1 100644 --- a/GraphNeuralNetworks/test/layers/heteroconv.jl +++ b/GraphNeuralNetworks/test/layers/heteroconv.jl @@ -179,4 +179,43 @@ y = HeteroGraphConv((:A, :to, :A) => AGNNConv())(hg_AA, (; A = x)) @test y.A ≈ AGNNConv()(GNNGraph(s, t), x) end + + @testset "GatedGraphConv" begin + function gated_ref(l, s, t, xj, xi; evolve_src) + pad(x) = vcat(x, zeros(Float32, l.dims - size(x, 1), size(x, 2))) + h, hj = pad(xi), pad(xj) + for k in 1:l.num_layers + src = evolve_src ? h : hj + m = zeros(Float32, l.dims, size(h, 2)) + for (j, i) in zip(s, t) + m[:, i] .+= l.weight[:, :, k] * src[:, j] + end + _, h = l.gru(m, h) + end + return h + end + + eAB = ([1, 1, 2, 3], [1, 2, 2, 4]) + eBA = ([1, 2, 4, 4], [1, 1, 3, 2]) + eAA = ([1, 2, 3], [2, 3, 1]) + g = GNNHeteroGraph(((:A, :to, :B) => eAB, (:B, :to, :A) => eBA, (:A, :to, :A) => eAA); + num_nodes = Dict(:A => 3, :B => 4)) + x = (A = rand(Float32, 3, 3), B = rand(Float32, 2, 4)) + layers = HeteroGraphConv((:A, :to, :B) => GatedGraphConv(4, 3), + (:B, :to, :A) => GatedGraphConv(4, 3), + (:A, :to, :A) => GatedGraphConv(4, 3)) + y = layers(g, x) + @test size(y.A) == (4, 3) && size(y.B) == (4, 4) + + lAB, lBA, lAA = layers.layers + @test y.B ≈ gated_ref(lAB, eAB..., x.A, x.B; evolve_src = false) + @test y.A ≈ gated_ref(lBA, eBA..., x.B, x.A; evolve_src = false) + + gated_ref(lAA, eAA..., x.A, x.A; evolve_src = true) + + loss(x) = sum(layers(g, x).A) + sum(abs2, layers(g, x).B) + dx = gradient(loss, x)[1] + _, ndx = finitediff_withgradient(loss, x) + @test dx.A ≈ ndx[1].A rtol=1e-3 + @test dx.B ≈ ndx[1].B rtol=1e-3 + end end