class_docstr: Performs Non-Parametric Instance Discrimination for self supervised learning on
  video. A memory bank is built to keep and update the historical feature embedding
  and use them for contrastive learning.
  
  The original paper is:
  Unsupervised Feature Learning via Non-Parametric Instance Discrimination
  https://arxiv.org/pdf/1805.01978.pdf
  
  More details can be found from the memory bank part in the following paper:
  Momentum Contrast for Unsupervised Visual Representation Learning
  https://arxiv.org/pdf/1911.05722.pdf
class_docstr_tok: ["Performs","Non-Parametric","Instance","Discrimination","for","self","supervised","learning","on","video",".","A","memory","bank","is","built","to","keep","and","update","the","historical","feature","embedding","and","use","them","for","contrastive","learning",".","The","original","paper","is",":","Unsupervised","Feature","Learning","via","Non-Parametric","Instance","Discrimination","https",":","\/\/arxiv.org\/pdf\/1805.01978.pdf","More","details","can","be","found","from","the","memory","bank","part","in","the","following","paper",":","Momentum","Contrast","for","Unsupervised","Visual","Representation","Learning","https",":","\/\/arxiv.org\/pdf\/1911.05722.pdf"]
docstr: Perform contrastive learning with random sampled negative instance from the
      memory bank. During training, update the memory bank with latest feature
      embedding.
  Args:
      x (torch.tensor): a batch of image with augmentation. The input tensor
          shape should able to be feed into the backbone.
      x_ind (torch.tensor): the index of the image x from the dataset. Expected
          shape is B.
docstr_tok: ["Perform","contrastive","learning","with","random","sampled","negative","instance","from","the","memory","bank",".","During","training",",","update","the","memory","bank","with","latest","feature","embedding",".","Args",":","x","(","torch.tensor",")",":","a","batch","of","image","with","augmentation",".","The","input","tensor","shape","should","able","to","be","feed","into","the","backbone",".","x_ind","(","torch.tensor",")",":","the","index","of","the","image","x","from","the","dataset",".","Expected","shape","is","B","."]
code: def forward(
      self, x: torch.Tensor, x_ind: torch.Tensor
  ) -> torch.Tensor:
      """
      Perform contrastive learning with random sampled negative instance from the
          memory bank. During training, update the memory bank with latest feature
          embedding.
      Args:
          x (torch.tensor): a batch of image with augmentation. The input tensor
              shape should able to be feed into the backbone.
          x_ind (torch.tensor): the index of the image x from the dataset. Expected
              shape is B.
      """
      batch_size = x.shape[0]
      x = self.backbone(x)
      if self.mlp is not None:
          x = self.mlp(x)
      # Normalize the output embedding before multiplication.
      x = F.normalize(x, p=2, dim=1)
      # Random sample negative instances from the memory bank.
      idx = torch.randint(
          0, self.bank_size, size=(batch_size, self.neg_size + 1)
      ).to(x.device)
      # Fill the first with positive instances.
      idx.select(1, 0).copy_(x_ind.data)
      weight = torch.index_select(self.memory, 0, idx.view(-1)).detach()
      weight = weight.view(batch_size, self.neg_size + 1, self.dim)
      # Multiplication for contrastive learning.
      out = torch.einsum("bkc,bc->bk", weight, x)
      out = torch.div(out, self.temperature)
      gt = torch.zeros((batch_size,), device=x.device, dtype=torch.long)
      loss = torch.nn.functional.cross_entropy(out, gt)
      # Update memory during training.
      if self.training:
          with torch.no_grad():
              pos = torch.index_select(self.memory, 0, x_ind.view(-1))
              pos.mul_(self.mmt)
              pos.add_(torch.mul(x, 1 - self.mmt))
              norm = pos.pow(2).sum(1, keepdim=True).pow(0.5)
              updated = pos.div(norm)
              self.memory.index_copy_(0, x_ind, updated)
      return loss
  
file_path: pytorchvideo/pytorchvideo/models/memory_bank.py
imports: import math
  from typing import Optional
  import torch
  import torch.nn
  import torch.nn.functional
  from pytorchvideo.layers.utils import set_attributes
label_desc: Use image node_id 3 for calling the MemoryBank obj's underlying member method code with example usage: obj.forward(x, x_ind) and returns: loss