[docs]classCohenKappa(EpochMetric):"""Compute different types of Cohen's Kappa: Non-Wieghted, Linear, Quadratic. Accumulating predictions and the ground-truth during an epoch and applying `sklearn.metrics.cohen_kappa_score <https://scikit-learn.org/stable/modules/ generated/sklearn.metrics.cohen_kappa_score.html>`_ . Args: output_transform: a callable that is used to transform the :class:`~ignite.engine.engine.Engine`'s ``process_function``'s output into the form expected by the metric. This can be useful if, for example, you have a multi-output model and you want to compute the metric with respect to one of the outputs. weights: a string is used to define the type of Cohen's Kappa whether Non-Weighted or Linear or Quadratic. Default, None. check_compute_fn: Default False. If True, `cohen_kappa_score <https://scikit-learn.org/stable/modules/generated/sklearn.metrics.cohen_kappa_score.html>`_ is run on the first batch of data to ensure there are no issues. User will be warned in case there are any issues computing the function. device: optional device specification for internal storage. .. code-block:: python def activated_output_transform(output): y_pred, y = output return y_pred, y weights = None or linear or quadratic cohen_kappa = CohenKappa(activated_output_transform, weights) """def__init__(self,output_transform:Callable=lambdax:x,weights:Optional[str]=None,check_compute_fn:bool=False,device:Union[str,torch.device]=torch.device("cpu"),):try:fromsklearn.metricsimportcohen_kappa_score# noqa: F401exceptImportError:raiseRuntimeError("This contrib module requires sklearn to be installed.")ifweightsnotin(None,"linear","quadratic"):raiseValueError("Kappa Weighting type must be None or linear or quadratic.")# initalize weightsself.weights=weightsself.cohen_kappa_compute=self.get_cohen_kappa_fn()super(CohenKappa,self).__init__(self.cohen_kappa_compute,output_transform=output_transform,check_compute_fn=check_compute_fn,device=device,)
[docs]defget_cohen_kappa_fn(self)->Callable[[torch.Tensor,torch.Tensor],float]:"""Return a function computing Cohen Kappa from scikit-learn."""fromsklearn.metricsimportcohen_kappa_scoredefwrapper(y_targets:torch.Tensor,y_preds:torch.Tensor)->float:y_true=y_targets.cpu().numpy()y_pred=y_preds.cpu().numpy()returncohen_kappa_score(y_true,y_pred,weights=self.weights)returnwrapper