class_docstr: Label smoothing loss with sample-wise logging.
docstr: Class-specific reduction function to extract sample-wise losses. Currently,
  passing in reduce="mean" averages over all samples without providing access
  to sample-wise losses.
docstr_tok: ["Class-specific","reduction","function","to","extract","sample-wise","losses",".","Currently",",","passing","in","reduce=","''","mean","''","averages","over","all","samples","without","providing","access","to","sample-wise","losses","."]
code: def _reduce_mean(
      self,
      logits,
      targets,
      batch_size,
      label_loss,
      smoothing_loss,
      reduce=True,
  ):
      """
      Class-specific reduction function to extract sample-wise losses. Currently,
      passing in reduce="mean" averages over all samples without providing access
      to sample-wise losses.
      """
  
      # Save original losses.
      orig_label_loss = label_loss.clone()
      orig_smoothing_loss = smoothing_loss.clone()
  
      # Create target mask for pad tokens.
      mask = targets.ne(self.ignore_index)
  
      if mask.any():
          # Guarantee ignored tokens have zero contribution to loss.
          label_loss[~mask] = 0
          smoothing_loss[~mask] = 0
  
          # Lengths after masking.
          lengths = torch.sum(mask.reshape(batch_size, -1), dim=1)
  
          # Sample-wise losses (we do not consider masked tokens in this loss).
          samplewise_label_loss = (
              torch.sum(label_loss.reshape(batch_size, -1), dim=-1)
              / lengths
          )
          samplewise_smoothing_loss = (
              torch.sum(smoothing_loss.reshape(batch_size, -1), dim=-1)
              / lengths
          )
  
          # Replace NaNs with zero (only happens with zero length samples).
          samplewise_label_loss[torch.isnan(samplewise_label_loss)] = 0
          samplewise_smoothing_loss[
              torch.isnan(samplewise_smoothing_loss)
          ] = 0
  
          # Update original loss to use non-masked samples.
          label_loss = label_loss[mask]
          smoothing_loss = smoothing_loss[mask]
      else:
          samplewise_label_loss = torch.zeros(
              batch_size, device=logits.device
          )
          samplewise_smoothing_loss = torch.zeros(
              batch_size, device=logits.device
          )
          label_loss = torch.zeros(mask.shape, device=logits.shape)
          smoothing_loss = torch.zeros(mask.shape, device=logits.shape)
  
      # If `reduce` is enabled, compute mean loss over sequence. Otherwise,
      # revert values before masking.
      label_loss = torch.mean(label_loss) if reduce else orig_label_loss
      smoothing_loss = (
          torch.mean(smoothing_loss) if reduce else orig_smoothing_loss
      )
  
      return (
          samplewise_label_loss,
          samplewise_smoothing_loss,
          label_loss,
          smoothing_loss,
      )
  
imports: from typing import Union
  import torch
  from pytext.config import ConfigBase
  from pytext.config.component import create_loss
  from .loss import HingeLoss, Loss, maybe_log_normalize, NLLLoss, SourceType
  from .regularizer import AdaptiveRegularizer, EntropyRegularizer, UniformRegularizer
  from .structured_loss import StructuredLoss, StructuredMarginLoss
label_desc: Use image node_id 2 for calling the SamplewiseLabelSmoothingLoss obj's underlying member method code with example usage: obj._reduce_mean(logits, targets, batch_size, label_loss, smoothing_loss, reduce) and returns: samplewise_label_loss,  samplewise_smoothing_loss,  label_loss,  smoothing_loss