MessagePassingLayer<T>

Layers · AiDotNet.NeuralNetworks.Layers

Implements a general Message Passing Neural Network (MPNN) layer.

Inheritance: LayerBase<T> → MessagePassingLayer<T>

Implements: IGraphConvolutionLayer<T>, IShapeContract

For Beginners

Think of message passing like spreading information through a network.

Imagine a social network where:

  1. Message: Each friend sends you a message (combining their info with yours)
  2. Aggregate: You collect and summarize all messages from friends
  3. Update: You update your own status based on the summary

This happens for all people simultaneously, allowing information to flow through the network.

Use cases:

  • Molecule analysis: Atoms sharing information about chemical bonds
  • Social networks: Users influenced by their connections
  • Citation networks: Papers learning from papers they cite
  • Recommendation systems: Items learning from similar items

How It Works

Message Passing Neural Networks provide a general framework for graph neural networks. The framework consists of three key functions:

  1. Message: Computes messages from neighbors
  2. Aggregate: Combines messages from all neighbors
  3. Update: Updates node representations using aggregated messages

The layer performs the following computation for each node v:

  • m_v = AGGREGATE({MESSAGE(h_u, h_v, e_uv) : u ∈ N(v)})
  • h_v’ = UPDATE(h_v, m_v)

where h_v are node features, e_uv are edge features, and N(v) is the neighborhood of v.

Constructors

Constructor Summary
MessagePassingLayer(int inputFeatures, int outputFeatures, int messageFeatures, bool useEdgeFeatures, int edgeFeatureDim, IActivationFunction<T> activationFunction) Initializes a new instance of the MessagePassingLayer class.

Properties

Property Summary
bool SupportsTraining { get; }
bool UsesSparseAggregation { get; } Gets whether sparse (edge-based) aggregation is currently enabled.
int InputFeatures { get; }
int OutputFeatures { get; }

Methods

Method Summary
Dictionary<string, string> GetParameterRoles() Returns parameter roles for per-role optimizer configuration (e.g., weight decay exemption for biases).
IReadOnlyList<OutputAxisContract> OutputAxesFor(int inputRank)
IReadOnlyList<Tensor<T>> GetTrainableParameters() Returns all trainable parameter tensors marked with [TrainableParameter].
IReadOnlyList<ValueTuple<string, Tensor<T>>> GetRegisteredBuffers()
List<Tensor<T>> GetParameterTensors() Gets all trainable parameters as a list of tensors.
Tensor<T> GetAdjacencyMatrix()
void ClearEdges() Clears the edge list and switches back to dense adjacency matrix aggregation.
void ClearGradients()
void ResetState()
void SetAdjacencyMatrix(Tensor<T> adjacencyMatrix)
void SetEdgeFeatures(Tensor<T> edgeFeatures) Sets the edge features tensor.
void SetEdges(Tensor<int> sourceIndices, Tensor<int> targetIndices) Sets the edge list representation of the graph structure for sparse aggregation.
void SetParameterTensors(List<Tensor<T>> parameters) Sets all trainable parameters from a list of tensors.
void SetTrainableParameters(IReadOnlyList<Tensor<T>> parameters) Replaces trainable parameter tensors (e.g., with ParameterBuffer views).
void UpdateParameters(T learningRate)
void ZeroGrad() Clears all gradient fields discovered by convention ({paramName}Gradient).