Multi-objective models#
Multiobjective contrastive learning.
Starting in CEBRA 0.6.0, we have added support for subspace contrastive learning. This is a method for training models that are able to learn multiple subspaces of the feature space simultaneously.
Subspace contrastive learning requires to use specialized models and criterions.
This module specifies a test of classes required for training CEBRA models with multiple objectives.
The objectives are defined by the wrapper class cebra.models.multicriterions.MultiCriterions
.
Two solvers are currently implemented:
cebra.solver.multiobjective.ContrastiveMultiobjectiveSolverxCEBRA
cebra.solver.multiobjective.SupervisedMultiobjectiveSolverxCEBRA
See also
cebra.solver.multiobjective.SupervisedMultiobjectiveSolverxCEBRA
cebra.solver.multiobjective.MultiObjectiveConfig
cebra.models.multicriterions.MultiCriterions
- class cebra.solver.multiobjective.MultiObjectiveConfig(loader)#
Bases:
object
Configuration class for setting up multi-objective learning with Cebra.
- Parameters:
loader – Data loader used for configurations.
- set_slice(start, end)#
Select the index range of the embedding.
The configured loss will be applied to the
start:end
slice of the embedding space. Make sure the selected dimensionality is appropriate for the chosen loss function and distribution.
- set_loss(loss_name, **kwargs)#
Select the loss function to apply.
Select a valid loss function from
cebra.models.criterions
. Common choices are:FixedEuclideanInfoNCE
FixedCosineInfoNCE
which can be passed as string values to
loss_name
. The loss will be applied to the range specified withset_slice
.
- set_distribution(distribution_name, **kwargs)#
Select the distribution to sample from.
The loss function specified in
set_loss
is applied to positive and negative pairs sampled from the specified distribution.
- push()#
Add a slice/loss/distribution setting to the config.
After calling all of
set_slice
,set_loss
,set_distribution
, add this group to the config by calling this function.Once all configuration parts are pushed, call
finalize
to finish the configuration.
- finalize()#
Finalize the multiobjective configuration.
- class cebra.solver.multiobjective.MultiobjectiveSolverBase(model, criterion, optimizer, history=<factory>, decode_history=<factory>, log=<factory>, tqdm_on=True, feature_ranges=None, renormalize=None, use_sam=False, regularizer=None, metadata=<factory>)#
Bases:
Solver
- fit(loader, valid_loader=None, *, valid_frequency=None, log_frequency=None, save_hook=None, scheduler_regularizer=None, scheduler_loss=None, logger=None)#
Train model for the specified number of steps.
- Parameters:
loader (
Loader
) – Data loader, which is an iterator over cebra.data.Batch instances. Each batch contains reference, positive and negative input samples.valid_loader (
Optional
[Loader
]) – Data loader used for validation of the model.valid_frequency (
Optional
[int
]) – The frequency for running validation on thevalid_loader
instance.logdir – The logging directory for writing model checkpoints. The checkpoints can be read again using the solver.load function, or manually via loading the state dict.
save_hook (
Optional
[Callable
[[int
,Solver
],None
]]) – callback. It will be called when we run validation.logger (
Optional
[Logger
]) – logger to log progress. None by default.
- step(batch, weights_loss=None, weights_regularizer=None)#
Perform a single gradient update with multiple objectives.
- Return type:
- validation(loader, logger=None, weights_loss=None)#
Compute score of the model on data.
- Parameters:
loader (
Loader
) – Data loader, which is an iterator over cebra.data.Batch instances. Each batch contains reference, positive and negative input samples.session_id – The session ID, an integer between 0 and the number of sessions in the multisession model, set to None for single session.
- Returns:
Loss averaged over iterations on data batch.
- class cebra.solver.multiobjective.SupervisedMultiobjectiveSolverxCEBRA(model, criterion, optimizer, history=<factory>, decode_history=<factory>, log=<factory>, tqdm_on=True, feature_ranges=None, renormalize=None, use_sam=False, regularizer=None, metadata=<factory>)#
Bases:
MultiobjectiveSolverBase
Supervised neural network training using the MSE loss.
This solver can be used as a baseline variant instead of the contrastive solver,
cebra.solver.multiobjective.ContrastiveMultiobjectiveSolverxCEBRA
.
- class cebra.solver.multiobjective.ContrastiveMultiobjectiveSolverxCEBRA(model, criterion, optimizer, history=<factory>, decode_history=<factory>, log=<factory>, tqdm_on=True, feature_ranges=None, renormalize=None, use_sam=False, regularizer=None, metadata=<factory>)#
Bases:
MultiobjectiveSolverBase
Multi-objective solver for CEBRA.
This solver is used for training CEBRA models with multiple objectives.
Support for training CEBRA with multiple criteria.
Note
This module was introduced in CEBRA 0.6.0.
- class cebra.models.multicriterions.MultiCriterions(*args: Any, **kwargs: Any)#
Bases:
Module
A module for handling multiple loss functions with different criteria.
This module allows combining multiple loss functions, each operating on specific slices of the input data. It supports both supervised and contrastive learning modes.
- Parameters:
losses – A list of dictionaries containing loss configurations. Each dictionary should have: - ‘indices’: Tuple of (start, end) indices for the data slice - ‘supervised_loss’: Dict with loss config for supervised mode - ‘contrastive_loss’: Dict with loss config for contrastive mode Loss configs should contain: - ‘name’: Name of the loss function - ‘kwargs’: Optional parameters for the loss function
mode – Either “supervised” or “contrastive” to specify the training mode
The loss functions can be from torch.nn or custom implementations from cebra.models.criterions. Each criterion is applied to its corresponding slice of the input data during forward pass.
Example
>>> import torch >>> from cebra.data.datatypes import Batch >>> # Define loss configurations for a hybrid model with both contrastive and supervised losses >>> losses = [ ... { ... 'indices': (0, 10), # First 10 dimensions ... 'contrastive_loss': { ... 'name': 'InfoNCE', # Using CEBRA's InfoNCE loss ... 'kwargs': {'temperature': 1.0} ... }, ... 'supervised_loss': { ... 'name': 'nn.MSELoss', # Using PyTorch's MSE loss ... 'kwargs': {} ... } ... }, ... { ... 'indices': (10, 20), # Next 10 dimensions ... 'contrastive_loss': { ... 'name': 'InfoNCE', # Using CEBRA's InfoNCE loss ... 'kwargs': {'temperature': 0.5} ... }, ... 'supervised_loss': { ... 'name': 'nn.L1Loss', # Using PyTorch's L1 loss ... 'kwargs': {} ... } ... } ... ] >>> # Create sample predictions (2 batches of 32 samples each with 10 features) >>> ref1 = torch.randn(32, 10) >>> pos1 = torch.randn(32, 10) >>> neg1 = torch.randn(32, 10) >>> ref2 = torch.randn(32, 10) >>> pos2 = torch.randn(32, 10) >>> neg2 = torch.randn(32, 10) >>> predictions = ( ... Batch(reference=ref1, positive=pos1, negative=neg1), ... Batch(reference=ref2, positive=pos2, negative=neg2) ... ) >>> # Create multi-criterion module in contrastive mode >>> multi_loss = MultiCriterions(losses, mode="contrastive") >>> # Forward pass with multiple predictions >>> losses = multi_loss(predictions) # Returns list of loss values >>> assert len(losses) == 2 # One loss per criterion
- class cebra.models.multiobjective.MultiobjectiveModel(module, dimensions, renormalize=False, output_mode='overlapping', append_last_dimension=False)#
Bases:
Module
Wrapper around contrastive learning models to all training with multiple objectives
Multi-objective training splits the last layer’s feature representation into multiple chunks, which are then used for individual training objectives.
- Parameters:
module (
Module
) – The module to wrapdimensions (
Tuple
[int
]) – A tuple of dimension values to extract from the model’s feature embedding.renormalize (
bool
) – If True, the individual feature slices will be re-normalized before getting returned—this option only makes sense in conjunction with a loss based on the cosine distance or dot product.output_mode (
str
) – A mode as defined inMultiobjectiveModel.Mode
. Overlapping means that whendimensions
are set to (x0, x1, …)`, features will be extracted from0:x0, 0:x1, ...
. When mode is set to separate, features are extracted fromx0:x1, x1:x2, ...
.append_last_dimension (
bool
) – Defaults to True, and will allow to omit the last dimension in thedimensions
argument (which should be equal to the output dimension) of the given model.
Note
This model will be deprecated in a future version. Please use the functionality in
cebra.models.multiobjective
instead, which provides more versatile multi-objective training capabilities. Instantiation of this model will raise a deprecation warning. The new model iscebra.models.multiobjective.SubspaceMultiobjectiveModel
which allows for unlimited subspaces and better configuration of the feature ranges.- class Mode#
Bases:
object
Mode for slicing and potentially normalizing the output embedding.
The options are:
OVERLAPPING
: Whendimensions
are set to (x0, x1, …)`, features will be extracted from0:x0, 0:x1, ...
.SEPARATE
: Features are extracted fromx0:x1, x1:x2, ...
- is_valid(mode)#
Check if a given string representation is valid.
- Parameters:
mode – String representation of the mode.
- Returns:
True
for a valid representation,False
otherwise.
- property get_offset#
- property num_output#
- forward(inputs)#
Compute multiple embeddings for a single signal input.
- Parameters:
inputs – The input tensor
- Returns:
A tuple of tensors which are sliced according to self.feature_ranges if renormalize is set to true, each of the tensors will be normalized across the first (feature) dimension.
- class cebra.models.multiobjective.SubspaceMultiobjectiveModel(module, feature_ranges, renormalize, split_outputs=True)#
Bases:
Module
Wrapper around contrastive learning models to all training with multiple objectives
Multi-objective training splits the last layer’s feature representation into multiple chunks, which are then used for individual training objectives.
- Parameters:
module (
Module
) – The module to wrapdimensions – A tuple of dimension values to extract from the model’s feature embedding.
renormalize (
bool
) – If True, the individual feature slices will be re-normalized before getting returned—this option only makes sense in conjunction with a loss based on the cosine distance or dot product.
- property get_offset#
- property num_output#
- forward(inputs)#
Compute multiple embeddings for a single signal input.
- Parameters:
inputs – The input tensor
- Returns:
A tuple of tensors which are sliced according to self.feature_ranges if renormalize is set to true, each of the tensors will be normalized across the first (feature) dimension.
- class cebra.models.multiobjective.SubspaceMultiobjectiveConvolutionalModel(module, feature_ranges, renormalize, split_outputs=True)#