class_docstr: Video Transformer model for multiple n-way classification.
class_docstr_tok: ["Video","Transformer","model","for","multiple","n-way","classification","."]
docstr: Return softmax cross entropy loss with an L2 penalty on the weights.
code: def loss_function(
      self,
      logits: jnp.ndarray,
      batch: base_model.Batch,
      model_params: Optional[jnp.ndarray] = None,
  ) -> float:
      """Return softmax cross entropy loss with an L2 penalty on the weights."""
      weights = batch.get("batch_mask")
  
      if self.dataset_meta_data.get("target_is_onehot", False):
          one_hot_targets = batch["label"]
      else:
          raise ValueError("Target labels should be one-hot.")
  
      if logits.shape[-1] != self.class_splits[-1]:
          raise AssertionError(
              "Logit dimension must be equal to number of classes"
          )
  
      logit_splits = jnp.split(logits, self.class_splits, axis=-1)[:-1]
      one_hot_target_splits = jnp.split(
          one_hot_targets, self.class_splits, axis=-1
      )[:-1]
      label_smoothing = self.config.get("label_smoothing")
  
      sof_ce_losses = [
          base_model_utils.weighted_softmax_cross_entropy(
              logits, one_hot_targets, weights, label_smoothing
          )
          for logits, one_hot_targets in zip(
              logit_splits, one_hot_target_splits
          )
      ]
      sof_ce_loss = jnp.mean(jnp.array(sof_ce_losses))
  
      if self.config.get("l2_decay_factor") is None:
          total_loss = sof_ce_loss
      else:
          l2_loss = base_model_utils.l2_regularization(model_params)
          total_loss = (
              sof_ce_loss + 0.5 * self.config.l2_decay_factor * l2_loss
          )
      return total_loss
  
imports: import functools
  from typing import Any, Optional, Callable, Sequence
  from absl import logging
  import flax.linen
  from flax.linen.linear import default_kernel_init
  from immutabledict import immutabledict
  import jax
  import jax.numpy
  import ml_collections
  import numpy
  from scenic.common_lib import video_utils
  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.classification_model import ClassificationModel
  from scenic.model_lib.layers import attention_layers
  from scenic.model_lib.layers import nn_layers
  from scenic.projects.baselines import vit
  from scenic.projects.vivit import model_utils
label_desc: Use image node_id 2 for calling the ViViTMultiHeadClassificationModel obj's underlying member method code with example usage: obj.loss_function(logits, batch, model_params) and returns: total_loss