code: def expected_calibration_error(
      num_bins,
      logits=None,
      labels_true=None,
      labels_predicted=None,
      name=None,
  ):
      """Compute the Expected Calibration Error (ECE).
  
      This method implements equation (3) in [1].  In this equation the probability
      of the decided label being correct is used to estimate the calibration
      property of the predictor.
  
      Note: a trade-off exist between using a small number of `num_bins` and the
      estimation reliability of the ECE.  In particular, this method may produce
      unreliable ECE estimates in case there are few samples available in some bins.
      As an alternative to this method, consider also using
      `bayesian_expected_calibration_error`.
  
      #### References
      [1]: Chuan Guo, Geoff Pleiss, Yu Sun, Kilian Q. Weinberger,
           On Calibration of Modern Neural Networks.
           Proceedings of the 34th International Conference on Machine Learning
           (ICML 2017).
           arXiv:1706.04599
           https://arxiv.org/pdf/1706.04599.pdf
  
      Args:
        num_bins: int, number of probability bins, e.g. 10.
        logits: Tensor, (n,nlabels), with logits for n instances and nlabels.
        labels_true: Tensor, (n,), with tf.int32 or tf.int64 elements containing
          ground truth class labels in the range [0,nlabels].
        labels_predicted: Tensor, (n,), with tf.int32 or tf.int64 elements
          containing decisions of the predictive system.  If `None`, we will use
          the argmax decision using the `logits`.
        name: Python `str` name prefixed to Ops created by this function.
  
      Returns:
        ece: Tensor, scalar, tf.float32.
      """
      with tf.name_scope(name or "expected_calibration_error"):
          logits = tf.convert_to_tensor(logits)
          labels_true = tf.convert_to_tensor(labels_true)
          if labels_predicted is not None:
              labels_predicted = tf.convert_to_tensor(labels_predicted)
  
          # Compute empirical counts over the events defined by the sets
          # {incorrect,correct}x{0,1,..,num_bins-1}, as well as the empirical averages
          # of predicted probabilities in each probability bin.
          (
              event_bin_counts,
              pmean_observed,
          ) = _compute_calibration_bin_statistics(
              num_bins,
              logits=logits,
              labels_true=labels_true,
              labels_predicted=labels_predicted,
          )
  
          # Compute the marginal probability of observing a probability bin.
          event_bin_counts = tf.cast(event_bin_counts, tf.float32)
          bin_n = tf.reduce_sum(event_bin_counts, axis=0)
          pbins = bin_n / tf.reduce_sum(
              bin_n
          )  # Compute the marginal bin probability
  
          # Compute the marginal probability of making a correct decision given an
          # observed probability bin.
          tiny = np.finfo(np.float32).tiny
          pcorrect = event_bin_counts[1, :] / (bin_n + tiny)
  
          # Compute the ECE statistic as defined in reference [1].
          ece = tf.reduce_sum(pbins * tf.abs(pcorrect - pmean_observed))
  
      return ece
  
file_path: probability/tensorflow_probability/python/stats/calibration.py
imports: import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.internal import dtype_util
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import tensorshape_util
  from tensorflow_probability.python.math import generic
  from tensorflow_probability.python.stats import quantiles
label_desc: Use image node_id 4 for calling a global function with example usage: expected_calibration_error(num_bins, logits, labels_true, labels_predicted, name) and returns: ece