class_docstr: Implementation of focal loss (https://arxiv.org/pdf/1708.02002.pdf).
  
  This class computes the focal loss between labels and prediction. Focal loss
  is a weighted loss function that modulates the standard cross-entropy loss
  based on how well the neural network performs on a specific example of a
  class. The labels should be provided in a `one_hot` vector representation.
  There should be `#classes` floating point values per prediction.
  The loss is reduced across all samples using 'sum_over_batch_size' reduction
  (see https://www.tensorflow.org/api_docs/python/tf/keras/losses/Reduction).
  
  Example usage:
  >>> y_true = [[0, 1, 0], [0, 0, 1]]
  >>> y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
  >>> gamma = 2
  >>> focal_loss = FocalLoss(gamma)
  >>> focal_loss(y_true, y_pred).numpy()
  0.9326
  
  >>> # Calling with 'sample_weight'.
  >>> focal_loss(y_true, y_pred, sample_weight=tf.constant([0.3, 0.7])).numpy()
  0.6528
  
  Usage with the `compile()` API:
  ```python
  model.compile(optimizer='sgd', loss=FocalLoss(gamma))
  ```
class_docstr_tok: ["Implementation","of","focal","loss","(","https",":","\/\/arxiv.org\/pdf\/1708.02002.pdf",")",".","This","class","computes","the","focal","loss","between","labels","and","prediction",".","Focal","loss","is","a","weighted","loss","function","that","modulates","the","standard","cross-entropy","loss","based","on","how","well","the","neural","network","performs","on","a","specific","example","of","a","class",".","The","labels","should","be","provided","in","a","`","one_hot","`","vector","representation",".","There","should","be","`","#","classes","`","floating","point","values","per","prediction",".","The","loss","is","reduced","across","all","samples","using","'sum_over_batch_size","'","reduction","(","see","https",":","\/\/www.tensorflow.org\/api_docs\/python\/tf\/keras\/losses\/Reduction",")",".","Example","usage",":",">",">",">","y_true","=","[","[","0",",","1",",","0","]",",","[","0",",","0",",","1","]","]",">",">",">","y_pred","=","[","[","0.05",",","0.95",",","0","]",",","[","0.1",",","0.8",",","0.1","]","]",">",">",">","gamma","=","2",">",">",">","focal_loss","=","FocalLoss","(","gamma",")",">",">",">","focal_loss","(","y_true",",","y_pred",")",".numpy","(",")","0.9326",">",">",">","#","Calling","with","'sample_weight","'",".",">",">",">","focal_loss","(","y_true",",","y_pred",",","sample_weight=tf.constant","(","[","0.3",",","0.7","]",")",")",".numpy","(",")","0.6528","Usage","with","the","`","compile","(",")","`","API",":","``","`","python","model.compile","(","optimizer='sgd","'",",","loss=FocalLoss","(","gamma",")",")","``","`"]
docstr: Initializes FocalLoss.
  
  Args:
    gamma: Focal loss gamma, as described in class docs.
    class_weight: A weight to apply to the loss, one for each class. The
      weight is applied for each input where the ground truth label matches.
docstr_tok: ["Initializes","FocalLoss",".","Args",":","gamma",":","Focal","loss","gamma",",","as","described","in","class","docs",".","class_weight",":","A","weight","to","apply","to","the","loss",",","one","for","each","class",".","The","weight","is","applied","for","each","input","where","the","ground","truth","label","matches","."]
code: def __init__(
      self, gamma, class_weight: Optional[Sequence[float]] = None
  ):
      """Initializes FocalLoss.
  
      Args:
        gamma: Focal loss gamma, as described in class docs.
        class_weight: A weight to apply to the loss, one for each class. The
          weight is applied for each input where the ground truth label matches.
      """
      super().__init__()
      # Used for clipping min/max values of probability values in y_pred to avoid
      # NaNs and Infs in computation.
      self._epsilon = 1e-7
      # This is a tunable "focusing parameter"; should be >= 0.
      # When gamma = 0, the loss returned is the standard categorical
      # cross-entropy loss.
      self._gamma = gamma
      self._class_weight = class_weight
      # tf.keras.losses.Loss class implementation requires a Reduction specified
      # in self.reduction. To use this reduction, we should use tensorflow's
      # compute_weighted_loss function however it is only compatible with v1 of
      # Tensorflow: https://www.tensorflow.org/api_docs/python/tf/compat/v1/losses/compute_weighted_loss?hl=en.  pylint: disable=line-too-long
      # So even though it is specified here, we don't use self.reduction in the
      # loss function call.
      self.reduction = tf.keras.losses.Reduction.SUM_OVER_BATCH_SIZE
  
imports: import abc
  from typing import Mapping, Sequence
  import dataclasses
  from typing import Any, Optional
  import numpy
  import tensorflow
  from mediapipe.model_maker.python.core.utils import file_util
  from mediapipe.model_maker.python.core.utils import model_util
  from official.modeling import tf_utils
label_desc: Use image node_id 1 to create a new FocalLoss object from inherited base classes: tf.keras.losses with example: obj = FocalLoss(gamma, class_weight)