class_docstr: Uses the model's assessment of how likely the correct answer is
  to weight the loss for a each error
  
  multi-category focal loss, in other words
  
  from "Focal Loss for Dense Object Detection"
  
  https://arxiv.org/abs/1708.02002
class_docstr_tok: ["Uses","the","model","'s","assessment","of","how","likely","the","correct","answer","is","to","weight","the","loss","for","a","each","error","multi-category","focal","loss",",","in","other","words","from","``","Focal","Loss","for","Dense","Object","Detection","''","https",":","\/\/arxiv.org\/abs\/1708.02002"]
docstr: Weight the loss using the models assessment of the correct answer
  
  inputs: [N, C]
  targets: [N]
docstr_tok: ["Weight","the","loss","using","the","models","assessment","of","the","correct","answer","inputs",":","[","N",",","C","]","targets",":","[","N","]"]
code: def forward(self, inputs, targets):
      """
      Weight the loss using the models assessment of the correct answer
  
      inputs: [N, C]
      targets: [N]
      """
      if len(inputs.shape) == 2 and len(targets.shape) == 1:
          if inputs.shape[0] != targets.shape[0]:
              raise ValueError(
                  "Expected inputs N,C and targets N, but got {} and {}".format(
                      inputs.shape, targets.shape
                  )
              )
      elif len(inputs.shape) == 1 and len(targets.shape) == 0:
          raise NotImplementedError(
              "This would be a reasonable thing to implement, but we haven't done it yet"
          )
      else:
          raise ValueError(
              "Expected inputs N,C and targets N, but got {} and {}".format(
                  inputs.shape, targets.shape
              )
          )
  
      raw_loss = self.ce_loss(inputs, targets)
      assert (
          len(raw_loss.shape) == 1
          and raw_loss.shape[0] == inputs.shape[0]
      )
  
      # https://www.tutorialexample.com/implement-focal-loss-for-multi-label-classification-in-pytorch-pytorch-tutorial/
      final_loss = raw_loss * ((1 - torch.exp(-raw_loss)) ** self.gamma)
      assert (
          len(final_loss.shape) == 1
          and final_loss.shape[0] == inputs.shape[0]
      )
      if self.reduction == "sum":
          return final_loss.sum()
      elif self.reduction == "mean":
          return final_loss.mean()
      elif self.reduction == "none":
          return final_loss
      raise AssertionError("unknown reduction!  how did this happen??")
  
file_path: stanza/stanza/models/common/loss.py
imports: import logging
  import numpy
  import torch
  import torch.nn
  import stanza.models.common.seq2seq_constant
label_desc: Use image node_id 2 for calling the FocalLoss obj's underlying member method code with example usage: obj.forward(inputs, targets) and returns: final_loss, final_loss, final_loss