Table of Contents

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

T

The numeric type used for computations.

Inheritance
SimCLR<T>
Implements
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:

  1. Take an image and create two random augmented views
  2. Pass both through the same encoder network
  3. Pass both through a projection head (MLP)
  4. Apply NT-Xent contrastive loss to bring views together
  5. 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

encoder INeuralNetwork<T>

The backbone encoder network (e.g., ResNet).

projector IProjectorHead<T>

The projection head (MLP).

config SSLConfig

Optional SSL configuration.

Properties

Category

Gets the category of this SSL method.

public override SSLMethodCategory Category { get; }

Property Value

SSLMethodCategory

Remarks

Categories include Contrastive, NonContrastive, Generative, and SelfDistillation.

Name

Gets the name of this SSL method.

public override string Name { get; }

Property Value

string

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

bool

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

bool

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

encoder INeuralNetwork<T>

The backbone encoder.

encoderOutputDim int

Output dimension of the encoder.

projectionDim int

Dimension of the projection space (default: 128).

hiddenDim int

Hidden 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

batch Tensor<T>

The input batch tensor.

augmentationContext SSLAugmentationContext<T>

Optional augmentation context.

Returns

SSLStepResult<T>

The result of the training step.