class_docstr: Computes the binary cross entropy loss over a bag-of-words of target tokens.
class_docstr_tok: ["Computes","the","binary","cross","entropy","loss","over","a","bag-of-words","of","target","tokens","."]
docstr: pred: (batch_size, num_vocab) probabilities.
  labels: (batch_size, target_length) words.
code: def forward(self, output: pt.Tensor, label: pt.Tensor):
      """
      pred: (batch_size, num_vocab) probabilities.
      labels: (batch_size, target_length) words.
      """
      nvs_pred = output
  
      bow = _label_to_bow(label, self._num_labels)
  
      # Set automatically using positive and negative counts
      num_positive = pt.sum(bow).float()
      num_total = bow.shape[0] * bow.shape[1]
      num_negative = num_total - num_positive
      pos_weight = self.pos_weight * num_negative / num_positive
  
      # instead of normalizing 1/num_labels, as done by the ce block, we want to also
      # normalize by the virtual positive counts implied by the pos_weight
      # Everything is one per sentence, so we get the average positive cases
      # convert it to the additional (therefore pos_weight-1) implied counts
      # and renormalize
      avg_pos_count = pt.mean(pt.sum(bow, dim=1).float())
      implied_pos_count = avg_pos_count * (pos_weight - 1)
      scale = 1.0 / (self._num_labels + implied_pos_count)
  
      # shape: (batch_size, vocab_size)
      loss = self.ce_loss(nvs_pred, bow, pos_weight)
  
      # shape: (batch_size,)
      loss = pt.sum(loss, 1) * scale
  
      # Remove the batch dimension
      # (1,)
      ce = pt.mean(loss) * self.weight
  
      return ce, pt.ones(1, device=ce.device)
  
imports: import logging
  import math
  from abc import ABC, abstractmethod
  from typing import Any, Dict, Optional, Tuple
  import torch
  import numpy
  from .None import constants
  from .None import utils
label_desc: Use image node_id 2 for calling the BinaryCrossEntropyBowLoss obj's underlying member method code with example usage: obj.forward(output, label) and returns: ce,  pt