GraphSAGENetwork<T>
Models & Types · AiDotNet.NeuralNetworks
Represents a GraphSAGE (Graph Sample and Aggregate) Network for inductive learning on graphs.
Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → GraphModelLayoutBase<T> → GraphSAGENetwork<T>
For Beginners
GraphSAGE learns how to combine neighbor information.
How it works:
- For each node, sample its neighbors
- Aggregate neighbor features using a learnable function
- Combine with node’s own features
- Result: new representation that captures local structure
Example - Social Network Recommendations:
- New user joins the platform (unseen during training)
- GraphSAGE can still make recommendations by:
- Looking at the new user’s connections
- Aggregating features from those connections
- Generating a representation for the new user
Key Features:
- Inductive: Can generalize to new, unseen nodes
- Scalable: Uses sampling, not full neighborhoods
- Flexible aggregators: Mean, MaxPool, or Sum
- L2 normalization: Optional for stable training
Aggregator Types:
- Mean: Average of neighbor features (most common)
- MaxPool: Element-wise max (captures salient features)
- Sum: Sum of neighbor features (preserves structure)
Architecture:
- Multiple GraphSAGE layers with different aggregators
- Optional L2 normalization between layers
- Final classification or regression head
When to use GraphSAGE:
- When new nodes appear frequently (evolving graphs)
- When you need to generalize to new graphs
- For large-scale graphs where full-batch training is infeasible
- Social networks, recommendation systems, dynamic graphs
How It Works
GraphSAGE, introduced by Hamilton et al. (2017), is designed for inductive learning on graphs. Unlike transductive methods that require all nodes during training, GraphSAGE learns aggregator functions that can generalize to completely unseen nodes and even new graphs.
Example
var nodeFeatures = Tensor<float>.CreateRandom(new[] { 50, 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 GraphSAGENetwork<float>())
.Build(trainX, trainY);
var output = result.Predict(nodeFeatures);
Constructors
| Constructor | Summary |
|---|---|
GraphSAGENetwork() |
Initializes a new instance of the GraphSAGENetwork class with specified architecture. |
GraphSAGENetwork(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, ILearningRateScheduler learningRateScheduler, GraphSAGEOptions options) |
Initializes the model from an already-resolved options instance. |
Properties
| Property | Summary |
|---|---|
SAGEAggregatorType AggregatorType { get; } |
Gets the aggregator type used in GraphSAGE layers. |
bool IsLoRAEnabled { get; set; } |
Gets whether LoRA fine-tuning is currently enabled. |
bool Normalize { get; } |
Gets whether L2 normalization is applied after each layer. |
double DropoutRate { get; } |
Gets the dropout rate applied during training. |
int HiddenDim { get; } |
Gets the hidden dimension size for each layer. |
int LoRARank { get; set; } |
Gets the LoRA rank when LoRA is enabled. |
int NumLayers { get; } |
Gets the number of GraphSAGE 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> GetNodeEmbeddings(Tensor<T> nodeFeatures, Tensor<T> adjacencyMatrix) |
Generates node embeddings using the trained network. |
double Evaluate(Tensor<T> nodeFeatures, Tensor<T> adjacencyMatrix, Tensor<T> labels, bool[] testMask) |
Evaluates the model on test data and returns accuracy. |
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 TrainMiniBatch(Tensor<T> nodeFeatures, Tensor<T> adjacencyMatrix, Tensor<T> labels, int[] trainIndices, int batchSize, int epochs, double learningRate, int numSamples) |
Performs mini-batch training using neighbor sampling for scalability. |
void TrainOnGraph(Tensor<T> nodeFeatures, Tensor<T> adjacencyMatrix, Tensor<T> labels, bool[] trainMask, int epochs, double learningRate) |
Trains the GraphSAGE network on graph-structured data. |