Skip to content

TAGConv: one weight matrix per hop, including hop 0 (breaking) - #760

Open
parth-patel010 wants to merge 1 commit into
JuliaGraphs:masterfrom
parth-patel010:fix/tagconv-per-hop-weights
Open

parth-patel010 wants to merge 1 commit into
JuliaGraphs:masterfrom
parth-patel010:fix/tagconv-per-hop-weights

Conversation

@parth-patel010

Copy link
Copy Markdown
Contributor

Fixes #752.

This is a breaking change. TAGConv's weight goes from one (out, in) matrix to an (out, in, k + 1) array, so saved TAGConv models (e.g. Flux.state / JLD2 checkpoints) will not load into the new layer, and code that reads or sets l.weight as a matrix needs updating. The outputs change too, because the old forward pass didn't compute the documented operation.

What was wrong

The docstring (and the TAGCN paper) define

$$H = \sum_{k=0}^{K} \hat{A}^{k} X \Theta_{k}, \qquad \hat{A} = D^{-1/2} A D^{-1/2}$$

with a separate weight Θ_k for each hop. GNNlib.tag_conv did something else:

  1. there was a single weight matrix for all hops;
  2. the hop-0 term X Θ_0 was never added;
  3. the powers were summed cumulatively: iteration i added W * (Âx + … + Âⁱx), so the output was W (K Âx + (K-1) ²x + … + Âᴷx).

As a result, TAGConv(in => out, 1; bias = false) gave exactly the same output as GCNConv(in => out; bias = false) with the same weight.

Change

  • GNNlib/src/layers/conv.jl, tag_conv: start from the hop-0 term l.weight[:, :, 1] * x, then for each hop k = 1:K propagate once more (same normalisation, self-loop and edge-weight handling as before) and add l.weight[:, :, k + 1] * Âᵏx. It asserts that the weight has k + 1 slices, so an old-layout layer fails with a clear message instead of a BoundsError.
  • GraphNeuralNetworks/src/layers/conv.jl, TAGConv: the weight is (out, in, k + 1), built as k + 1 separate init(out, in) matrices, so each hop is initialised like a dense layer, as PyG does with its K + 1 Linears, and a custom init is still called with two dimensions. The struct's type parameter is now AbstractArray{<:Any, 3}. show is unchanged (TAGConv(3 => 2, 2)). The docstring now says how Θ_k maps to l.weight, and the example uses the positional k (TAGConv(3 => 5, k=3; ...) threw, since k is not a keyword).
  • GNNLux has no TAGConv (it is commented out in the exports), so there's nothing to change there.

Weight layout. I went with (out, in, K + 1), Θ_k = weight[:, :, k + 1], because it reads directly as the docstring's per-hop Θ_k, matches PyG's K + 1 separate linear maps, and each slice is contiguous. DGL's layout, one (out, (K+1)*in) matrix applied to [x; Âx; …; Âᴷx], is the same thing: it's reshape(weight, out, :) of this array, as the example below checks. If you'd prefer another layout, or the field renamed to weights like DConv, I'm happy to change it.

Before / after

Path graph 1-2-3-4 (both directions), x = randn(Float32, 2, 4), Â = D^{-1/2}(A + I)D^{-1/2} (self-loops are on by default):

using GraphNeuralNetworks, LinearAlgebra, Random, Flux
Random.seed!(0)
g = GNNGraph([1, 2, 2, 3, 3, 4], [2, 1, 3, 2, 4, 3])
x = randn(Float32, 2, g.num_nodes)
A = Float32.(adjacency_matrix(g)) + I
d = vec(sum(A, dims = 1))
 = Diagonal(1 ./ sqrt.(d)) * A * Diagonal(1 ./ sqrt.(d))
Θ(l, k) = ndims(l.weight) == 3 ? l.weight[:, :, k + 1] : l.weight   # the single matrix on master
for k in 1:3
    l = TAGConv(2 => 3, k; bias = false)
    doc = sum(Θ(l, j) * x * Â^j for j in 0:k)    # Σ_j Θ_j Âʲ x, the docstring formula
    println("k = $k: size(weight) = ", size(l.weight), ", max|y - Σ_j Θ_j Âʲ x| = ", maximum(abs, l(g, x) - doc))
