code: def hn_nce_loss_with_hardnegs(
      logits, labels, n=None, beta=0, masking=None
  ):
      """Returns HN-NCE loss when considering verb hard negatives."""
      # The weights will compare how similar is each text compared to the average
      ws = jnp.exp(
          beta * logits - jnp.max(beta * logits, axis=-1, keepdims=True)
      )
      # Zeroing the positive elements
      ws = (1 != labels) * ws
      # and the masked elements...
      if masking is not None:
          ws = (-1000000 != masking) * ws
      # Substracting the mean of each row.
      if n is None:
          n = ws.shape[-1]
      ws = (n - 1) * ws / jnp.sum(ws, axis=-1, keepdims=True)
  
      # Setting to 1 the positive elements so that they participate in the sum in
      # the denominator.
      ws = ws + labels
  
      exp_logits = jnp.exp(
          logits - jnp.max(logits, axis=-1, keepdims=True)
      )
      ws = ws * exp_logits
      loss = -jnp.sum(
          labels
          * jnp.log(exp_logits / jnp.sum(ws, axis=-1, keepdims=True)),
          axis=-1,
      )
      return loss
  
file_path: scenic/scenic/projects/verbs_in_action/losses.py
imports: from typing import Tuple
  import flax.linen
  import jax.numpy
  import numpy
  from scenic.projects.verbs_in_action import utils
label_desc: Use image node_id 4 for calling a global function with example usage: hn_nce_loss_with_hardnegs(logits, labels, n, beta, masking) and returns: loss