Class DiffusionConvLayer<T>
- Namespace
- AiDotNet.NeuralNetworks.Layers
- Assembly
- AiDotNet.dll
Implements diffusion convolution for mesh surface processing using the heat diffusion equation.
public class DiffusionConvLayer<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>DiffusionConvLayer<T>
- Implements
-
ILayer<T>
- Inherited Members
Remarks
DiffusionConvLayer applies learned diffusion kernels on mesh surfaces to aggregate information across geodesic neighborhoods. Instead of using fixed spatial neighborhoods, it leverages the heat diffusion equation to create adaptive receptive fields that respect the underlying geometry.
For Beginners: Think of heat spreading on a surface:
- Place a heat source at each vertex
- Let the heat diffuse across the mesh surface
- After some time, nearby vertices (in geodesic distance) will share heat
- Use this diffusion pattern to aggregate features from neighbors
Key advantages:
- Respects the true surface geometry (not just mesh connectivity)
- Adaptive receptive field based on diffusion time
- Robust to mesh discretization and vertex density
Applications:
- Shape classification and segmentation
- Surface analysis (curvature, normals, features)
- Correspondence between shapes
- Texture synthesis on surfaces
Reference: "DiffusionNet: Discretization Agnostic Learning on Surfaces" by Sharp et al.
Constructors
DiffusionConvLayer(int, int, int, int, int, IActivationFunction<T>?, bool?)
Initializes a new instance of the DiffusionConvLayer<T> class.
public DiffusionConvLayer(int inputChannels, int outputChannels, int numTimeScales = 4, int numEigenvectors = 128, int numVertices = 1, IActivationFunction<T>? activation = null, bool? preferSpectralDiffusion = null)
Parameters
inputChannelsintNumber of input feature channels per vertex.
outputChannelsintNumber of output feature channels per vertex.
numTimeScalesintNumber of diffusion time scales to use (default: 4).
numEigenvectorsintNumber of eigenvectors for spectral acceleration (default: 128).
numVerticesintExpected number of vertices (for shape calculation).
activationIActivationFunction<T>Activation function to apply. Defaults to ReLU.
preferSpectralDiffusionbool?Controls CPU execution when eigenbasis is missing. null = automatic (compute), true = always compute, false = use direct Laplacian method.
Exceptions
- ArgumentOutOfRangeException
Thrown when parameters are non-positive.
DiffusionConvLayer(int, int, int, int, int, IVectorActivationFunction<T>?, bool?)
Initializes a new instance of the DiffusionConvLayer<T> class with vector activation.
public DiffusionConvLayer(int inputChannels, int outputChannels, int numTimeScales = 4, int numEigenvectors = 128, int numVertices = 1, IVectorActivationFunction<T>? vectorActivation = null, bool? preferSpectralDiffusion = null)
Parameters
inputChannelsintNumber of input feature channels per vertex.
outputChannelsintNumber of output feature channels per vertex.
numTimeScalesintNumber of diffusion time scales to use.
numEigenvectorsintNumber of eigenvectors for spectral acceleration.
numVerticesintExpected number of vertices (for shape calculation).
vectorActivationIVectorActivationFunction<T>Vector activation function to apply.
preferSpectralDiffusionbool?Controls CPU execution when eigenbasis is missing. null = automatic (compute), true = always compute, false = use direct Laplacian method.
Properties
DiffusionTimes
Gets the learned diffusion time values.
public T[] DiffusionTimes { get; }
Property Value
- T[]
Remarks
These are learnable parameters that determine the spatial extent of diffusion. Typically initialized as log-spaced values from small (local) to large (global).
InputChannels
Gets the number of input feature channels per vertex.
public int InputChannels { get; }
Property Value
NumTimeScales
Gets the number of diffusion time scales to use.
public int NumTimeScales { get; }
Property Value
Remarks
Multiple time scales capture different spatial extents. Small times capture local features while large times capture global structure. Common choices are 4-8 time scales.
OutputChannels
Gets the number of output feature channels per vertex.
public int OutputChannels { get; }
Property Value
ParameterCount
Gets the total number of trainable parameters.
public override int ParameterCount { get; }
Property Value
SupportsGpuExecution
Gets a value indicating whether this layer supports GPU-accelerated forward pass.
protected override bool SupportsGpuExecution { get; }
Property Value
Remarks
DiffusionConvLayer supports GPU execution for spectral diffusion operations. GPU training is supported when an eigenbasis is available.
SupportsGpuTraining
Gets a value indicating whether this layer supports GPU training.
public override bool SupportsGpuTraining { get; }
Property Value
SupportsJitCompilation
Gets a value indicating whether this layer supports JIT compilation.
public override bool SupportsJitCompilation { get; }
Property Value
SupportsTraining
Gets a value indicating whether this layer supports training.
public override bool SupportsTraining { get; }
Property Value
Methods
Backward(Tensor<T>)
Performs the backward pass to compute gradients.
public override Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<T>
Returns
- Tensor<T>
BackwardGpu(IGpuTensor<T>)
Performs the backward pass using GPU-resident tensors.
public override IGpuTensor<T> BackwardGpu(IGpuTensor<T> outputGradient)
Parameters
outputGradientIGpuTensor<T>GPU-resident gradient tensor.
Returns
- IGpuTensor<T>
GPU-resident input gradient tensor.
Clone()
Creates a deep copy of this layer.
public override LayerBase<T> Clone()
Returns
- LayerBase<T>
Deserialize(BinaryReader)
Deserializes the layer from a binary stream.
public override void Deserialize(BinaryReader reader)
Parameters
readerBinaryReaderBinary reader to deserialize from.
Remarks
Restores all learnable parameters and mesh configuration. After deserialization, the layer is ready for inference without needing to call SetEigenbasis or SetLaplacian.
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>>
Returns
Forward(Tensor<T>)
Performs the forward pass of diffusion 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].
Exceptions
- InvalidOperationException
Thrown when eigenbasis/laplacian is not set.
ForwardGpu(params IGpuTensor<T>[])
Performs GPU-accelerated forward pass using spectral heat diffusion.
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
The spectral diffusion is computed entirely on GPU: 1. Project input to spectral domain: coeffs = Phi^T @ input 2. Apply heat kernel: coeffs = coeffs * exp(-lambda * t) for each time scale 3. Project back: diffused_t = Phi @ coeffs 4. Concatenate all time scales 5. Linear transform: output = diffused @ weights^T + bias 6. Apply activation
If the eigenbasis is missing but a Laplacian is available, it is computed automatically before GPU execution.
GetBiases()
Gets the bias tensor.
public override Tensor<T> GetBiases()
Returns
- Tensor<T>
GetParameters()
Gets all trainable parameters as a single vector.
public override Vector<T> GetParameters()
Returns
- Vector<T>
GetWeights()
Gets the weight tensor.
public override Tensor<T> GetWeights()
Returns
- Tensor<T>
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 to serialize to.
Remarks
Saves all learnable parameters and mesh configuration including: - Layer configuration (channels, time scales, eigenvector count) - Weights and biases - Diffusion time parameters - Eigenvalues and eigenvectors (if available) - Laplacian and mass matrices (if available)
SetEigenbasis(T[], Tensor<T>, Tensor<T>?)
Sets the Laplacian eigenbasis for the current mesh.
public void SetEigenbasis(T[] eigenvalues, Tensor<T> eigenvectors, Tensor<T>? massMatrix = null)
Parameters
eigenvaluesT[]Eigenvalues of the mesh Laplacian.
eigenvectorsTensor<T>Eigenvectors of the mesh Laplacian [numVertices, numEigenvalues].
massMatrixTensor<T>Vertex area weights (optional).
Remarks
For Beginners: The eigenbasis allows efficient computation of diffusion on the mesh surface. You can precompute this from the mesh Laplacian matrix.
Exceptions
- ArgumentNullException
Thrown when required parameters are null.
SetLaplacian(Tensor<T>, Tensor<T>?)
Sets the Laplacian matrix directly for the current mesh.
public void SetLaplacian(Tensor<T> laplacian, Tensor<T>? massMatrix = null)
Parameters
laplacianTensor<T>The mesh Laplacian matrix [numVertices, numVertices].
massMatrixTensor<T>Vertex area weights (optional).
Remarks
If the eigenbasis is not precomputed, it will be derived from the Laplacian automatically unless the layer is configured to prefer the direct method. Automatic eigenbasis computation ignores the mass matrix; provide an explicit eigenbasis when massMatrix is required.
SetParameters(Vector<T>)
Sets all trainable parameters from a vector.
public override void SetParameters(Vector<T> parameters)
Parameters
parametersVector<T>
UpdateParameters(T)
Updates layer parameters using computed gradients.
public override void UpdateParameters(T learningRate)
Parameters
learningRateTLearning rate for gradient descent step.
Remarks
Updates weights, biases, and diffusion time parameters using gradient descent. Diffusion times are constrained to remain positive after update.
UpdateParametersGpu(IGpuOptimizerConfig)
Updates parameters on GPU using the configured optimizer.
public override void UpdateParametersGpu(IGpuOptimizerConfig config)
Parameters
configIGpuOptimizerConfigThe GPU optimizer configuration.