end
check master (4cab006) this PR
size(TAGConv(2 => 3, k).weight), k = 1 / 2 / 3 (3, 2) / (3, 2) / (3, 2) (3, 2, 2) / (3, 2, 3) / (3, 2, 4)
max |y − Σⱼ Θⱼ Âʲ x|, k = 1 1.0496908 1.4901161e-8
same, k = 2 1.6514537 1.1920929e-7
same, k = 3 2.1905093 1.1920929e-7
TAGConv(k = 1) vs GCNConv with the same Θ₁ (both bias = false) 0.0 (identical) 1.4060614 (differs by the hop-0 term)
DGL-style (out, (K+1)*in) matrix on [x; Âx; ²x], k = 2 n/a max diff 5.96e-8

(On master the docstring check uses the single weight for every Θ_j, so it measures the formula itself, not the weight count.)

PyG. I also compared against PyTorch Geometric 2.8.1's TAGConv(2, 3, K=3, bias=False, normalize=True) in Float64, copying weight[:, :, k + 1] into conv.lins[k].weight, on a directed graph with 4 nodes and 8 edges. PyG's TAGConv doesn't add self-loops itself, so for the default add_self_loops = true I passed it add_self_loops(edge_index):

add_self_loops = False: max |GraphNeuralNetworks - PyG TAGConv| = 4.441e-16
add_self_loops = True:  max |GraphNeuralNetworks - PyG TAGConv| = 2.220e-16

Tests

A new @testitem "TAGConv values" in GraphNeuralNetworks/test/layers/conv.jl checks the output against a dense Σ_{k=0}^K Θ_k Âᵏ x + b, with  built by hand from the edge list, on a small graph with one one-way edge (so  isn't symmetric), for k = 1, 2, 3:

  • default (self-loops added), with a random bias;
  • use_edge_weight = true with the weights on the graph, and with the weights passed as the third argument;
  • add_self_loops = false;
  • with Θ_1…Θ_K zeroed, the output is the plain linear map Θ_0 x + b;
  • the weight size, and show.

On master this item gives 1 pass, 6 failed, 12 errored (the values don't match, and weight[:, :, k + 1] is a BoundsError on a matrix). With this PR, the TAGConv items, including the existing shape and test_gradients checks (Zygote, Mooncake, and Enzyme on :sparse), plus the edge-weight gradient item that uses TAGConv:

cd GraphNeuralNetworks
julia --project=test -e 'using TestItemRunner; TestItemRunner.run_tests(pwd();
    filter = ti -> :gpu ∉ ti.tags && (occursin("TAGConv", ti.name) || occursin("edge weights", ti.name)))'
Test Summary:       | Pass  Total   Time
GraphNeuralNetworks |  121    121  4m10.4s

Julia 1.13.3, CPU. I couldn't run the :gpu item here; the new code only uses matrix products, slicing and the same propagate calls as before.

Versioning

I left the versions alone because that's a release decision, but you should know what this implies. Per semver this is a major change for GraphNeuralNetworks (the parameter shape changes). GNNlib.tag_conv now also expects the new layout, and the released GraphNeuralNetworks 1.1.1 has GNNlib = "1.4" in its compat. So if this went out as GNNlib 1.5, a GraphNeuralNetworks 1.1.1 install that picked up GNNlib 1.5 would hit the assertion above. Two options: release it as a major version of both packages, or keep a matrix-weight method in GNNlib with the old behaviour for older frontends. I can do either in this PR. Per AGENTS.md I haven't touched CHANGELOG.md, so here is a line for it: "Breaking: TAGConv now uses a separate weight matrix for each hop 0:k (weight of size (out, in, k + 1)) and computes Σ_k Θ_k Âᵏ x as documented; it previously used one shared weight, skipped hop 0 and summed the powers cumulatively. Saved TAGConv parameters from earlier versions are not compatible."

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TAGConv uses a single weight, drops the hop-0 term and sums propagation powers cumulatively

1 participant