GraphIsomorphismNetwork<T>

Models & Types · AiDotNet.NeuralNetworks

Represents a Graph Isomorphism Network (GIN) for powerful graph representation learning.

Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → GraphModelLayoutBase<T> → GraphIsomorphismNetwork<T>

For Beginners

GIN is optimal for structural graph understanding.

How it works:

  • Sum neighbor features (preserves multiset information)
  • Combine with self features using learnable weighting (1 + epsilon)
  • Transform through a 2-layer MLP
  • Result: maximally expressive graph representation

Example - Chemical Structure Analysis:

  • Distinguishing molecules with subtle structural differences
  • GIN can tell apart molecules that simpler GNNs confuse
  • Critical for drug discovery where small differences matter

Key Features:

  • Provably powerful: As expressive as WL test
  • Learnable epsilon: Optimizes self vs neighbor weighting
  • Two-layer MLP: Provides non-linear transformation capacity
  • Sum aggregation: Preserves structural information

Why GIN is powerful:

  • Mean/max pooling loses information (e.g., can’t distinguish {1,1,1} from {1})
  • Sum aggregation preserves multiset: {1,1,1} != {1}
  • MLP can approximate complex functions
  • Learnable epsilon finds optimal self-weighting

Architecture:

  1. Multiple GIN layers with sum aggregation
  2. Each layer has learnable epsilon and 2-layer MLP
  3. Optional graph-level readout for classification

When to use GIN:

  • When structural differentiation is critical
  • Molecular property prediction
  • Chemical compound classification
  • Any task where graph structure similarity matters

How It Works

Graph Isomorphism Networks (GIN), introduced by Xu et al. (2019), are provably as powerful as the Weisfeiler-Lehman (WL) graph isomorphism test for distinguishing graph structures. GIN uses sum aggregation with a learnable epsilon parameter and applies a multi-layer perceptron (MLP) for powerful feature transformation.

Example

var architecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 4);
var nodeFeatures = Tensor<float>.CreateRandom(new[] { 20, 16 });
var trainX = Tensor<float>.CreateRandom(4, 8);
var trainY = Tensor<float>.CreateRandom(4, 2);
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
    .ConfigureModel(new GraphIsomorphismNetwork<float>(architecture))
    .Build(trainX, trainY);
var output = result.Predict(nodeFeatures);

Constructors

Constructor Summary
GraphIsomorphismNetwork() Initializes a new instance of the GraphIsomorphismNetwork class with specified architecture.
GraphIsomorphismNetwork(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, ILearningRateScheduler learningRateScheduler, GraphIsomorphismNetworkOptions options) Initializes the model from an already-resolved options instance.

Properties

Property Summary
bool IsLoRAEnabled { get; set; } Gets whether LoRA fine-tuning is currently enabled.
bool LearnEpsilon { get; } Gets whether epsilon is learnable in GIN layers.
double InitialEpsilon { get; } Gets the initial epsilon value for GIN layers.
int LoRARank { get; set; } Gets the LoRA rank when LoRA is enabled.
int MlpHiddenDim { get; } Gets the hidden dimension size for MLP in each layer.
int NumLayers { get; } Gets the number of GIN layers in the network.

Methods

Method Summary
Dictionary<string, Tensor<T>> GetNamedLayerActivations(Tensor<T> input) Gets the intermediate activations from each layer, ensuring adjacency is set for graph layers.
ModelMetadata<T> GetModelMetadata() Gets metadata about this model.
Tensor<T> Forward(Tensor<T> nodeFeatures, Tensor<T> adjacencyMatrix) Performs a forward pass through the network with node features and adjacency matrix.
Tensor<T> ForwardForTraining(Tensor<T> input)
Tensor<T> GetGraphRepresentation(Tensor<T> nodeFeatures, Tensor<T> adjacencyMatrix) Gets graph-level representations using sum, mean, and max pooling combined.
double Evaluate(Tensor<T> nodeFeatures, Tensor<T> adjacencyMatrix, Tensor<T> labels, bool[] testMask) Evaluates the model on test data and returns accuracy for node classification.
double EvaluateGraphs(List<Tensor<T>> graphs, List<Tensor<T>> adjacencyMatrices, Tensor<T> graphLabels) Evaluates the model on test graphs and returns accuracy for graph classification.
int GetLoRAParameterCount() Gets the number of trainable LoRA parameters.
long GetParameterCount() Gets the total number of trainable parameters in the network.
void DisableLoRA() Disables LoRA fine-tuning and restores original layers.
void EnableLoRAFineTuning(int rank, double alpha, bool freezeBaseLayers) Enables LoRA fine-tuning for parameter-efficient training.
void MergeLoRAWeights() Merges LoRA weights into the base layers and disables LoRA mode.
void SetAdjacencyMatrix(Tensor<T> adjacencyMatrix) Sets the adjacency matrix for graph operations.
void TrainOnGraph(Tensor<T> nodeFeatures, Tensor<T> adjacencyMatrix, Tensor<T> labels, bool[] trainMask, int epochs, double learningRate) Trains the GIN network on a single graph with node classification.
void TrainOnGraphs(List<Tensor<T>> graphs, List<Tensor<T>> adjacencyMatrices, Tensor<T> graphLabels, int epochs, double learningRate) Trains the GIN network on multiple graphs for graph classification.