class_docstr: Plain Vision Transformer.
class_docstr_tok: ["Plain","Vision","Transformer","."]
docstr: Returns sigmoid or softmax cross entropy loss.
  
  Args:
    logits: Output of model in shape [batch, length, num_classes].
    batch: Batch of data that has 'label' and optionally 'batch_mask'.
    model_params: Parameters of the model, for optionally applying
      regularization.
  
  Returns:
    Total loss.
docstr_tok: ["Returns","sigmoid","or","softmax","cross","entropy","loss",".","Args",":","logits",":","Output","of","model","in","shape","[","batch",",","length",",","num_classes","]",".","batch",":","Batch","of","data","that","has","'label","'","and","optionally","'batch_mask","'",".","model_params",":","Parameters","of","the","model",",","for","optionally","applying","regularization",".","Returns",":","Total","loss","."]
code: def loss_function(
      self,
      logits: jnp.ndarray,
      batch: base_model.Batch,
      model_params: Optional[jnp.ndarray] = None,
  ) -> float:
      """Returns sigmoid or softmax cross entropy loss.
  
      Args:
        logits: Output of model in shape [batch, length, num_classes].
        batch: Batch of data that has 'label' and optionally 'batch_mask'.
        model_params: Parameters of the model, for optionally applying
          regularization.
  
      Returns:
        Total loss.
      """
      weights = batch.get("batch_mask")
      loss_fn = self.config.get("loss", "sigmoid_xent")
  
      if self.dataset_meta_data.get("target_is_onehot", False):
          one_hot_targets = batch["label"]
      else:
          # This is to support running a multi-label classification model on
          # single-label classification tasks.
          one_hot_targets = common_utils.onehot(
              batch["label"], logits.shape[-1]
          )
  
      if loss_fn == "sigmoid_xent":
          total_loss = model_utils.weighted_sigmoid_cross_entropy(
              logits,
              one_hot_targets,
              weights,
              label_smoothing=self.config.get("label_smoothing"),
          )
      elif loss_fn == "softmax_xent":
          total_loss = model_utils.weighted_softmax_cross_entropy(
              logits,
              one_hot_targets,
              weights,
              label_smoothing=self.config.get("label_smoothing"),
          )
      else:
          raise ValueError(f"Unknown loss function {loss_fn}.")
  
      if self.config.get("l2_decay_factor"):
          l2_loss = model_utils.l2_regularization(model_params)
          total_loss = (
              total_loss + 0.5 * self.config.l2_decay_factor * l2_loss
          )
      return total_loss
  
imports: import functools
  from typing import Any, Optional
  from absl import logging
  import flax
  import flax.linen
  from flax.training import common_utils
  import jax.numpy
  import ml_collections
  import numpy
  from scenic.model_lib.base_models import base_model
  from scenic.model_lib.base_models import classification_model
  from scenic.model_lib.base_models import model_utils
  from scenic.model_lib.base_models import multilabel_classification_model
  from scenic.model_lib.layers import nn_layers
  import scipy
label_desc: Use image node_id 2 for calling the PlainViT obj's underlying member method code with example usage: obj.loss_function(logits, batch, model_params) and returns: total_loss