LinkPredictionModel<T>
Models & Types · AiDotNet.NeuralNetworks.Tasks.Graph
Implements a complete neural network model for link prediction tasks on graphs.
Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → GraphModelLayoutBase<T> → LinkPredictionModel<T>
For Beginners
This model predicts connections between nodes.
How it works:
- Encode: Learn embeddings for all nodes using GNN layers
Input: Node features + Graph structure
Process: Stack of graph conv layers
Output: Node embeddings [num_nodes, embedding_dim]
- Decode: Score node pairs to predict edges
Input: Node pair (i, j)
Compute: score = f(embedding[i], embedding[j])
Common functions:
- Dot product: z_i * z_j
- Concatenation + MLP: MLP([z_i || z_j])
- Distance-based: -||z_i - z_j||^2
- Train: Learn to score existing edges high, non-existing edges low
Example:
Friend recommendation:
- Encode users as embeddings using friend network
- For user pair (Alice, Bob):
* Compute score from their embeddings
* High score -> Likely to be friends
* Low score -> Unlikely to be friends
How It Works
Link prediction predicts whether edges should exist between node pairs using:
- Node features
- Graph structure
- Learned node embeddings
Example
// Create a link prediction model for graph edge 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
var adjacency = new Tensor<float>(new[] { 100, 100 }); // 100-node graph
var nodeFeatures = new Tensor<float>(new[] { 100, 16 });
// Predict edge likelihood between node pairs
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 LinkPredictionModel<float>(architecture))
.Build(trainX, trainY);
var scores = result.Predict(nodeFeatures);
// Result is available in the returned value
Constructors
| Constructor | Summary |
|---|---|
LinkPredictionModel() |
Initializes a new instance of the LinkPredictionModel class. |
LinkPredictionModel(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, LinkPredictionOptions 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 embedding dimension. |
int HiddenDim { get; } |
Gets the hidden dimension size. |
int InputFeatures { get; } |
Gets the number of input features per node. |
int NumLayers { get; } |
Gets the number of GNN layers. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, List<double>> TrainOnTask(LinkPredictionTask<T> task, int epochs, double learningRate) |
Trains the model on a link prediction 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 encoder network. |
Tensor<T> PredictEdges(Tensor<T> edges) |
Computes edge scores for given node pairs. |
double EvaluateOnTask(LinkPredictionTask<T> task) |
Evaluates the model on test edges. |
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. |