class_docstr: Implementation of the VICRegL loss from VICRegL paper [0].
  
  This implementation follows the code published by the authors [1].
  
  - [0]: VICRegL, 2022, https://arxiv.org/abs/2210.01571
  - [1]: https://github.com/facebookresearch/VICRegL
  
  Attributes:
      lambda_param:
          Coefficient for the invariance term of the loss.
      mu_param:
          Coefficient for the variance term of the loss.
      nu_param:
          Coefficient for the covariance term of the loss.
      alpha:
          Coefficient to weight global with local loss. The final loss is computed as
          (self.alpha * global_loss + (1-self.alpha) * local_loss).
      gather_distributed:
          If True then the cross-correlation matrices from all gpus are gathered and
          summed before the loss calculation.
      eps:
          Epsilon for numerical stability.
      num_matches:
          Number of local features to match using nearest neighbors.
  
  Examples:
  
      >>> # initialize loss function
      >>> criterion = VICRegLLoss()
      >>> transform = VICRegLTransform(n_global_views=2, n_local_views=4)
      >>>
      >>> # generate two random transforms of images
      >>> views_and_grids = transform(images)
      >>> views = views_and_grids[:6] # 2 global views + 4 local views
      >>> grids = views_and_grids[6:]
      >>>
      >>> # feed through model images
      >>> features = [model(view) for view in views]
      >>>
      >>> # calculate loss
      >>> loss = criterion(
      ...     global_view_features=features[:2],
      ...     global_view_grids=grids[:2],
      ...     local_view_features=features[2:],
      ...     local_view_grids=grids[2:],
      ... )
class_docstr_tok: ["Implementation","of","the","VICRegL","loss","from","VICRegL","paper","[","0","]",".","This","implementation","follows","the","code","published","by","the","authors","[","1","]",".","-","[","0","]",":","VICRegL",",","2022",",","https",":","\/\/arxiv.org\/abs\/2210.01571","-","[","1","]",":","https",":","\/\/github.com\/facebookresearch\/VICRegL","Attributes",":","lambda_param",":","Coefficient","for","the","invariance","term","of","the","loss",".","mu_param",":","Coefficient","for","the","variance","term","of","the","loss",".","nu_param",":","Coefficient","for","the","covariance","term","of","the","loss",".","alpha",":","Coefficient","to","weight","global","with","local","loss",".","The","final","loss","is","computed","as","(","self.alpha","*","global_loss","+","(","1-self.alpha",")","*","local_loss",")",".","gather_distributed",":","If","True","then","the","cross-correlation","matrices","from","all","gpus","are","gathered","and","summed","before","the","loss","calculation",".","eps",":","Epsilon","for","numerical","stability",".","num_matches",":","Number","of","local","features","to","match","using","nearest","neighbors",".","Examples",":",">",">",">","#","initialize","loss","function",">",">",">","criterion","=","VICRegLLoss","(",")",">",">",">","transform","=","VICRegLTransform","(","n_global_views=2",",","n_local_views=4",")",">",">",">",">",">",">","#","generate","two","random","transforms","of","images",">",">",">","views_and_grids","=","transform","(","images",")",">",">",">","views","=","views_and_grids","[",":6","]","#","2","global","views","+","4","local","views",">",">",">","grids","=","views_and_grids","[","6",":","]",">",">",">",">",">",">","#","feed","through","model","images",">",">",">","features","=","[","model","(","view",")","for","view","in","views","]",">",">",">",">",">",">","#","calculate","loss",">",">",">","loss","=","criterion","(","...","global_view_features=features","[",":2","]",",","...","global_view_grids=grids","[",":2","]",",","...","local_view_features=features","[","2",":","]",",","...","local_view_grids=grids","[","2",":","]",",","...",")"]
docstr: Computes the global and local VICRegL loss from the input features.
  
  Args:
      global_view_features:
          Sequence of (global_features, local_features) tuples from the global
          crop views. global_features must have size
          (batch_size, global_feature_dim) and local_features must have size
          (batch_size, grid_height, grid_width, local_feature_dim).
      global_view_grids:
          Sequence of grid tensors from the global crop views. Every tensor must
          have shape (batch_size, grid_height, grid_width, 2).
      local_view_features:
          Sequence of (global_features, local_features) tuples from the local crop
          views. global_features must have size
          (batch_size, global_feature_dim) and local_features must have size
          (batch_size, grid_height, grid_width, local_feature_dim). Note that
          grid_height and grid_width can differ between global_view_features and
          local_view_features.
      local_view_grids:
          Sequence of grid tensors from the local crop views. Every tensor must
          have shape (batch_size, grid_height, grid_width, 2). Note that
          grid_height and grid_width can differ between global_view_features and
          local_view_features.
  
  Returns:
      Weighted sum of the global and local loss, calculated as:
      `self.alpha * global_loss + (1-self.alpha) * local_loss`.
