GraphGenerationModel<T>

Models & Types · AiDotNet.NeuralNetworks

Represents a Graph Generation Model using Variational Autoencoder (VAE) architecture.

Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → GraphModelLayoutBase<T> → GraphGenerationModel<T>

For Beginners

Graph generation creates new graphs similar to training data.

How it works:

  • Encoder: Compress graph structure into latent space using GNN
  • Latent space: Learn probabilistic representation (mean and variance)
  • Decoder: Reconstruct graph from latent representation
  • Sampling: Generate new graphs by sampling from latent space

Example - Drug Discovery:

  • Train on known drug molecules
  • Learn latent representation of valid molecular structures
  • Generate new candidate molecules by sampling
  • Filter candidates by predicted properties

Key Components:

  • GNN Encoder: Maps node features to latent space
  • Variational Layer: Learns mean and log-variance for each node
  • Inner Product Decoder: Reconstructs adjacency matrix
  • Reparameterization: Enables gradient flow through sampling

Loss Function:

  • Reconstruction Loss: How well we reconstruct the adjacency matrix
  • KL Divergence: Regularization to keep latent space well-structured

Applications:

  • Molecular design and drug discovery
  • Social network generation
  • Circuit design
  • Protein structure generation

How It Works

Graph generation models learn to generate new graph structures from latent representations. This implementation uses a Variational Graph Autoencoder (VGAE) approach that learns a latent distribution of graph structures and can sample new valid graphs.

Example

var trainX = Tensor<float>.CreateRandom(4, 9);
var trainY = Tensor<float>.CreateRandom(4, 9);
            
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
    .ConfigureModel(new GraphGenerationModel<float>(options: new GraphGenerationModelOptions { InputFeatures = 9, HiddenDim = 128, MaxNodes = 50, LearningRate = 0.005 }))
    .Build(trainX, trainY);
            
var nodeFeatures = Tensor<float>.CreateRandom(new[] { 20, 9 });
var graph = result.Predict(nodeFeatures);

Constructors

Constructor Summary
GraphGenerationModel(IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, ILearningRateScheduler learningRateScheduler, GraphGenerationModelOptions options) Initializes the model from an already-resolved options instance.

Properties

Property Summary
GraphGenerationType GenerationType { get; } Gets the type of graph generation.
double KLWeight { get; set; } KL divergence weight for balancing reconstruction and regularization.
int HiddenDim { get; } Gets the hidden dimension for encoder layers.
int LatentDim { get; } Gets the latent dimension size.
int MaxNodes { get; } Gets the maximum number of nodes for graph generation.
int NumEncoderLayers { get; } Gets the number of encoder layers.
int NumLayers { get; } Gets the number of layers in the model.

Methods

Method Summary
Dictionary<string, Tensor<T>> GetNamedLayerActivations(Tensor<T> input) Gets the intermediate activations from each layer, ensuring adjacency is set.
List<Tensor<T>> Generate(int numNodes, int numSamples, double threshold) Generates new graphs by sampling from the latent space.
List<Tensor<T>> Interpolate(Tensor<T> graph1Features, Tensor<T> graph1Adj, Tensor<T> graph2Features, Tensor<T> graph2Adj, int numSteps) Interpolates between two graphs in latent space.
ModelMetadata<T> GetModelMetadata() Gets metadata about this model.
T ComputeLoss(Tensor<T> reconstructed, Tensor<T> original) Computes the ELBO loss (reconstruction + KL divergence).
Tensor<T> Decode(Tensor<T> latent) Decodes latent representations to reconstruct the adjacency matrix.
Tensor<T> Forward(Tensor<T> nodeFeatures, Tensor<T> adjacencyMatrix) Performs a complete forward pass: encode, sample, decode.
Tensor<T> Generate(int numSamples, int numNodes, Tensor<T> conditioningInput, double threshold) Generates new graphs by sampling from the latent space with optional conditioning.
Tensor<T> Reparameterize(Tensor<T> mean, Tensor<T> logVar) Samples from the latent distribution using the reparameterization trick.
ValueTuple<Tensor<T>, Tensor<T>> Encode(Tensor<T> nodeFeatures, Tensor<T> adjacencyMatrix) Encodes node features into latent space representations.
long GetParameterCount() Gets the total number of trainable parameters in the network.
void Train(Tensor<T> nodeFeatures, Tensor<T> adjacencyMatrix, int epochs, double learningRate) Trains the model on graph data.