PrimaryCapsuleLayer<T>
Layers · AiDotNet.NeuralNetworks.Layers
Represents a primary capsule layer for capsule networks.
Inheritance: LayerBase<T> → PrimaryCapsuleLayer<T>
Implements: IShapeContract
For Beginners
This layer is the first step in creating a capsule network.
In traditional neural networks, each neuron outputs a single number indicating the presence of a feature. In capsule networks, neurons are grouped into “capsules” where each capsule outputs a vector:
- The length of the vector represents the presence of an entity
- The orientation of the vector represents properties of that entity
Think of it like this:
- Standard neurons: “I see a nose with 90% confidence”
- Capsule neurons: “I see a nose with 90% confidence, and it’s pointing 30° to the left,
it’s 2cm long, it has a slightly curved shape…”
The primary capsule layer converts traditional feature maps (from convolutional layers) into these vector-based capsules that can capture more detailed information about the entities detected.
This approach helps the network understand spatial relationships and maintain information about pose, orientation, and other properties that are typically lost in traditional networks.
How It Works
The PrimaryCapsuleLayer is the first layer in a capsule network that transforms traditional scalar feature maps into capsule vectors. It performs a convolution operation followed by reshaping the output into capsules. Each capsule represents a group of neurons that encodes both the presence and properties of a particular entity. This layer serves as a bridge between standard convolutional layers and higher-level capsule layers.
Constructors
| Constructor | Summary |
|---|---|
PrimaryCapsuleLayer(int capsuleChannels, int capsuleDimension, int kernelSize, int stride, IActivationFunction<T> scalarActivation) |
Lazy constructor (vector activation): resolves inputChannels from input.Shape[1] (NCHW) on first Forward. |
PrimaryCapsuleLayer(int capsuleChannels, int capsuleDimension, int kernelSize, int stride, IVectorActivationFunction<T> vectorActivation) |
Lazy constructor (vector activation): resolves inputChannels from input.Shape[1] (NCHW) on first Forward. |
PrimaryCapsuleLayer(int inputChannels, int capsuleChannels, int capsuleDimension, int kernelSize, int stride, IActivationFunction<T> scalarActivation) |
Initializes a new instance of the PrimaryCapsuleLayer class with the specified parameters and a vector activation function. |
PrimaryCapsuleLayer(int inputChannels, int capsuleChannels, int capsuleDimension, int kernelSize, int stride, IVectorActivationFunction<T> vectorActivation) |
Initializes a new instance of the PrimaryCapsuleLayer class with the specified parameters and a vector activation function. |
Properties
| Property | Summary |
|---|---|
bool SupportsTraining { get; } |
Methods
| Method | Summary |
|---|---|
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]. |
void ClearGradients() |
|
void ResetState() |
Resets the internal state of the primary capsule layer. |
void SetTrainableParameters(IReadOnlyList<Tensor<T>> parameters) |
Replaces trainable parameter tensors (e.g., with ParameterBuffer views). |
void UpdateParameters(T learningRate) |
Updates the parameters of the primary capsule layer using the calculated gradients. |
void ZeroGrad() |
Clears all gradient fields discovered by convention ({paramName}Gradient). |