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/docs/src/api/conv.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | | | ✓ | | |
Expand Down
17 changes: 17 additions & 0 deletions GNNLux/src/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions GNNLux/test/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions GNNlib/src/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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) | | | ✓ | | |
Expand Down
17 changes: 17 additions & 0 deletions GraphNeuralNetworks/src/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions GraphNeuralNetworks/test/layers/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
39 changes: 39 additions & 0 deletions GraphNeuralNetworks/test/layers/heteroconv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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