Class BarlowTwins<T>
- Namespace
- AiDotNet.SelfSupervisedLearning
- Assembly
- AiDotNet.dll
Barlow Twins: Self-Supervised Learning via Redundancy Reduction.
public class BarlowTwins<T> : SSLMethodBase<T>, ISSLMethod<T>
Type Parameters
TThe numeric type used for computations.
- Inheritance
-
BarlowTwins<T>
- Implements
-
ISSLMethod<T>
- Inherited Members
Remarks
For Beginners: Barlow Twins learns representations by making the cross-correlation matrix between embeddings of two augmented views close to the identity matrix. This achieves both invariance (diagonal = 1) and reduces redundancy between features (off-diagonal = 0).
Key innovations:
- No negatives: Unlike contrastive methods, doesn't need negative samples
- No momentum encoder: Uses a simpler symmetric architecture
- Decorrelation: Explicitly reduces redundancy between feature dimensions
- No asymmetry: Both branches are identical (no predictor needed)
Loss components:
- Invariance term: Makes diagonal elements = 1 (same view features match)
- Redundancy reduction: Makes off-diagonal elements = 0 (decorrelation)
Cross-correlation matrix:
C_ij = (1/N) * Σ_b z1[b,i] * z2[b,j]
Loss = Σ_i (1 - C_ii)² + λ * Σ_i Σ_{j≠i} C_ij²
Reference: Zbontar et al., "Barlow Twins: Self-Supervised Learning via Redundancy Reduction" (ICML 2021)
Constructors
BarlowTwins(INeuralNetwork<T>, IProjectorHead<T>, SSLConfig?)
Initializes a new instance of the BarlowTwins class.
public BarlowTwins(INeuralNetwork<T> encoder, IProjectorHead<T> projector, SSLConfig? config = null)
Parameters
encoderINeuralNetwork<T>The encoder network (shared between both branches).
projectorIProjectorHead<T>Projection head (typically 3-layer MLP with 8192 output dim).
configSSLConfigOptional SSL configuration.
Properties
Category
Gets the category of this SSL method.
public override SSLMethodCategory Category { get; }
Property Value
Remarks
Categories include Contrastive, NonContrastive, Generative, and SelfDistillation.
Lambda
Gets the lambda (redundancy reduction weight) used in the loss.
public double Lambda { get; }
Property Value
Name
Gets the name of this SSL method.
public override string Name { get; }
Property Value
Remarks
Examples: "SimCLR", "MoCo v2", "BYOL", "DINO", "MAE"
RequiresMemoryBank
Indicates whether this method requires a memory bank for negative samples.
public override bool RequiresMemoryBank { get; }
Property Value
Remarks
For Beginners: Memory banks store embeddings from previous batches to use as negative samples in contrastive learning. MoCo uses this, SimCLR does not.
UsesMomentumEncoder
Indicates whether this method uses a momentum-updated encoder.
public override bool UsesMomentumEncoder { get; }
Property Value
Remarks
For Beginners: A momentum encoder is a slowly-updated copy of the main encoder. Methods like MoCo, BYOL, and DINO use this to provide stable targets.
Methods
Create(INeuralNetwork<T>, int, int, int, double)
Creates a Barlow Twins instance with default configuration.
public static BarlowTwins<T> Create(INeuralNetwork<T> encoder, int encoderOutputDim, int projectionDim = 8192, int hiddenDim = 8192, double lambda = 0.0051)
Parameters
encoderINeuralNetwork<T>The backbone encoder.
encoderOutputDimintOutput dimension of the encoder.
projectionDimintDimension of the projection space (default: 8192).
hiddenDimintHidden dimension of the projector MLP (default: 8192).
lambdadoubleRedundancy reduction weight (default: 0.0051).
Returns
- BarlowTwins<T>
A configured Barlow Twins instance.
TrainStepCore(Tensor<T>, SSLAugmentationContext<T>?)
Implementation-specific training step logic.
protected override SSLStepResult<T> TrainStepCore(Tensor<T> batch, SSLAugmentationContext<T>? augmentationContext)
Parameters
batchTensor<T>The input batch tensor.
augmentationContextSSLAugmentationContext<T>Optional augmentation context.
Returns
- SSLStepResult<T>
The result of the training step.