Class SimCLR<T>
- Namespace
- AiDotNet.SelfSupervisedLearning
- Assembly
- AiDotNet.dll
SimCLR: A Simple Framework for Contrastive Learning of Visual Representations.
public class SimCLR<T> : SSLMethodBase<T>, ISSLMethod<T>
Type Parameters
TThe numeric type used for computations.
- Inheritance
-
SimCLR<T>
- Implements
-
ISSLMethod<T>
- Inherited Members
Remarks
For Beginners: SimCLR is one of the most influential self-supervised learning methods. It learns representations by maximizing agreement between differently augmented views of the same image using a contrastive loss.
How SimCLR works:
- Take an image and create two random augmented views
- Pass both through the same encoder network
- Pass both through a projection head (MLP)
- Apply NT-Xent contrastive loss to bring views together
- Other images in the batch serve as negative samples
Key hyperparameters:
- Batch size: Larger is better (4096-8192 in paper)
- Temperature: 0.1 (controls softmax sharpness)
- Projection dimension: 128
- Augmentations: Random crop, color jitter, blur
Reference: Chen et al., "A Simple Framework for Contrastive Learning of Visual Representations" (ICML 2020)
Constructors
SimCLR(INeuralNetwork<T>, IProjectorHead<T>, SSLConfig?)
Initializes a new instance of the SimCLR class.
public SimCLR(INeuralNetwork<T> encoder, IProjectorHead<T> projector, SSLConfig? config = null)
Parameters
encoderINeuralNetwork<T>The backbone encoder network (e.g., ResNet).
projectorIProjectorHead<T>The projection head (MLP).
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.
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)
Creates a SimCLR instance with default configuration.
public static SimCLR<T> Create(INeuralNetwork<T> encoder, int encoderOutputDim, int projectionDim = 128, int hiddenDim = 2048)
Parameters
encoderINeuralNetwork<T>The backbone encoder.
encoderOutputDimintOutput dimension of the encoder.
projectionDimintDimension of the projection space (default: 128).
hiddenDimintHidden dimension of the projector MLP (default: 2048).
Returns
- SimCLR<T>
A configured SimCLR 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.