GraphNeuralNetwork<T>
Models & Types · AiDotNet.NeuralNetworks
Represents a Graph Neural Network that can process data represented as graphs.
Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → GraphModelLayoutBase<T> → GraphNeuralNetwork<T>
Implements: IAuxiliaryLossLayer<T>
For Beginners
A Graph Neural Network is a type of neural network that works with connected data.
Think of it like analyzing a social network:
- Each person is a “node” in the graph
- Friendships between people are “edges” connecting the nodes
- People have attributes (like age, location, interests) which are “node features”
GNNs are useful when:
- The relationships between items are as important as the items themselves
- You’re working with network-like data (social networks, molecules, road systems)
- You need to make predictions about how nodes influence each other
For example, GNNs can help predict which products a customer might like based on what similar customers have purchased, by analyzing the connections between customers and products.
How It Works
A Graph Neural Network (GNN) is designed to work with data structured as graphs, where nodes represent entities and edges represent relationships between these entities. This implementation supports various activation functions for different layers and provides methods for predicting outputs from both vector inputs and graph inputs.
Example
var options = new GraphNeuralNetworkOptions { NodeFeatureSize = 16, HiddenSize = 64, NumLayers = 3 };
var nodeFeatures = Tensor<float>.CreateRandom(new[] { 10, 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 GraphNeuralNetwork<float>(options))
.Build(trainX, trainY);
var output = result.Predict(nodeFeatures);
Constructors
| Constructor | Summary |
|---|---|
GraphNeuralNetwork() |
Initializes a new instance of the GraphNeuralNetwork class with vector activation functions. |
GraphNeuralNetwork(GraphNeuralNetworkOptions options) |
Initializes a new instance from fully customizable GCN options. |
GraphNeuralNetwork(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, IActivationFunction<T> graphConvolutionalActivation, IActivationFunction<T> activationLayerActivation, IActivationFunction<T> finalDenseLayerActivation, IActivationFunction<T> finalActivationLayerActivation, GraphNeuralNetworkOptions options) |
Initializes a new instance of the GraphNeuralNetwork class with scalar activation functions. |
GraphNeuralNetwork(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, IVectorActivationFunction<T> graphConvolutionalVectorActivation, IVectorActivationFunction<T> activationLayerVectorActivation, IVectorActivationFunction<T> finalDenseLayerVectorActivation, IVectorActivationFunction<T> finalActivationLayerVectorActivation, GraphNeuralNetworkOptions options) |
Initializes a new instance of the GraphNeuralNetwork class with scalar activation functions. |
Properties
| Property | Summary |
|---|---|
T AuxiliaryLossWeight { get; set; } |
Gets or sets the weight for the graph smoothness auxiliary loss. |
bool UseAuxiliaryLoss { get; set; } |
Gets or sets whether auxiliary loss (graph smoothness regularization) should be used during training. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, Tensor<T>> GetNamedLayerActivations(Tensor<T> input) |
Gets metadata about the Graph Neural Network model. |
Dictionary<string, string> GetAuxiliaryLossDiagnostics() |
Gets diagnostic information about the graph smoothness auxiliary loss. |
Dictionary<string, string> GetDiagnostics() |
Gets diagnostic information about this component’s state and behavior. |
ModelMetadata<T> GetModelMetadata() |
|
T ComputeAuxiliaryLoss() |
Computes the auxiliary loss for graph smoothness regularization. |
Tensor<T> ForwardForTraining(Tensor<T> input) |
|
Tensor<T> PredictGraph(Tensor<T> nodeFeatures, Tensor<T> adjacencyMatrix) |
Predicts over the given graph: Predict with adjacencyMatrix in place of the graph set through SetAdjacencyMatrix, for this call only. |
void SetAdjacencyMatrix(Tensor<T> adjacencyMatrix) |
Sets the graph that Predict and Train propagate over, or clears it with null. |