code: def _compute_calibration_bin_statistics(
      num_bins, logits=None, labels_true=None, labels_predicted=None
  ):
      """Compute binning statistics required for calibration measures.
  
      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`.
  
      Returns:
        bz: Tensor, shape (2,num_bins), tf.int32, counts of incorrect (row 0) and
          correct (row 1) predictions in each of the `num_bins` probability bins.
        pmean_observed: Tensor, shape (num_bins,), tf.float32, the mean predictive
          probabilities in each probability bin.
      """
  
      if labels_predicted is None:
          # If no labels are provided, we take the label with the maximum probability
          # decision.  This corresponds to the optimal expected minimum loss decision
          # under 0/1 loss.
          pred_y = tf.argmax(
              logits, axis=1, output_type=labels_true.dtype
          )
      else:
          pred_y = labels_predicted
  
      correct = tf.cast(tf.equal(pred_y, labels_true), tf.int32)
  
      # Collect predicted probabilities of decisions
      pred = tf.nn.softmax(logits, axis=1)
      prob_y = tf.gather(
          pred, pred_y[:, tf.newaxis], batch_dims=1
      )  # p(pred_y | x)
      prob_y = tf.reshape(prob_y, (ps.size(prob_y),))
  
      # Compute b/z histogram statistics:
      # bz[0,bin] contains counts of incorrect predictions in the probability bin.
      # bz[1,bin] contains counts of correct predictions in the probability bin.
      bins = tf.histogram_fixed_width_bins(
          prob_y, [0.0, 1.0], nbins=num_bins
      )
      event_bin_counts = tf.math.bincount(
          correct * num_bins + bins,
          minlength=2 * num_bins,
          maxlength=2 * num_bins,
      )
      event_bin_counts = tf.reshape(event_bin_counts, (2, num_bins))
  
      # Compute mean predicted probability value in each of the `num_bins` bins
      pmean_observed = tf.math.unsorted_segment_sum(
          prob_y, bins, num_bins
      )
      tiny = np.finfo(dtype_util.as_numpy_dtype(logits.dtype)).tiny
      pmean_observed = pmean_observed / (
          tf.cast(tf.reduce_sum(event_bin_counts, axis=0), logits.dtype)
          + tiny
      )
  
      return event_bin_counts, pmean_observed
  
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 3 for calling a global function with example usage: _compute_calibration_bin_statistics(num_bins, logits, labels_true, labels_predicted) and returns: event_bin_counts,  pmean_observed