docstr_tok: ["Computes","the","global","and","local","VICRegL","loss","from","the","input","features",".","Args",":","global_view_features",":","Sequence","of","(","global_features",",","local_features",")","tuples","from","the","global","crop","views",".","global_features","must","have","size","(","batch_size",",","global_feature_dim",")","and","local_features","must","have","size","(","batch_size",",","grid_height",",","grid_width",",","local_feature_dim",")",".","global_view_grids",":","Sequence","of","grid","tensors","from","the","global","crop","views",".","Every","tensor","must","have","shape","(","batch_size",",","grid_height",",","grid_width",",","2",")",".","local_view_features",":","Sequence","of","(","global_features",",","local_features",")","tuples","from","the","local","crop","views",".","global_features","must","have","size","(","batch_size",",","global_feature_dim",")","and","local_features","must","have","size","(","batch_size",",","grid_height",",","grid_width",",","local_feature_dim",")",".","Note","that","grid_height","and","grid_width","can","differ","between","global_view_features","and","local_view_features",".","local_view_grids",":","Sequence","of","grid","tensors","from","the","local","crop","views",".","Every","tensor","must","have","shape","(","batch_size",",","grid_height",",","grid_width",",","2",")",".","Note","that","grid_height","and","grid_width","can","differ","between","global_view_features","and","local_view_features",".","Returns",":","Weighted","sum","of","the","global","and","local","loss",",","calculated","as",":","`","self.alpha","*","global_loss","+","(","1-self.alpha",")","*","local_loss","`","."]
code: def forward(
      self,
      global_view_features: Sequence[Tuple[Tensor, Tensor]],
      global_view_grids: Sequence[Tensor],
      local_view_features: Optional[
          Sequence[Tuple[Tensor, Tensor]]
      ] = None,
      local_view_grids: Optional[Sequence[Tensor]] = None,
  ) -> Tensor:
      """Computes the global and local VICRegL loss from the input features.
  
      Args:
          global_view_features:
              Sequence of (global_features, local_features) tuples from the global
              crop views. global_features must have size
              (batch_size, global_feature_dim) and local_features must have size
              (batch_size, grid_height, grid_width, local_feature_dim).
          global_view_grids:
              Sequence of grid tensors from the global crop views. Every tensor must
              have shape (batch_size, grid_height, grid_width, 2).
          local_view_features:
              Sequence of (global_features, local_features) tuples from the local crop
              views. global_features must have size
              (batch_size, global_feature_dim) and local_features must have size
              (batch_size, grid_height, grid_width, local_feature_dim). Note that
              grid_height and grid_width can differ between global_view_features and
              local_view_features.
          local_view_grids:
              Sequence of grid tensors from the local crop views. Every tensor must
              have shape (batch_size, grid_height, grid_width, 2). Note that
              grid_height and grid_width can differ between global_view_features and
              local_view_features.
  
      Returns:
          Weighted sum of the global and local loss, calculated as:
          `self.alpha * global_loss + (1-self.alpha) * local_loss`.
      """
      if len(global_view_features) != len(global_view_grids):
          raise ValueError(
              f"global_view_features and global_view_grids must have same length "
              f"but found {len(global_view_features)} and {len(global_view_grids)}."
          )
      if (
          local_view_features is not None
          and local_view_grids is not None
      ):
          if len(local_view_features) != len(local_view_grids):
              raise ValueError(
                  f"local_view_features and local_view_grids must have same length "
                  f"but found {len(local_view_features)} and {len(local_view_grids)}."
              )
      elif (
          local_view_features is not None
          or local_view_grids is not None
      ):
          raise ValueError(
              f"local_view_features and local_view_grids must either both be set or "
              f"None but found {type(local_view_features)} and {type(local_view_grids)}."
          )
  
      # calculate loss from global features
      global_loss = self._global_loss(
          global_view_features=global_view_features,
          local_view_features=local_view_features,
      )
  
      # calculate loss from local features
      local_loss = self._local_loss(
          global_view_features=global_view_features,
          global_view_grids=global_view_grids,
          local_view_features=local_view_features,
          local_view_grids=local_view_grids,
      )
  
      loss = self.alpha * global_loss + (1 - self.alpha) * local_loss
      return loss
  
file_path: lightly/lightly/loss/vicregl_loss.py
imports: from typing import Optional, Sequence, Tuple
  import torch
  import torch.distributed
  from torch import Tensor
  from lightly.loss.vicreg_loss import VICRegLoss, covariance_loss, invariance_loss, variance_loss
  from lightly.models.utils import nearest_neighbors
  from lightly.utils.dist import gather
label_desc: Use image node_id 2 for calling the VICRegLLoss obj's underlying member method code with example usage: obj.forward(global_view_features, global_view_grids, local_view_features, local_view_grids) and returns: loss