Class MeshEdgeConvLayer<T>
- Namespace
- AiDotNet.NeuralNetworks.Layers
- Assembly
- AiDotNet.dll
Implements edge convolution for mesh-based neural networks (MeshCNN style).
public class MeshEdgeConvLayer<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>MeshEdgeConvLayer<T>
- Implements
-
ILayer<T>
- Inherited Members
Remarks
MeshEdgeConvLayer processes triangle meshes by treating edges as the fundamental unit. Each edge connects two triangular faces, and the layer learns to extract features from the geometric relationship between these faces.
For Beginners: Think of a mesh as a surface made of connected triangles. Each triangle edge is shared by (at most) two triangles. This layer examines each edge and the two triangles it connects to learn meaningful features about the shape.
Key concepts:
- Edge: A line segment connecting two vertices, shared by up to 2 faces
- Dihedral angle: The angle between two faces sharing an edge
- Edge features: Properties like length, angles, and face normals
The layer learns to recognize patterns in how faces connect, enabling recognition of shapes, surface curvature, and other geometric properties.
Reference: "MeshCNN: A Network with an Edge" by Hanocka et al., SIGGRAPH 2019
Constructors
MeshEdgeConvLayer(int, int, int, IActivationFunction<T>?)
Initializes a new instance of the MeshEdgeConvLayer<T> class.
public MeshEdgeConvLayer(int inputChannels, int outputChannels, int numNeighbors = 4, IActivationFunction<T>? activationFunction = null)
Parameters
inputChannelsintNumber of input feature channels per edge.
outputChannelsintNumber of output feature channels per edge.
numNeighborsintNumber of neighboring edges per edge. Default is 4 (standard MeshCNN).
activationFunctionIActivationFunction<T>
Remarks
For Beginners: Creates an edge convolution layer for processing mesh data.
Typical usage: - inputChannels: 5 for initial edge features (dihedral angle, ratios, inner angles) - outputChannels: 32, 64, 128, etc. depending on network depth - numNeighbors: 4 (each edge has 4 neighboring edges in a triangular mesh)
Exceptions
- ArgumentOutOfRangeException
Thrown when parameters are non-positive.
MeshEdgeConvLayer(int, int, int, IVectorActivationFunction<T>?)
Initializes a new instance of the MeshEdgeConvLayer<T> class with vector activation.
public MeshEdgeConvLayer(int inputChannels, int outputChannels, int numNeighbors = 4, IVectorActivationFunction<T>? vectorActivation = null)
Parameters
inputChannelsintNumber of input feature channels per edge.
outputChannelsintNumber of output feature channels per edge.
numNeighborsintNumber of neighboring edges per edge. Default is 4.
vectorActivationIVectorActivationFunction<T>The vector activation function to apply.
Remarks
Vector activation functions operate on entire vectors at once, which can be more efficient for operations like Softmax that need to consider all elements together.
Properties
InputChannels
Gets the number of input feature channels per edge.
public int InputChannels { get; }
Property Value
Remarks
Input channels represent features associated with each edge. Initial edge features typically include dihedral angle, edge length ratios, and inner angles (5 features). After processing, this can be any number of learned features.
NumNeighbors
Gets the number of neighboring edges to consider for each edge convolution.
public int NumNeighbors { get; }
Property Value
Remarks
In MeshCNN, each edge has 4 neighboring edges (2 from each adjacent face). The convolution aggregates features from these neighbors to capture local structure.
OutputChannels
Gets the number of output feature channels per edge.
public int OutputChannels { get; }
Property Value
Remarks
Each output channel corresponds to a learned filter that detects specific edge patterns. More channels allow learning more diverse patterns.
ParameterCount
Gets the total number of trainable parameters.
public override int ParameterCount { get; }
Property Value
SupportsJitCompilation
Gets a value indicating whether this layer supports JIT compilation.
public override bool SupportsJitCompilation { get; }
Property Value
- bool
false. JIT compilation is not currently supported for MeshEdgeConvLayer due to the lack of graph-specific operations for edge aggregation in TensorOperations.
SupportsTraining
Gets a value indicating whether this layer supports training (backpropagation).
public override bool SupportsTraining { get; }
Property Value
- bool
Always
truefor MeshEdgeConvLayer as it has learnable parameters.
Methods
Backward(Tensor<T>)
Performs the backward pass to compute gradients for training.
public override Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<T>The gradient of the loss with respect to this layer's output.
Returns
- Tensor<T>
The gradient of the loss with respect to this layer's input.
Remarks
Routes to manual or autodiff implementation based on UseAutodiff property.
Exceptions
- InvalidOperationException
Thrown when Forward has not been called.
Clone()
Creates a deep copy of the layer.
public override LayerBase<T> Clone()
Returns
- LayerBase<T>
A new instance with identical configuration and parameters.
Deserialize(BinaryReader)
Deserializes the layer from a binary stream.
public override void Deserialize(BinaryReader reader)
Parameters
readerBinaryReaderThe binary reader to deserialize from.
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
- NotSupportedException
Thrown because MeshEdgeConv JIT is not yet implemented.
Forward(Tensor<T>)
Performs the forward pass of edge convolution.
public override Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<T>Edge features tensor of shape [numEdges, InputChannels].
Returns
- Tensor<T>
Output tensor of shape [numEdges, OutputChannels] after convolution and activation.
Remarks
This method requires edge adjacency to be set via SetEdgeAdjacency(int[,]) before calling. The convolution aggregates features from each edge and its neighbors.
Exceptions
- ArgumentException
Thrown when input has invalid shape.
- InvalidOperationException
Thrown when edge adjacency is not set.
ForwardGpu(params IGpuTensor<T>[])
Performs GPU-accelerated forward pass for edge convolution.
public override IGpuTensor<T> ForwardGpu(params IGpuTensor<T>[] inputs)
Parameters
inputsIGpuTensor<T>[]Input GPU tensors (uses first input).
Returns
- IGpuTensor<T>
GPU-resident output tensor.
Remarks
Uses GPU Gather for efficient neighbor feature aggregation and Gemm for linear transformation. The operation is: output = activation(aggregate(features) @ weights^T + biases)
GetBiases()
Gets the bias tensor.
public override Tensor<T> GetBiases()
Returns
- Tensor<T>
The bias tensor.
GetParameters()
Gets all trainable parameters as a single vector.
public override Vector<T> GetParameters()
Returns
- Vector<T>
A vector containing all weight and bias parameters.
GetWeights()
Gets the weight tensor.
public override Tensor<T> GetWeights()
Returns
- Tensor<T>
The weights tensor.
ResetState()
Resets the 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
writerBinaryWriterThe binary writer to serialize to.
SetEdgeAdjacency(int[,])
Sets the edge adjacency information for the current mesh.
public void SetEdgeAdjacency(int[,] edgeAdjacency)
Parameters
edgeAdjacencyint[,]A 2D array of shape [numEdges, NumNeighbors] containing neighbor edge indices. Use -1 for boundary edges with fewer neighbors.
Remarks
For Beginners: This tells the layer how edges are connected in your mesh.
For each edge, provide the indices of its neighboring edges. In a standard triangular mesh, each edge has 4 neighbors (2 per adjacent face). For boundary edges with fewer neighbors, use -1 as a placeholder.
Exceptions
- ArgumentNullException
Thrown when edgeAdjacency is null.
- ArgumentException
Thrown when second dimension doesn't match NumNeighbors.
SetParameters(Vector<T>)
Sets all trainable parameters from a single vector.
public override void SetParameters(Vector<T> parameters)
Parameters
parametersVector<T>Vector containing all parameters (weights followed by biases).
Exceptions
- ArgumentException
Thrown when parameter count does not match expected.
UpdateParameters(T)
Updates the layer parameters using the computed gradients and learning rate.
public override void UpdateParameters(T learningRate)
Parameters
learningRateTThe learning rate for gradient descent.
Exceptions
- InvalidOperationException
Thrown when Backward has not been called.