VariationalAutoencoder<T>

Models & Types · AiDotNet.NeuralNetworks

Represents a Variational Autoencoder (VAE) neural network architecture, which is used for generating new data similar to the training data and learning compressed representations.

Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → VectorModelLayoutBase<T> → VariationalAutoencoder<T>

Implements: IAuxiliaryLossLayer<T>

For Beginners

A Variational Autoencoder is like a creative compression system.

Imagine you have a folder full of photos of cats:

  • The encoder is like a person who studies all these photos and learns to describe any cat using just a few key attributes (like fur color, ear shape, size)
  • These few attributes are the “latent space” - a much smaller representation of the data
  • The special thing about a VAE is that instead of exact values, it describes each attribute as a range of possible values (a probability distribution)
  • The decoder is like an artist who can take these attribute descriptions and draw a new cat based on them

This ability to work with probability distributions means:

  • You can generate new, never-before-seen cats by sampling from these distributions
  • The generated cats will look realistic because they follow the patterns learned from real cats
  • You can smoothly transition between different types of cats by moving through the latent space

VAEs are used for image generation, data compression, anomaly detection, and other creative applications.

How It Works

A Variational Autoencoder is a type of generative model that learns to encode input data into a probabilistic latent space and then decode samples from that space back into the original data space. Unlike standard autoencoders, VAEs ensure the latent space has good properties for generating new samples by learning a distribution rather than a fixed encoding.

VAEs consist of:

  • An encoder network that maps input data to a probability distribution in latent space
  • A sampling mechanism that draws samples from this distribution
  • A decoder network that maps samples from latent space back to the original data space

Example

var input = Tensor<float>.CreateRandom(new[] { 1, 784 });
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 VariationalAutoencoder<float>())
    .Build(trainX, trainY);
var reconstructed = result.Predict(input);

Constructors

Constructor Summary
VariationalAutoencoder() Initializes a new instance of the VariationalAutoencoder class with the specified architecture, latent space size, and optional optimizer and loss function.
VariationalAutoencoder(NeuralNetworkArchitecture<T> architecture, int latentSize, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, VariationalAutoencoderOptions options)

Properties

Property Summary
T AuxiliaryLossWeight { get; set; } Gets or sets the weight (beta parameter) for the KL divergence auxiliary loss.
bool SupportsGpuTraining { get; }
bool UseAuxiliaryLoss { get; set; } Gets or sets whether to use auxiliary loss (KL divergence) during training.
int LatentSize { get; set; } Gets the size of the latent space dimension in the Variational Autoencoder.

Methods

Method Summary
Dictionary<string, string> GetAuxiliaryLossDiagnostics() Gets diagnostic information about the auxiliary loss computation.
Dictionary<string, string> GetDiagnostics() Gets diagnostic information about this component’s state and behavior.
ModelMetadata<T> GetModelMetadata() Gets metadata about the Variational Autoencoder model.
T ComputeAuxiliaryLoss() Computes the auxiliary loss for the VAE, which is the KL divergence between the learned latent distribution and a standard normal distribution.
Tensor<T> ForwardForTraining(Tensor<T> input)
ValueTuple<Vector<T>, Vector<T>> Encode(Vector<T> input) Encodes an input vector into mean and log variance parameters in the latent space.
Vector<T> Decode(Vector<T> latentVector) Decodes a vector from the latent space back to the original data space.
Vector<T> Reparameterize(Vector<T> mean, Vector<T> logVariance) Implements the reparameterization trick to sample from the latent distribution in a way that allows gradient flow.