code: def label_smoothed_nll_loss(
      lprobs: torch.Tensor,
      target: torch.Tensor,
      epsilon: float,
      ignore_index=None,
      reduction="mean",
      dim=-1,
  ) -> torch.Tensor:
      """
  
      Source: https://github.com/pytorch/fairseq/blob/master/fairseq/criterions/label_smoothed_cross_entropy.py
  
      :param lprobs: Log-probabilities of predictions (e.g after log_softmax)
      :param target:
      :param epsilon:
      :param ignore_index:
      :param reduction:
      :return:
      """
      if target.dim() == lprobs.dim() - 1:
          target = target.unsqueeze(dim)
  
      if ignore_index is not None:
          pad_mask = target.eq(ignore_index)
          target = target.masked_fill(pad_mask, 0)
          nll_loss = -lprobs.gather(dim=dim, index=target)
          smooth_loss = -lprobs.sum(dim=dim, keepdim=True)
  
          # nll_loss.masked_fill_(pad_mask, 0.0)
          # smooth_loss.masked_fill_(pad_mask, 0.0)
          nll_loss = nll_loss.masked_fill(pad_mask, 0.0)
          smooth_loss = smooth_loss.masked_fill(pad_mask, 0.0)
      else:
          nll_loss = -lprobs.gather(dim=dim, index=target)
          smooth_loss = -lprobs.sum(dim=dim, keepdim=True)
  
          nll_loss = nll_loss.squeeze(dim)
          smooth_loss = smooth_loss.squeeze(dim)
  
      if reduction == "sum":
          nll_loss = nll_loss.sum()
          smooth_loss = smooth_loss.sum()
      if reduction == "mean":
          nll_loss = nll_loss.mean()
          smooth_loss = smooth_loss.mean()
  
      eps_i = epsilon / lprobs.size(dim)
      loss = (1.0 - epsilon) * nll_loss + eps_i * smooth_loss
      return loss
  
file_path: pytorch-toolbelt/pytorch_toolbelt/losses/functional.py
imports: import math
  from typing import Optional
  import torch
  import torch.nn.functional
  from ..utils import pytorch_toolbelt_deprecated
label_desc: Use image node_id 8 for calling a global function with example usage: label_smoothed_nll_loss(lprobs, target, epsilon, ignore_index, reduction, dim) and returns: loss