CapsuleNetwork<T>

Models & Types · AiDotNet.NeuralNetworks

Represents a Capsule Network, a type of neural network that preserves spatial relationships between features.

Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → ImageClassifierModelLayoutBase<T> → CapsuleNetwork<T>

Implements: IAuxiliaryLossLayer<T>

For Beginners

A Capsule Network is like a more advanced version of traditional neural networks.

Think of it this way:

  • Traditional networks detect features like edges or textures, but lose information about how these features relate to each other
  • Capsule Networks not only detect features, but also understand their relationships, orientations, and positions
  • This is like the difference between recognizing individual puzzle pieces versus understanding how they fit together

For example, a traditional network might recognize an eye, a nose, and a mouth separately, but a Capsule Network can better understand that these features need to be in a specific arrangement to make a face. This makes Capsule Networks particularly good at recognizing objects from different angles or when parts are arranged differently.

How It Works

A Capsule Network is a neural network architecture designed to address limitations of traditional convolutional neural networks. Instead of using scalar-output feature detectors (neurons), Capsule Networks use vector-output capsules. Each capsule’s output vector represents the presence of an entity and its instantiation parameters (like position, orientation, and scale). This architecture helps to preserve hierarchical relationships between features, making it particularly effective for tasks requiring understanding of spatial relationships.

Example

var architecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 4);
var input = Tensor<float>.CreateRandom(new[] { 1, 1, 28, 28 });
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 CapsuleNetwork<float>(architecture))
    .Build(trainX, trainY);
var output = result.Predict(input);

Constructors

Constructor Summary
CapsuleNetwork() Initializes a new instance of the CapsuleNetwork class with the specified architecture.
CapsuleNetwork(NeuralNetworkArchitecture<T> architecture, ILossFunction<T> lossFunction, CapsuleNetworkOptions options)

Properties

Property Summary
T AuxiliaryLossWeight { get; set; } Gets or sets the weight for reconstruction loss.
bool UseAuxiliaryLoss { get; set; } Gets or sets whether to use auxiliary loss (reconstruction regularization) during training.

Methods

Method Summary
Dictionary<string, string> GetAuxiliaryLossDiagnostics() Gets diagnostic information about the auxiliary losses.
Dictionary<string, string> GetDiagnostics() Gets diagnostic information about this component’s state and behavior.
ModelMetadata<T> GetModelMetadata() Retrieves metadata about the Capsule Network model.
T ComputeAuxiliaryLoss() Computes the auxiliary loss for the CapsuleNetwork, which is the reconstruction regularization.
T ComputeReconstructionLoss(Tensor<T> input, int? trueLabel) Serializes Capsule Network-specific data to a binary writer.
Tensor<T> ForwardForTraining(Tensor<T> input) Tape-tracked forward pass that also captures output for reconstruction loss.