GraphClassificationModel<T>
Models & Types · AiDotNet.NeuralNetworks.Tasks.Graph
Implements a complete neural network model for graph classification tasks.
Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → GraphModelLayoutBase<T> → GraphClassificationModel<T>
For Beginners
This model classifies whole graphs.
Architecture pipeline:
Step 1: Node Encoding
Input: Graph with node features
Process: Stack of GNN layers
Output: Node embeddings [num_nodes, hidden_dim]
Step 2: Graph Pooling (KEY STEP!)
Input: Node embeddings from variable-sized graph
Process: Aggregate to fixed-size representation
Output: Graph embedding [hidden_dim]
Step 3: Classification
Input: Graph embedding [hidden_dim]
Process: Fully connected layers
Output: Class probabilities [num_classes]
Why pooling is crucial:
- Graphs have variable sizes (10 nodes vs 100 nodes)
- Need fixed-size representation for classification
- Like summarizing a book (any length) into a fixed review (200 words)
Example: Molecular toxicity prediction
Molecule (graph) -> GNN layers -> Molecule embedding -> Classifier -> Toxic? (Yes/No)
Small molecule (10 atoms):
10 nodes -> GNN -> 10 embeddings -> Pool -> 1 graph embedding -> Classify
Large molecule (50 atoms):
50 nodes -> GNN -> 50 embeddings -> Pool -> 1 graph embedding -> Classify
Both produce same-sized graph embedding despite different input sizes!
How It Works
Graph classification assigns labels to entire graphs based on their structure and features. The model consists of:
- Node-level processing (GNN layers)
- Graph-level pooling (aggregate node embeddings)
- Classification head (fully connected layers)
Example
// Create a graph classification model for molecular property prediction
var architecture = new NeuralNetworkArchitecture<float>(
InputType.OneDimensional, NeuralNetworkTaskType.MultiClassClassification,
inputSize: 16, // node feature dimension
outputSize: 2); // number of classes (e.g. toxic / non-toxic)
// Prepare graph data (adjacency + node features as tensors)
var adjacency = new Tensor<float>(new[] { 10, 10 }); // 10-node graph
var nodeFeatures = new Tensor<float>(new[] { 10, 16 });
// Classify the entire graph
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 GraphClassificationModel<float>(architecture))
.Build(trainX, trainY);
var prediction = result.Predict(nodeFeatures);
// Result is available in the returned value
Constructors
| Constructor | Summary |
|---|---|
GraphClassificationModel() |
Initializes a new instance of the GraphClassificationModel class. |
GraphClassificationModel(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, GraphClassificationOptions options) |
Initializes the model from an already-resolved options instance. |
Properties
| Property | Summary |
|---|---|
double DropoutRate { get; } |
Gets the dropout rate for regularization. |
int EmbeddingDim { get; } |
Gets the graph embedding dimension after pooling. |
int HiddenDim { get; } |
Gets the hidden dimension size. |
int InputFeatures { get; } |
Gets the number of input features per node. |
int NumClasses { get; } |
Gets the number of output classes. |
int NumGnnLayers { get; } |
Gets the number of GNN layers. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, List<double>> TrainOnTask(GraphClassificationTask<T> task, int epochs, double learningRate) |
Trains the model on a graph classification task. |
ModelMetadata<T> GetModelMetadata() |
Gets metadata about this model for serialization and identification. |
Tensor<T> Forward(Tensor<T> nodeFeatures) |
Performs a forward pass through the network. |
double EvaluateOnTask(GraphClassificationTask<T> task) |
Evaluates the model on test graphs. |
void EnableImplicitIdentityAdjacency() |
Enables implicit self-loops-only (identity) tolerance for this model and its graph layers — the model-level analogue of GraphConvolutionalLayer’s implicitIdentityWhenUnset ctor flag. |
void SetAdjacencyMatrix(Tensor<T> adjacencyMatrix) |
Sets the adjacency matrix for all graph layers in the model. |