class_docstr: Implements the Weighted Kappa loss function.
  
  Weighted Kappa loss was introduced in the
  [Weighted kappa loss function for multi-class classification
  of ordinal data in deep learning]
  (https://www.sciencedirect.com/science/article/abs/pii/S0167865517301666).
  Weighted Kappa is widely used in Ordinal Classification Problems.
  The loss value lies in $ [-\infty, \log 2] $, where $ \log 2 $
  means the random prediction.
  
  Usage:
  
  >>> kappa_loss = tfa.losses.WeightedKappaLoss(num_classes=4)
  >>> y_true = tf.constant([[0, 0, 1, 0], [0, 1, 0, 0],
  ...                  [1, 0, 0, 0], [0, 0, 0, 1]])
  >>> y_pred = tf.constant([[0.1, 0.2, 0.6, 0.1], [0.1, 0.5, 0.3, 0.1],
  ...                  [0.8, 0.05, 0.05, 0.1], [0.01, 0.09, 0.1, 0.8]])
  >>> loss = kappa_loss(y_true, y_pred)
  >>> loss
  <tf.Tensor: shape=(), dtype=float32, numpy=-1.1611925>
  
  Usage with `tf.keras` API:
  
  >>> model = tf.keras.Model()
  >>> model.compile('sgd', loss=tfa.losses.WeightedKappaLoss(num_classes=4))
  
  <... outputs should be softmax results
  if you want to weight the samples, just multiply the outputs
  by the sample weight ...>
docstr: Creates a `WeightedKappaLoss` instance.
  
  Args:
    num_classes: Number of unique classes in your dataset.
    weightage: (Optional) Weighting to be considered for calculating
      kappa statistics. A valid value is one of
      ['linear', 'quadratic']. Defaults to 'quadratic'.
    name: (Optional) String name of the metric instance.
    epsilon: (Optional) increment to avoid log zero,
      so the loss will be $ \log(1 - k + \epsilon) $, where $ k $ lies
      in $ [-1, 1] $. Defaults to 1e-6.
  Raises:
    ValueError: If the value passed for `weightage` is invalid
      i.e. not any one of ['linear', 'quadratic']
docstr_tok: ["Creates","a","`","WeightedKappaLoss","`","instance",".","Args",":","num_classes",":","Number","of","unique","classes","in","your","dataset",".","weightage",":","(","Optional",")","Weighting","to","be","considered","for","calculating","kappa","statistics",".","A","valid","value","is","one","of","[","'linear","'",",","'quadratic","'","]",".","Defaults","to","'quadratic","'",".","name",":","(","Optional",")","String","name","of","the","metric","instance",".","epsilon",":","(","Optional",")","increment","to","avoid","log","zero",",","so","the","loss","will","be","$","\\log","(","1","-","k","+","\\epsilon",")","$",",","where","$","k","$","lies","in","$","[","-1",",","1","]","$",".","Defaults","to","1e-6",".","Raises",":","ValueError",":","If","the","value","passed","for","`","weightage","`","is","invalid","i.e",".","not","any","one","of","[","'linear","'",",","'quadratic","'","]"]
code: def __init__(
      self,
      num_classes: int,
      weightage: Optional[str] = "quadratic",
      name: Optional[str] = "cohen_kappa_loss",
      epsilon: Optional[Number] = 1e-6,
      reduction: str = tf.keras.losses.Reduction.NONE,
  ):
      r"""Creates a `WeightedKappaLoss` instance.
  
      Args:
        num_classes: Number of unique classes in your dataset.
        weightage: (Optional) Weighting to be considered for calculating
          kappa statistics. A valid value is one of
          ['linear', 'quadratic']. Defaults to 'quadratic'.
        name: (Optional) String name of the metric instance.
        epsilon: (Optional) increment to avoid log zero,
          so the loss will be $ \log(1 - k + \epsilon) $, where $ k $ lies
          in $ [-1, 1] $. Defaults to 1e-6.
      Raises:
        ValueError: If the value passed for `weightage` is invalid
          i.e. not any one of ['linear', 'quadratic']
      """
  
      super().__init__(name=name, reduction=reduction)
  
      if weightage not in ("linear", "quadratic"):
          raise ValueError("Unknown kappa weighting type.")
  
      self.weightage = weightage
      self.num_classes = num_classes
      self.epsilon = epsilon or tf.keras.backend.epsilon()
      label_vec = tf.range(num_classes, dtype=tf.keras.backend.floatx())
      self.row_label_vec = tf.reshape(label_vec, [1, num_classes])
      self.col_label_vec = tf.reshape(label_vec, [num_classes, 1])
      col_mat = tf.tile(self.col_label_vec, [1, num_classes])
      row_mat = tf.tile(self.row_label_vec, [num_classes, 1])
      if weightage == "linear":
          self.weight_mat = tf.abs(col_mat - row_mat)
      else:
          self.weight_mat = (col_mat - row_mat) ** 2
  
file_path: addons/tensorflow_addons/losses/kappa_loss.py
imports: from typing import Optional
  import tensorflow
  from typeguard import typechecked
  from tensorflow_addons.utils.types import Number
label_desc: Use image node_id 1 to create a new WeightedKappaLoss object from inherited base classes: tf.keras.losses with example: obj = WeightedKappaLoss(num_classes, weightage, name, epsilon, reduction)