Autoencoder<T>

Models & Types · AiDotNet.NeuralNetworks

Represents an autoencoder neural network that can compress data into a lower-dimensional representation and reconstruct it.

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

Implements: IAuxiliaryLossLayer<T>

For Beginners

An autoencoder is like a sophisticated compression and decompression system.

Think of it like this:

  • The encoder part takes your original data (like an image) and compresses it into a smaller representation
  • The middle layer (latent space) holds this compressed version of your data
  • The decoder part takes this compressed version and tries to recreate the original data

For example, with images:

  • You might compress a 256x256 pixel image (65,536 values) into just 100 numbers
  • The network learns which features are most important to preserve
  • It then learns to reconstruct the image from only those 100 numbers

This is useful for:

  • Data compression
  • Noise reduction (by removing noise during reconstruction)
  • Feature learning (the compressed representation often contains meaningful features)
  • Anomaly detection (unusual data is reconstructed poorly)

How It Works

An autoencoder is a type of neural network designed to learn efficient data encodings in an unsupervised manner. It consists of an encoder that compresses the input data into a lower-dimensional representation (the latent space) and a decoder that reconstructs the original data from this representation. Autoencoders are trained to minimize the difference between the original input and the reconstructed output.

Example

var architecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 4);
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 Autoencoder<float>(architecture))
    .Build(trainX, trainY);
var reconstructed = result.Predict(input);

Constructors

Constructor Summary
Autoencoder() Initializes a new instance of the Autoencoder class.
Autoencoder(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, AutoencoderOptions options)

Properties

Property Summary
T AuxiliaryLossWeight { get; set; } Gets or sets the weight for the sparsity penalty.
bool UseAuxiliaryLoss { get; set; } Gets or sets whether to use auxiliary loss (sparsity penalty) during training.
int EncodedSize { get; set; } Gets the size of the encoded representation (latent space).

Methods

Method Summary
Dictionary<string, string> GetAuxiliaryLossDiagnostics() Gets diagnostic information about the sparsity loss.
Dictionary<string, string> GetDiagnostics() Gets diagnostic information about this component’s state and behavior.
ModelMetadata<T> GetModelMetadata() Gets metadata about the autoencoder model.
T ComputeAuxiliaryLoss() Computes the auxiliary loss for sparse autoencoders, which penalizes non-sparse activations.
Tensor<T> Decode(Tensor<T> encodedInput) Decodes the latent space representation back to the original space.
Tensor<T> Encode(Tensor<T> input) Encodes the input data into the latent space representation.
Tensor<T> GenerateSamples(int count, double mean, double stdDev) Generates new data samples by sampling points in the latent space and decoding them.
void SetSparsityParameter(T sparsity) Sets the target sparsity parameter for sparse autoencoder training.