class_docstr: Copy generator criterion.
class_docstr_tok: ["Copy","generator","criterion","."]
docstr: Args:
      scores (FloatTensor): ``(batch_size*tgt_len)`` x dynamic vocab size
          whose sum along dim 1 is less than or equal to 1, i.e. cols
          softmaxed.
      align (LongTensor): ``(batch_size x tgt_len)``
      target (LongTensor): ``(batch_size x tgt_len)``
docstr_tok: ["Args",":","scores","(","FloatTensor",")",":","``","(","batch_size","*","tgt_len",")","``","x","dynamic","vocab","size","whose","sum","along","dim","1","is","less","than","or","equal","to","1",",","i.e",".","cols","softmaxed",".","align","(","LongTensor",")",":","``","(","batch_size","x","tgt_len",")","``","target","(","LongTensor",")",":","``","(","batch_size","x","tgt_len",")","``"]
code: def forward(self, scores, align, target):
      """
      Args:
          scores (FloatTensor): ``(batch_size*tgt_len)`` x dynamic vocab size
              whose sum along dim 1 is less than or equal to 1, i.e. cols
              softmaxed.
          align (LongTensor): ``(batch_size x tgt_len)``
          target (LongTensor): ``(batch_size x tgt_len)``
      """
      # probabilities assigned by the model to the gold targets
      vocab_probs = scores.gather(1, target.unsqueeze(1)).squeeze(1)
  
      # probability of tokens copied from source
      copy_ix = align.unsqueeze(1) + self.vocab_size
      copy_tok_probs = scores.gather(1, copy_ix).squeeze(1)
      # Set scores for unk to 0 and add eps
      copy_tok_probs[align == self.unk_index] = 0
      copy_tok_probs += self.eps  # to avoid -inf logs
  
      # find the indices in which you do not use the copy mechanism
      non_copy = align == self.unk_index
      if not self.force_copy:
          non_copy = non_copy | (target != self.unk_index)
  
      probs = torch.where(
          non_copy, copy_tok_probs + vocab_probs, copy_tok_probs
      )
  
      loss = (
          -probs.log()
      )  # just NLLLoss; can the module be incorporated?
      # Drop padding.
      loss[target == self.ignore_index] = 0
      return loss
  
file_path: OpenNMT-py/onmt/modules/copy_generator.py
imports: import torch
  import torch.nn
label_desc: Use image node_id 2 for calling the CopyGeneratorLoss obj's underlying member method code with example usage: obj.forward(scores, align, target) and returns: loss