TorchLean API

NN.Spec.Layers.Gnn

Graph neural network layers (spec layer) #

We provide a couple of small, standard GNN building blocks that show up in lots of papers and PyTorch GNN libraries:

Message passing (the common core idea) #

Most GNN layers have the same shape of computation:

In this file the aggregation step is written with a matrix A : (n×n):

Agg(A, H) = A · H.

This captures many common conventions:

GCN layer (one very common choice) #

We model a GCN-style layer as:

H' = A · H · W + b

where:

PyTorch mental picture:

Why this file defines only these two:

def Spec.messagePassingSpec {α : Type} [TorchLean.Storage α] [Context α] {n inDim : } (A : TorchLean.Tensor α [n, n]) (x : TorchLean.Tensor α [n, inDim]) :

Neighbor aggregation / message passing via a graph matrix: Agg(A, X) = A · X.

This is the reusable "mix neighbors" step. The semantics are entirely determined by A (raw adjacency, normalized adjacency, weighted adjacency, etc.).

Instances For
    def Spec.messagePassingBackwardSpec {α : Type} [TorchLean.Storage α] [Context α] {n inDim : } (A : TorchLean.Tensor α [n, n]) (x dY : TorchLean.Tensor α [n, inDim]) :

    Backward/VJP for messagePassingSpec: returns (dA, dX).

    Instances For
      structure Spec.GCNLayerSpec (n inDim outDim : ) (α : Type) [TorchLean.Storage α] :

      Parameters/data for a single GCN-style layer.

      We bundle A with the layer because many code paths treat A as a fixed input per graph, while others treat it as a parameter (e.g. learned normalization). Keeping it in the record makes both uses explicit.

      Instances For
        def Spec.gcnLayerSpec {α : Type} [TorchLean.Storage α] [Context α] {n inDim outDim : } (layer : GCNLayerSpec n inDim outDim α) (x : TorchLean.Tensor α [n, inDim]) :

        Forward spec for a GCN-style layer: Y = A · X · W + b.

        Notes:

        • The bias b is broadcast across the n nodes (row-wise add).
        • Any normalization/self-loop convention belongs in the choice of A supplied to the layer.
        Instances For

          Gradients #

          For the simple GCN-style layer

          Y = A · X · W + b

          the reverse-mode derivatives are the standard matrix calculus ones:

          We include dA because in some setups the adjacency/normalization is also:

          structure Spec.GCNLayerParameterGradients (n inDim outDim : ) (α : Type) [TorchLean.Storage α] :

          Parameter gradients for a GCNLayerSpec.

          The adjacency gradient sits with the weight and bias gradients because a GCN layer can be trained with learned edge weights, in which case A really is a parameter; when it is a fixed normalization the field is simply ignored.

          Instances For
            structure Spec.GCNLayerGradients (n inDim outDim : ) (α : Type) [TorchLean.Storage α] :

            Everything a GCN layer's backward pass produces: the parameter gradients plus the gradient travelling on to the node features.

            Instances For
              def Spec.gcnLayerBackwardSpec {α : Type} [TorchLean.Storage α] [Context α] {n inDim outDim : } (layer : GCNLayerSpec n inDim outDim α) (x : TorchLean.Tensor α [n, inDim]) (gradOutput : TorchLean.Tensor α [n, outDim]) (h_n : n 0) :
              GCNLayerGradients n inDim outDim α

              Backward/VJP spec for gcnLayerSpec.

              Instances For