Repository navigation
TAGConv: one weight matrix per hop, including hop 0 (breaking) - #760
Open
parth-patel010 wants to merge 1 commit into
Open
parth-patel010 wants to merge 1 commit into
parth-patel010 wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #752.
This is a breaking change.
TAGConv's weight goes from one(out, in)matrix to an(out, in, k + 1)array, so savedTAGConvmodels (e.g.Flux.state/ JLD2 checkpoints) will not load into the new layer, and code that reads or setsl.weightas 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
with a separate weight
Θ_kfor each hop.GNNlib.tag_convdid something else:X Θ_0was never added;iaddedW * (Âx + … + Âⁱx), so the output wasW (K Âx + (K-1) ²x + … + Âᴷx).As a result,
TAGConv(in => out, 1; bias = false)gave exactly the same output asGCNConv(in => out; bias = false)with the same weight.Change
GNNlib/src/layers/conv.jl,tag_conv: start from the hop-0 terml.weight[:, :, 1] * x, then for each hopk = 1:Kpropagate once more (same normalisation, self-loop and edge-weight handling as before) and addl.weight[:, :, k + 1] * Âᵏx. It asserts that the weight hask + 1slices, so an old-layout layer fails with a clear message instead of aBoundsError.GraphNeuralNetworks/src/layers/conv.jl,TAGConv: the weight is(out, in, k + 1), built ask + 1separateinit(out, in)matrices, so each hop is initialised like a dense layer, as PyG does with itsK + 1Linears, and a custominitis still called with two dimensions. The struct's type parameter is nowAbstractArray{<:Any, 3}.showis unchanged (TAGConv(3 => 2, 2)). The docstring now says howΘ_kmaps tol.weight, and the example uses the positionalk(TAGConv(3 => 5, k=3; ...)threw, sincekis not a keyword).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'sK + 1separate 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'sreshape(weight, out, :)of this array, as the example below checks. If you'd prefer another layout, or the field renamed toweightslikeDConv, 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):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)1.04969081.4901161e-81.65145371.1920929e-72.19050931.1920929e-7TAGConv(k = 1)vsGCNConvwith the same Θ₁ (bothbias = false)0.0(identical)1.4060614(differs by the hop-0 term)(out, (K+1)*in)matrix on[x; Âx; ²x], k = 25.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, copyingweight[:, :, k + 1]intoconv.lins[k].weight, on a directed graph with 4 nodes and 8 edges. PyG'sTAGConvdoesn't add self-loops itself, so for the defaultadd_self_loops = trueI passed itadd_self_loops(edge_index):Tests
A new
@testitem "TAGConv values"inGraphNeuralNetworks/test/layers/conv.jlchecks 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), fork = 1, 2, 3:use_edge_weight = truewith the weights on the graph, and with the weights passed as the third argument;add_self_loops = false;Θ_1…Θ_Kzeroed, the output is the plain linear mapΘ_0 x + b;show.On master this item gives 1 pass, 6 failed, 12 errored (the values don't match, and
weight[:, :, k + 1]is aBoundsErroron a matrix). With this PR, theTAGConvitems, including the existing shape andtest_gradientschecks (Zygote, Mooncake, and Enzyme on:sparse), plus the edge-weight gradient item that usesTAGConv:Julia 1.13.3, CPU. I couldn't run the
:gpuitem here; the new code only uses matrix products, slicing and the samepropagatecalls 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_convnow also expects the new layout, and the released GraphNeuralNetworks 1.1.1 hasGNNlib = "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 touchedCHANGELOG.md, so here is a line for it: "Breaking:TAGConvnow uses a separate weight matrix for each hop0:k(weightof size(out, in, k + 1)) and computesΣ_k Θ_k Âᵏ xas documented; it previously used one shared weight, skipped hop 0 and summed the powers cumulatively. SavedTAGConvparameters from earlier versions are not compatible."