class_docstr: <a id="MaximumLikelihoodLoss"></a>
  
  ## Type II Maximum Likelihood Loss
  
  The distribution $D(\mathbf{p} ert         extcolor{orange}{\mathbf{lpha}})$ is a prior on the likelihood
  $Multi(\mathbf{y} ert p)$,
   and the negative log marginal likelihood is calculated by integrating over class probabilities
   $\mathbf{p}$.
  
  If target probabilities (one-hot targets) are $y_k$ for a given sample the loss is,
  
  egin{align}
  \mathcal{L}(\Theta)
  &= -\log \Bigg(
   \int
    \prod_{k=1}^K p_k^{y_k}
    rac{1}{B(        extcolor{orange}{\mathbf{lpha}})}
    \prod_{k=1}^K p_k^{       extcolor{orange}{lpha_k} - 1}
   d\mathbf{p}
   \Bigg ) \
  &= \sum_{k=1}^K y_k igg( \log S - \log     extcolor{orange}{lpha_k} igg)
  \end{align}
class_docstr_tok: ["<","a","id=","''","MaximumLikelihoodLoss","''",">","<","\/a",">","#","#","Type","II","Maximum","Likelihood","Loss","The","distribution","$","D","(","\\mathbf","{","p","}","ert","extcolor","{","orange","}","{","\\mathbf","{","\u0007lpha","}","}",")","$","is","a","prior","on","the","likelihood","$","Multi","(","\\mathbf","{","y","}","ert","p",")","$",",","and","the","negative","log","marginal","likelihood","is","calculated","by","integrating","over","class","probabilities","$","\\mathbf","{","p","}","$",".","If","target","probabilities","(","one-hot","targets",")","are","$","y_k","$","for","a","given","sample","the","loss","is",",","\begin","{","align","}","\\mathcal","{","L","}","(","\\Theta",")","&","=","-\\log","\\Bigg","(","\\int","\\prod_","{","k=1","}","^K","p_k^","{","y_k","}","rac","{","1","}","{","B","(","extcolor","{","orange","}","{","\\mathbf","{","\u0007lpha","}","}",")","}","\\prod_","{","k=1","}","^K","p_k^","{","extcolor","{","orange","}","{","\u0007lpha_k","}","-","1","}","d\\mathbf","{","p","}","\\Bigg",")","\\","&","=","\\sum_","{","k=1","}","^K","y_k","\bigg","(","\\log","S","-","\\log","extcolor","{","orange","}","{","\u0007lpha_k","}","\bigg",")","\\end","{","align","}"]
docstr: * `evidence` is $\mathbf{e} \ge 0$ with shape `[batch_size, n_classes]`
  * `target` is $\mathbf{y}$ with shape `[batch_size, n_classes]`
docstr_tok: ["*","`","evidence","`","is","$","\\mathbf","{","e","}","\\ge","0","$","with","shape","`","[","batch_size",",","n_classes","]","`","*","`","target","`","is","$","\\mathbf","{","y","}","$","with","shape","`","[","batch_size",",","n_classes","]","`"]
code: def forward(self, evidence: torch.Tensor, target: torch.Tensor):
      """
      * `evidence` is $\mathbf{e} \ge 0$ with shape `[batch_size, n_classes]`
      * `target` is $\mathbf{y}$ with shape `[batch_size, n_classes]`
      """
      # $\textcolor{orange}{\alpha_k} = e_k + 1$
      alpha = evidence + 1.0
      # $S = \sum_{k=1}^K \textcolor{orange}{\alpha_k}$
      strength = alpha.sum(dim=-1)
  
      # Losses $\mathcal{L}(\Theta) = \sum_{k=1}^K y_k \bigg( \log S - \log \textcolor{orange}{\alpha_k} \bigg)$
      loss = (target * (strength.log()[:, None] - alpha.log())).sum(
          dim=-1
      )
  
      # Mean loss over the batch
      return loss.mean()
  
file_path: annotated_deep_learning_paper_implementations/labml_nn/uncertainty/evidence/__init__.py
imports: import torch
  from labml import tracker
  from labml_helpers.module import Module
label_desc: Use image node_id 1 for calling the MaximumLikelihoodLoss obj's underlying member method code with example usage: obj.forward(evidence, target) and returns: loss