sadaco.apis.losses package

Submodules

sadaco.apis.losses.BasicLoss module

class sadaco.apis.losses.BasicLoss.CELoss(mode: Union[str, int] = 'onehot', **kwargs)[source]

Bases: torch.nn.modules.loss.CrossEntropyLoss

_summary_

Parameters

CrossEntropyLoss (_type_) – _description_

__init__(mode: Union[str, int] = 'onehot', **kwargs)[source]

_summary_

Parameters

mode (Union[str, int], optional) – _description_, defaults to ‘onehot’

Raises

ValueError – _description_

forward(output, label, **kwargs)[source]

_summary_

Parameters
  • output (_type_) – _description_

  • label (_type_) – _description_

Returns

_description_

Return type

_type_

ignore_index: int
label_smoothing: float
class sadaco.apis.losses.BasicLoss.BCEWithLogitsLoss(mode: Union[str, int] = 'multihot', max=None, **kwargs)[source]

Bases: torch.nn.modules.loss.BCEWithLogitsLoss

_summary_

Parameters

BCEWithLogitsLoss (_type_) – _description_

__init__(mode: Union[str, int] = 'multihot', max=None, **kwargs)[source]

_summary_

Parameters
  • mode (Union[str, int], optional) – _description_, defaults to ‘multihot’

  • max (_type_, optional) – _description_, defaults to None

Raises
  • ValueError – _description_

  • ValueError – _description_

forward(output: torch.Tensor, label: torch.Tensor, **kwargs)[source]

_summary_

Parameters
  • input (torch.Tensor) – _description_

  • label (torch.Tensor) – _description_

Returns

_description_

Return type

_type_

reduction: str
class sadaco.apis.losses.BasicLoss.Normalized_MSELoss(**kwargs)[source]

Bases: torch.nn.modules.loss.MSELoss

A modified version of the MSELoss for non-constrastive self-supervised learning in BYOL, which is between the normalized predictions and target projections.

Parameters

MSELoss (Loss fn) – Parent Loss ‘MSELoss’ from pytorch

Returns

MSE Loss value

Return type

torch.Tensor

reduction: str
forward(predictions: torch.Tensor, target_projections: torch.Tensor, **kwargs)[source]

sadaco.apis.losses.ContrastiveLoss module

class sadaco.apis.losses.ContrastiveLoss.SupervisedContrastiveLoss(temperature=0.07, contrast_mode='all', base_temperature=0.07)[source]

Bases: torch.nn.modules.module.Module

This SupConLoss is almost identical to the code of original repository https://github.com/HobbitLong/SupContrast

forward(features, label=None, mask=None, breaker=False, **kwargs)[source]

Compute loss for model. If both labels and mask are None, it degenerates to SimCLR unsupervised loss: https://arxiv.org/pdf/2002.05709.pdf

Parameters
  • features – hidden vector of shape [bsz, n_views, …].

  • labels – ground truth of shape [bsz].

  • mask – contrastive mask of shape [bsz, bsz], mask_{i,j}=1 if sample j has the same class as sample i. Can be asymmetric.

Returns

A loss scalar.

training: bool

sadaco.apis.losses.CustomLoss module

sadaco.apis.losses.CustomLoss.mixup_criterion(criterion: Callable, **criterion_options)[source]

Module contents