docstr: gold_mention_bounds (bs, num_mentions, 2)
  gold_mention_bounds_mask (bs, num_mentions):
  mention_logits (bs, all_mentions)
  menion_bounds (bs, all_mentions, 2)
docstr_tok: ["gold_mention_bounds","(","bs",",","num_mentions",",","2",")","gold_mention_bounds_mask","(","bs",",","num_mentions",")",":","mention_logits","(","bs",",","all_mentions",")","menion_bounds","(","bs",",","all_mentions",",","2",")"]
code: def get_span_loss(
      self,
      gold_mention_bounds,
      gold_mention_bounds_mask,
      mention_logits,
      mention_bounds,
  ):
      """
      gold_mention_bounds (bs, num_mentions, 2)
      gold_mention_bounds_mask (bs, num_mentions):
      mention_logits (bs, all_mentions)
      menion_bounds (bs, all_mentions, 2)
      """
      loss_fct = nn.BCEWithLogitsLoss(reduction="mean")
  
      gold_mention_bounds[
          ~gold_mention_bounds_mask
      ] = -1  # ensure don't select masked to score
      # triples of [ex in batch, mention_idx in gold_mention_bounds, idx in mention_bounds]
      # use 1st, 2nd to index into gold_mention_bounds, 1st, 3rd to index into mention_bounds
      gold_mention_pos_idx = (
          (
              mention_bounds.unsqueeze(1)
              - gold_mention_bounds.unsqueeze(
                  2
              )  # (bs, num_mentions, start_pos * end_pos, 2)
          )
          .abs()
          .sum(-1)
          == 0
      ).nonzero()
      # gold_mention_pos_idx should have 1 entry per masked element
      # (num_gold_mentions [~gold_mention_bounds_mask])
      gold_mention_pos = gold_mention_pos_idx[:, 2]
  
      # (bs, total_possible_spans)
      gold_mention_binary = torch.zeros(
          mention_logits.size(), dtype=mention_logits.dtype
      ).to(gold_mention_bounds.device)
      gold_mention_binary[
          gold_mention_pos_idx[:, 0], gold_mention_pos_idx[:, 2]
      ] = 1
  
      # prune masked spans
      mask = mention_logits != -float("inf")
      masked_mention_logits = mention_logits[mask]
      masked_gold_mention_binary = gold_mention_binary[mask]
  
      # (bs, total_possible_spans)
      span_loss = loss_fct(
          masked_mention_logits, masked_gold_mention_binary
      )
  
      return span_loss
  
imports: import os
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from tqdm import tqdm
  from collections import OrderedDict
  from pytorch_transformers.modeling_bert import BertPreTrainedModel, BertConfig, BertModel
  from pytorch_transformers.tokenization_bert import BertTokenizer
  from elq.common.ranker_base import BertEncoder, get_model_obj
  from blink.common.optimizer import get_bert_optimizer
  from elq.biencoder.allennlp_span_utils import batched_span_select, batched_index_select
  from elq.biencoder.utils import batch_reshape_mask_left
label_desc: Use image node_id 10 for calling the BiEncoderRanker obj's underlying member method code with example usage: obj.get_span_loss(gold_mention_bounds, gold_mention_bounds_mask, mention_logits, mention_bounds) and returns: span_loss