class_docstr: Calculates the Expected Calibration Error of a model.
  
  The input to this loss is the logits of a model, NOT the softmax scores.
  This divides the confidence outputs into equally-sized interval bins.
  In each bin, we compute the confidence gap:
  
      bin_gap = | avg_confidence_in_bin - accuracy_in_bin |
  
  We then return an average of the gaps, weighted by the number of samples in each bin.
  
  References:
      Naeini, Mahdi Pakdaman, Gregory F. Cooper, and Milos Hauskrecht
      "Obtaining Well Calibrated Probabilities Using Bayesian Binning." AAAI. 2015.
  
      Chuan Guo, Geoff Pleiss, Yu Sun, Kilian Q. Weinberger
      "On Calibration of Modern Neural Networks." PMLR 2017.
class_docstr_tok: ["Calculates","the","Expected","Calibration","Error","of","a","model",".","The","input","to","this","loss","is","the","logits","of","a","model",",","NOT","the","softmax","scores",".","This","divides","the","confidence","outputs","into","equally-sized","interval","bins",".","In","each","bin",",","we","compute","the","confidence","gap",":","bin_gap","=","|","avg_confidence_in_bin","-","accuracy_in_bin","|","We","then","return","an","average","of","the","gaps",",","weighted","by","the","number","of","samples","in","each","bin",".","References",":","Naeini",",","Mahdi","Pakdaman",",","Gregory","F.","Cooper",",","and","Milos","Hauskrecht","``","Obtaining","Well","Calibrated","Probabilities","Using","Bayesian","Binning",".","''","AAAI",".","2015",".","Chuan","Guo",",","Geoff","Pleiss",",","Yu","Sun",",","Kilian","Q.","Weinberger","``","On","Calibration","of","Modern","Neural","Networks",".","''","PMLR","2017","."]
code: def forward(
      self, logits: torch.Tensor, one_hot_labels: torch.Tensor
  ) -> torch.Tensor:
      softmaxes = nn.functional.softmax(logits, dim=1)
      confidences, predictions = torch.max(softmaxes, 1)
      labels = torch.argmax(one_hot_labels, 1)
      accuracies = predictions.eq(labels)
      ece = torch.zeros(1, device=logits.device)
      for bin_lower, bin_upper in zip(self.bin_lowers, self.bin_uppers):
          # Calculates |confidence - accuracy| in each bin
          in_bin = confidences.gt(bin_lower.item()) * confidences.le(
              bin_upper.item()
          )
          prop_in_bin = in_bin.float().mean()
          if prop_in_bin.item() > 0:
              accuracy_in_bin = accuracies[in_bin].float().mean()
              avg_confidence_in_bin = confidences[in_bin].mean()
              ece += (
                  torch.abs(avg_confidence_in_bin - accuracy_in_bin)
                  * prop_in_bin
              )
      return ece
  
imports: import logging
  from abc import ABC, abstractmethod
  from dataclasses import dataclass
  from typing import List, Type, Union
  import numpy
  import torch
  import torch.nn
  from ludwig.api_annotations import DeveloperAPI
  from ludwig.constants import BINARY, CATEGORY
  from ludwig.utils.registry import DEFAULT_KEYS, Registry
label_desc: Use image node_id 2 for calling the ECELoss obj's underlying member method code with example usage: obj.forward(logits, one_hot_labels) and returns: ece