Class SpiralConvLayer<T>
- Namespace
- AiDotNet.NeuralNetworks.Layers
- Assembly
- AiDotNet.dll
Implements spiral convolution for mesh vertex processing.
public class SpiralConvLayer<T> : LayerBase<T>, ILayer<T>, IJitCompilable<T>, IDiagnosticsProvider, IWeightLoadable<T>, IDisposable
Type Parameters
TThe numeric type used for calculations, typically float or double.
- Inheritance
-
LayerBase<T>SpiralConvLayer<T>
- Implements
-
ILayer<T>
- Inherited Members
Remarks
SpiralConvLayer operates on mesh vertices by aggregating features from neighbors in a consistent spiral ordering. This enables translation-equivariant convolutions on irregular mesh structures by defining a canonical ordering of vertex neighbors.
For Beginners: Unlike image convolutions where neighbors are in a grid, mesh vertices have irregular connectivity. Spiral convolution solves this by:
- Starting at each vertex
- Visiting neighbors in a consistent spiral pattern (like a clock hand)
- Gathering features from each neighbor in order
- Applying learned weights to the ordered features
This creates a consistent "template" for convolution regardless of mesh topology.
Applications:
- 3D shape analysis and classification
- Facial expression recognition
- Body pose estimation
- Medical surface analysis
Reference: "Neural 3D Morphable Models: Spiral Convolutional Networks" by Bouritsas et al. Reference: "SpiralNet++: A Fast and Highly Efficient Mesh Convolution Operator" by Gong et al.
Constructors
SpiralConvLayer(int, int, int, int, IActivationFunction<T>?)
Initializes a new instance of the SpiralConvLayer<T> class.
public SpiralConvLayer(int inputChannels, int outputChannels, int spiralLength, int numVertices = 1, IActivationFunction<T>? activationFunction = null)
Parameters
inputChannelsintNumber of input feature channels per vertex.
outputChannelsintNumber of output feature channels per vertex.
spiralLengthintLength of the spiral sequence (number of neighbors).
numVerticesintExpected number of vertices (for shape calculation).
activationFunctionIActivationFunction<T>
Remarks
For Beginners: Creates a spiral convolution layer for mesh processing.
The spiral indices must be set via SetSpiralIndices(int[,]) before forward pass.
Exceptions
- ArgumentOutOfRangeException
Thrown when parameters are non-positive.
SpiralConvLayer(int, int, int, int, IVectorActivationFunction<T>?)
Initializes a new instance of the SpiralConvLayer<T> class with vector activation.
public SpiralConvLayer(int inputChannels, int outputChannels, int spiralLength, int numVertices = 1, IVectorActivationFunction<T>? vectorActivation = null)
Parameters
inputChannelsintNumber of input feature channels per vertex.
outputChannelsintNumber of output feature channels per vertex.
spiralLengthintLength of the spiral sequence (number of neighbors).
numVerticesintExpected number of vertices (for shape calculation).
vectorActivationIVectorActivationFunction<T>Vector activation function to apply.
Exceptions
- ArgumentOutOfRangeException
Thrown when parameters are non-positive.
Properties
InputChannels
Gets the number of input feature channels per vertex.
public int InputChannels { get; }
Property Value
Remarks
Input channels represent features at each vertex. For 3D coordinates, this is 3. After processing, this can include normals, colors, or learned features.
OutputChannels
Gets the number of output feature channels per vertex.
public int OutputChannels { get; }
Property Value
Remarks
Each output channel represents a learned feature detector. More channels enable learning more diverse vertex patterns.
ParameterCount
Gets the total number of trainable parameters.
public override int ParameterCount { get; }
Property Value
SpiralLength
Gets the spiral sequence length (number of neighbors in the spiral).
public int SpiralLength { get; }
Property Value
Remarks
The spiral length determines how many neighbors are considered for each vertex. Longer spirals capture more context but increase computation. Typical values are 9-16 for detailed meshes.
SupportsGpuExecution
Gets a value indicating whether this layer supports GPU execution.
protected override bool SupportsGpuExecution { get; }
Property Value
SupportsJitCompilation
Gets a value indicating whether this layer supports JIT compilation.
public override bool SupportsJitCompilation { get; }
Property Value
- bool
trueif weights are initialized and activation can be JIT compiled.
SupportsTraining
Gets a value indicating whether this layer supports training (backpropagation).
public override bool SupportsTraining { get; }
Property Value
- bool
Always
truefor SpiralConvLayer as it has learnable parameters.
Methods
Backward(Tensor<T>)
Performs the backward pass to compute gradients.
public override Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<T>Gradient of loss with respect to output.
Returns
- Tensor<T>
Gradient of loss with respect to input.
Exceptions
- InvalidOperationException
Thrown when Forward has not been called.
Clone()
Creates a deep copy of this layer.
public override LayerBase<T> Clone()
Returns
- LayerBase<T>
A new SpiralConvLayer with identical configuration and parameters.
Deserialize(BinaryReader)
Deserializes the layer from a binary stream.
public override void Deserialize(BinaryReader reader)
Parameters
readerBinaryReaderBinary reader for deserialization.
Remarks
If spiral indices were serialized with the layer, they are restored automatically. Otherwise, users must call SetSpiralIndices(int[,]) before calling Forward.
Dispose(bool)
Releases resources used by this layer.
protected override void Dispose(bool disposing)
Parameters
disposingboolTrue if called from Dispose(), false if called from finalizer.
Remarks
Override this method in derived classes to release layer-specific resources. Always call base.Dispose(disposing) after releasing your resources.
For Beginners: When creating a custom layer with resources:
protected override void Dispose(bool disposing)
{
if (disposing)
{
// Release your managed resources here
_myGpuHandle?.Dispose();
_myGpuHandle = null;
}
base.Dispose(disposing);
}
ExportComputationGraph(List<ComputationNode<T>>)
Exports the layer as a computation graph for JIT compilation.
public override ComputationNode<T> ExportComputationGraph(List<ComputationNode<T>> inputNodes)
Parameters
inputNodesList<ComputationNode<T>>List to populate with input nodes.
Returns
- ComputationNode<T>
The output computation node.
Exceptions
- ArgumentNullException
Thrown when inputNodes is null.
- InvalidOperationException
Thrown when layer is not initialized.
Forward(Tensor<T>)
Performs the forward pass of spiral convolution.
public override Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<T>Vertex features tensor with shape [numVertices, InputChannels] or [batch, numVertices, InputChannels].
Returns
- Tensor<T>
Output tensor with shape [numVertices, OutputChannels] or [batch, numVertices, OutputChannels].
Remarks
The forward pass: 1. Gathers neighbor features according to spiral indices 2. Concatenates gathered features into a flat vector per vertex 3. Applies linear transformation (weights + bias) 4. Applies activation function
Exceptions
- InvalidOperationException
Thrown when spiral indices are not set.
- ArgumentException
Thrown when input has invalid shape.
ForwardGpu(params IGpuTensor<T>[])
Performs the forward pass of the layer on GPU.
public override IGpuTensor<T> ForwardGpu(params IGpuTensor<T>[] inputs)
Parameters
inputsIGpuTensor<T>[]The GPU-resident input tensor(s).
Returns
- IGpuTensor<T>
The GPU-resident output tensor.
Remarks
This method performs the layer's forward computation entirely on GPU. The input and output tensors remain in GPU memory, avoiding expensive CPU-GPU transfers.
For Beginners: This is like Forward() but runs on the graphics card.
The key difference:
- Forward() uses CPU tensors that may be copied to/from GPU
- ForwardGpu() keeps everything on GPU the whole time
Override this in derived classes that support GPU acceleration.
Exceptions
- NotSupportedException
Thrown when the layer does not support GPU execution.
GetBiases()
Gets the bias tensor.
public override Tensor<T> GetBiases()
Returns
- Tensor<T>
Biases [OutputChannels].
GetParameters()
Gets all trainable parameters as a single vector.
public override Vector<T> GetParameters()
Returns
- Vector<T>
Vector containing all weights and biases.
GetWeights()
Gets the weight tensor.
public override Tensor<T> GetWeights()
Returns
- Tensor<T>
Weights [OutputChannels, InputChannels * SpiralLength].
ResetState()
Resets cached state from forward/backward passes.
public override void ResetState()
Serialize(BinaryWriter)
Serializes the layer to a binary stream.
public override void Serialize(BinaryWriter writer)
Parameters
writerBinaryWriterBinary writer for serialization.
Remarks
If spiral indices are set, they are serialized along with the layer. Otherwise, users must call SetSpiralIndices(int[,]) after deserialization.
SetParameters(Vector<T>)
Sets all trainable parameters from a vector.
public override void SetParameters(Vector<T> parameters)
Parameters
parametersVector<T>Vector containing all parameters.
Exceptions
- ArgumentException
Thrown when parameter count is incorrect.
SetSpiralIndices(int[,])
Sets the spiral indices for the mesh being processed.
public void SetSpiralIndices(int[,] spiralIndices)
Parameters
spiralIndicesint[,]A 2D array of shape [numVertices, SpiralLength] containing neighbor vertex indices in spiral order for each vertex.
Remarks
For Beginners: Before processing a mesh, you must define the spiral ordering of neighbors for each vertex. This method sets that ordering.
The spiral indices should be computed from the mesh topology using a consistent algorithm that starts at a fixed reference direction and visits neighbors in order.
Exceptions
- ArgumentNullException
Thrown when spiralIndices is null.
- ArgumentException
Thrown when spiral length doesn't match.
UpdateParameters(T)
Updates layer parameters using computed gradients.
public override void UpdateParameters(T learningRate)
Parameters
learningRateTLearning rate for gradient descent.
Exceptions
- InvalidOperationException
Thrown when Backward has not been called.