class_docstr: Defines metrics/loss among all moment retrieval models.
  
  A model is class with three members: get_metrics_fn, loss_fn, & a flax_model.
  
  get_metrics_fn returns a callable function, metric_fn, that calculates the
  metrics and returns a dictionary. The metric function computes f(logits_i,
  batch_i) on a minibatch, it has API:
    ```metric_fn(logits, batch).```
  
  The trainer will then aggregate and compute the mean across all samples
  evaluated.
  
  loss_fn is a function of API
    loss = loss_fn(logits, batch, model_params=None).
  
  This model class defines two losses, sigmoid cross entropy for classification
  and IoU for boundary regression.
class_docstr_tok: ["Defines","metrics\/loss","among","all","moment","retrieval","models",".","A","model","is","class","with","three","members",":","get_metrics_fn",",","loss_fn",",","&","a","flax_model",".","get_metrics_fn","returns","a","callable","function",",","metric_fn",",","that","calculates","the","metrics","and","returns","a","dictionary",".","The","metric","function","computes","f","(","logits_i",",","batch_i",")","on","a","minibatch",",","it","has","API",":","``","`","metric_fn","(","logits",",","batch",")",".","``","`","The","trainer","will","then","aggregate","and","compute","the","mean","across","all","samples","evaluated",".","loss_fn","is","a","function","of","API","loss","=","loss_fn","(","logits",",","batch",",","model_params=None",")",".","This","model","class","defines","two","losses",",","sigmoid","cross","entropy","for","classification","and","IoU","for","boundary","regression","."]
docstr: Returns the sum of classification and box regression losses.
  
  Args:
    logits: (batch_size, batch_size * num_max_captions, num_frames, 3).
    batch: Batch of data.
    model_params: Parameters of the model, for optionally applying
      regularization.
  
  Returns:
    Total loss.
docstr_tok: ["Returns","the","sum","of","classification","and","box","regression","losses",".","Args",":","logits",":","(","batch_size",",","batch_size","*","num_max_captions",",","num_frames",",","3",")",".","batch",":","Batch","of","data",".","model_params",":","Parameters","of","the","model",",","for","optionally","applying","regularization",".","Returns",":","Total","loss","."]
code: def loss_function(
      self,
      logits: jnp.ndarray,
      batch: Batch,
      model_params: Optional[jnp.ndarray] = None,
  ) -> float:
      """Returns the sum of classification and box regression losses.
  
      Args:
        logits: (batch_size, batch_size * num_max_captions, num_frames, 3).
        batch: Batch of data.
        model_params: Parameters of the model, for optionally applying
          regularization.
  
      Returns:
        Total loss.
      """
      label = batch["label"]
      gt_displacements = batch["displacements"]
      class_logits = logits[..., :1]
      pred_displacements = logits[..., 1:]
      batch_mask = batch["batch_mask"]
      caption_mask = batch["inputs"]["caption_mask"]
      frame_mask = batch["inputs"]["input_mask"]
      box_loss = self._box_loss(
          batch_mask,
          caption_mask,
          frame_mask,
          pred_displacements,
          gt_displacements,
          label,
      )
      cls_loss = self._cls_loss(
          batch_mask, caption_mask, frame_mask, class_logits, label
      )
      return (
          self.config.get("classification_loss_alpha", 1.0) * cls_loss
          + box_loss
      )
  
imports: import functools
  from typing import Any, Dict, Mapping, Optional, Tuple, Union
  import immutabledict
  import jax.numpy
  import ml_collections
  from scenic.model_lib.base_models import base_model
  from scenic.model_lib.base_models import model_utils
  from scenic.projects.unloc import temporal_localization_base_model
label_desc: Use image node_id 4 for calling the MomentRetrievalModel obj's underlying member method code with example usage: obj.loss_function(logits, batch, model_params) and returns: unknown