Class LinearProjector<T>
- Namespace
- AiDotNet.SelfSupervisedLearning
- Assembly
- AiDotNet.dll
Linear projection head for self-supervised learning.
public class LinearProjector<T> : IProjectorHead<T>
Type Parameters
TThe numeric type used for computations (typically float or double).
- Inheritance
-
LinearProjector<T>
- Implements
- Inherited Members
Remarks
For Beginners: A linear projector is the simplest projection head - just a single linear transformation (matrix multiplication + bias). While simpler than MLP projectors, linear projectors can still be effective in some scenarios.
Architecture:
Input → Linear → Output
[d_in] [d_out]
When to use Linear vs MLP:
- Use Linear for simplicity, lower compute, or when encoder is already powerful
- Use MLP for better downstream performance (recommended for most SSL methods)
Constructors
LinearProjector(int, int, bool, int?)
Initializes a new instance of the LinearProjector class.
public LinearProjector(int inputDim, int outputDim = 128, bool useBias = true, int? seed = null)
Parameters
inputDimintInput dimension (encoder output size).
outputDimintOutput dimension (projection size).
useBiasboolWhether to include a bias term.
seedint?Optional random seed for initialization.
Properties
HiddenDimension
Gets the hidden dimension (for MLP projectors).
public int? HiddenDimension { get; }
Property Value
- int?
Remarks
Typical values: 2048-4096. Usually larger than output dimension.
InputDimension
Gets the input dimension expected by this projector.
public int InputDimension { get; }
Property Value
OutputDimension
Gets the output dimension produced by this projector.
public int OutputDimension { get; }
Property Value
Remarks
Typical values: 128-2048. SimCLR uses 128, MoCo uses 128, BYOL uses 256.
ParameterCount
Gets the total number of trainable parameters.
public int ParameterCount { get; }
Property Value
Methods
Backward(Tensor<T>)
Performs the backward pass through the projector.
public Tensor<T> Backward(Tensor<T> gradients)
Parameters
gradientsTensor<T>The gradients from the loss with respect to projector output.
Returns
- Tensor<T>
The gradients with respect to projector input (for encoder backprop).
ClearGradients()
Clears accumulated gradients.
public void ClearGradients()
GetParameterGradients()
Gets the gradients computed during the last backward pass.
public Vector<T> GetParameterGradients()
Returns
- Vector<T>
A vector containing gradients for all parameters.
GetParameters()
Gets all trainable parameters of the projector.
public Vector<T> GetParameters()
Returns
- Vector<T>
A vector containing all parameters.
Project(Tensor<T>)
Projects encoder output to the SSL embedding space.
public Tensor<T> Project(Tensor<T> input)
Parameters
inputTensor<T>The encoder output tensor.
Returns
- Tensor<T>
The projected embedding tensor.
Remarks
For Beginners: This transforms encoder features into a lower-dimensional space where the SSL loss is computed. The projection helps separate the pretraining objective from the learned representations.
Reset()
Resets the projector state (clears any internal buffers).
public void Reset()
SetParameters(Vector<T>)
Sets the parameters of the projector.
public void SetParameters(Vector<T> parameters)
Parameters
parametersVector<T>The parameter vector to load.
SetTrainingMode(bool)
Sets training or evaluation mode.
public void SetTrainingMode(bool isTraining)
Parameters
isTrainingboolTrue for training mode, false for evaluation.