CapsuleLayer<T>

Layers · AiDotNet.NeuralNetworks.Layers

Represents a capsule neural network layer that encapsulates groups of neurons to better preserve spatial information.

Inheritance: LayerBase<T> → CapsuleLayer<T>

Implements: IAuxiliaryLossLayer<T>, IShapeContract

For Beginners

A capsule layer is an advanced type of neural network layer that works differently from standard layers.

Traditional neural network layers use single numbers to represent features, but capsule layers use groups of numbers (vectors) that can capture more information:

  • Each “capsule” is a group of neurons that work together
  • The length of a capsule’s output tells you how likely something exists
  • The direction of a capsule’s output tells you about its properties (like position, size, rotation)

For example, if detecting faces in images:

  • A traditional network might have neurons that detect eyes, nose, mouth separately
  • A capsule network would understand how these parts relate to each other spatially

This helps the network recognize objects even when they’re viewed from different angles or positions, which is something traditional networks struggle with.

How It Works

A capsule layer is a specialized neural network layer that groups neurons into “capsules,” where each capsule represents a specific entity or feature. Unlike traditional neural networks that use scalar outputs, capsules output vectors whose length represents the probability of the entity’s existence and whose orientation encodes the entity’s properties. Capsule layers use dynamic routing between capsules, which helps preserve hierarchical relationships between features and improves the network’s ability to recognize objects from different viewpoints.

Constructors

Constructor Summary
CapsuleLayer(int numCapsules, int capsuleDimension, int numRoutingIterations, IActivationFunction<T> activationFunction) Initializes a new instance of the CapsuleLayer class with specified dimensions and routing iterations.

Properties

Property Summary
T AuxiliaryLossWeight { get; set; } Gets or sets the weight for the routing entropy auxiliary loss.
bool SupportsTraining { get; }
bool UseAuxiliaryLoss { get; set; } Gets or sets whether auxiliary loss (routing entropy regularization) should be used during training.

Methods

Method Summary
Dictionary<string, string> GetAuxiliaryLossDiagnostics() Gets diagnostic information about the routing entropy auxiliary loss.
Dictionary<string, string> GetDiagnostics() Gets diagnostic information about this component’s state and behavior.
Dictionary<string, string> GetParameterRoles() Returns parameter roles for per-role optimizer configuration (e.g., weight decay exemption for biases).
IReadOnlyList<OutputAxisContract> OutputAxesFor(int inputRank)
IReadOnlyList<Tensor<T>> GetTrainableParameters() Returns all trainable parameter tensors marked with [TrainableParameter].
T ComputeAuxiliaryLoss() Computes the auxiliary loss for routing entropy regularization.
void ClearGradients() Resets the internal state of the capsule layer.
void ResetState()
void SetTrainableParameters(IReadOnlyList<Tensor<T>> parameters) Replaces trainable parameter tensors (e.g., with ParameterBuffer views).
void UpdateParameters(T learningRate) Updates the layer’s parameters using the calculated gradients.
void ZeroGrad() Clears all gradient fields discovered by convention ({paramName}Gradient).