NodeClassificationModel<T>
Models & Types · AiDotNet.NeuralNetworks.Tasks.Graph
Implements a complete neural network model for node classification tasks on graphs.
Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → GraphModelLayoutBase<T> → NodeClassificationModel<T>
Implements: IGraphInferenceModel<T>
For Beginners
This model classifies nodes in a graph.
How it works:
- Input: Graph with node features and structure
- Processing: Stack of graph convolutional layers
- Each layer aggregates information from neighbors
- Features become more “context-aware” at each layer
- After k layers, each node knows about its k-hop neighborhood
- Output: Class predictions for each node
Example architecture:
Input: [num_nodes, input_features]
|
GCN Layer 1: [num_nodes, hidden_dim]
|
Activation (ReLU)
|
Dropout
|
GCN Layer 2: [num_nodes, num_classes]
|
Softmax: [num_nodes, num_classes] (probabilities)
Training:
- Use labeled nodes for computing loss
- Unlabeled nodes still participate in message passing
- Graph structure helps propagate label information
How It Works
Node classification predicts labels for individual nodes in a graph using:
- Node features
- Graph structure (adjacency information)
- Semi-supervised learning (only some nodes have labels)
Example
// Create a node classification model for semi-supervised learning on graphs
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[] { 100, 100 }); // 100-node graph
var nodeFeatures = new Tensor<float>(new[] { 100, 16 });
// Classify each node using GCN with neighborhood aggregation
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 NodeClassificationModel<float>(architecture))
.Build(trainX, trainY);
var predictions = result.Predict(nodeFeatures);
// Result is available in the returned value
Constructors
| Constructor | Summary |
|---|---|
NodeClassificationModel() |
Initializes a new instance of the NodeClassificationModel class. |
NodeClassificationModel(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, NodeClassificationOptions options) |
Initializes the model from an already-resolved options instance. |
Properties
| Property | Summary |
|---|---|
double DropoutRate { get; } |
Gets the dropout rate for regularization. |
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 NumLayers { get; } |
Gets the number of graph layers. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, List<double>> TrainOnTask(NodeClassificationTask<T> task, int epochs, double learningRate) |
Trains the model on a node 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. |
Tensor<T> ForwardForTraining(Tensor<T> input) |
Tape-recorded training forward pass. |
Tensor<T> ForwardOnGraph(Tensor<T> nodeFeatures) |
|
double EvaluateOnTask(NodeClassificationTask<T> task) |
Evaluates the model on test nodes. |
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. |