class_docstr: <a id="CrossEntropyBayesRisk"></a>
  
  ## Bayes Risk with Cross Entropy Loss
  
  Bayes risk is the overall maximum cost of making incorrect estimates.
  It takes a cost function that gives the cost of making an incorrect estimate
  and sums it over all possible outcomes based on probability distribution.
  
  Here the cost function is cross-entropy loss, for one-hot coded $\mathbf{y}$
  $$\sum_{k=1}^K -y_k \log p_k$$
  
  We integrate this cost over all $\mathbf{p}$
  
  egin{align}
  \mathcal{L}(\Theta)
  &= -\log \Bigg(
   \int
    \Big[ \sum_{k=1}^K -y_k \log p_k \Big]
    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( \psi(S) - \psi(   extcolor{orange}{lpha_k} ) igg)
  \end{align}
  
  where $\psi(\cdot)$ is the $digamma$ function.
class_docstr_tok: ["<","a","id=","''","CrossEntropyBayesRisk","''",">","<","\/a",">","#","#","Bayes","Risk","with","Cross","Entropy","Loss","Bayes","risk","is","the","overall","maximum","cost","of","making","incorrect","estimates",".","It","takes","a","cost","function","that","gives","the","cost","of","making","an","incorrect","estimate","and","sums","it","over","all","possible","outcomes","based","on","probability","distribution",".","Here","the","cost","function","is","cross-entropy","loss",",","for","one-hot","coded","$","\\mathbf","{","y","}","$","$","$","\\sum_","{","k=1","}","^K","-y_k","\\log","p_k","$","$","We","integrate","this","cost","over","all","$","\\mathbf","{","p","}","$","\begin","{","align","}","\\mathcal","{","L","}","(","\\Theta",")","&","=","-\\log","\\Bigg","(","\\int","\\Big","[","\\sum_","{","k=1","}","^K","-y_k","\\log","p_k","\\Big","]","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","(","\\psi","(","S",")","-","\\psi","(","extcolor","{","orange","}","{","\u0007lpha_k","}",")","\bigg",")","\\end","{","align","}","where","$","\\psi","(","\\cdot",")","$","is","the","$","digamma","$","function","."]
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( \psi(S) - \psi( \textcolor{orange}{\alpha_k} ) \bigg)$
      loss = (
          target
          * (torch.digamma(strength)[:, None] - torch.digamma(alpha))
      ).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 CrossEntropyBayesRisk obj's underlying member method code with example usage: obj.forward(evidence, target) and returns: loss