class_docstr: ## Margin loss for class existence
  
  A separate margin loss is used for each output capsule and the total loss is the sum of them.
  The length of each output capsule is the probability that class is present in the input.
  
  Loss for each output capsule or class $k$ is,
  $$\mathcal{L}_k = T_k \max(0, m^{+} - \lVert\mathbf{v}_k
  Vert)^2 +
  \lambda (1 - T_k) \max(0, \lVert\mathbf{v}_k
  Vert - m^{-})^2$$
  
  $T_k$ is $1$ if the class $k$ is present and $0$ otherwise.
  The first component of the loss is $0$ when the class is not present,
  and the second component is $0$ if the class is present.
  The $\max(0, x)$ is used to avoid predictions going to extremes.
  $m^{+}$ is set to be $0.9$ and $m^{-}$ to be $0.1$ in the paper.
  
  The $\lambda$ down-weighting is used to stop the length of all capsules from
  falling during the initial phase of training.
class_docstr_tok: ["#","#","Margin","loss","for","class","existence","A","separate","margin","loss","is","used","for","each","output","capsule","and","the","total","loss","is","the","sum","of","them",".","The","length","of","each","output","capsule","is","the","probability","that","class","is","present","in","the","input",".","Loss","for","each","output","capsule","or","class","$","k","$","is",",","$","$","\\mathcal","{","L","}","_k","=","T_k","\\max","(","0",",","m^","{","+","}","-","\\lVert\\mathbf","{","v","}","_k","Vert",")","^2","+","\\lambda","(","1","-","T_k",")","\\max","(","0",",","\\lVert\\mathbf","{","v","}","_k","Vert","-","m^","{","-","}",")","^2","$","$","$","T_k","$","is","$","1","$","if","the","class","$","k","$","is","present","and","$","0","$","otherwise",".","The","first","component","of","the","loss","is","$","0","$","when","the","class","is","not","present",",","and","the","second","component","is","$","0","$","if","the","class","is","present",".","The","$","\\max","(","0",",","x",")","$","is","used","to","avoid","predictions","going","to","extremes",".","$","m^","{","+","}","$","is","set","to","be","$","0.9","$","and","$","m^","{","-","}","$","to","be","$","0.1","$","in","the","paper",".","The","$","\\lambda","$","down-weighting","is","used","to","stop","the","length","of","all","capsules","from","falling","during","the","initial","phase","of","training","."]
code: def __init__(
      self,
      *,
      n_labels: int,
      lambda_: float = 0.5,
      m_positive: float = 0.9,
      m_negative: float = 0.1,
  ):
      super().__init__()
  
      self.m_negative = m_negative
      self.m_positive = m_positive
      self.lambda_ = lambda_
      self.n_labels = n_labels
  
file_path: annotated_deep_learning_paper_implementations/labml_nn/capsule_networks/__init__.py
imports: import torch.nn
  import torch.nn.functional
  import torch.utils.data
  from labml_helpers.module import Module
label_desc: Use image node_id 1 to create a new MarginLoss object from inherited base classes: Module with example: obj = MarginLoss()