TorchLean API

NN.Spec.Layers.Gnn

Graph neural network layers (spec layer) #

This file defines a GCN-style graph convolution layer. Neighbor aggregation uses the shared tensor matrix multiplication and its backward rule.

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:

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 α] [Add α] [Mul α] [Zero α] {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 α] [Add α] [Mul α] [Zero α] {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