class_docstr: A class for temperature calibration for binary and multi-class classification problems.
  
  For binary classification, Platt Scaling is used for calibration. Platt Scaling was
  proposed in the paper Probabilistic outputs for support vector machines and comparisons to regularized
  likelihood methods, J. C. Platt, Advances in large margin classifiers, 10(3): 61-74, 1999.
  
  For multi-class classification, Temperature Calibration is used. It is an extension of Platt Scaling
  and it was proposed in the paper On Calibration of Modern Neural Networks, C. Guo et. al., ICML, 2017.
  
  In Temperature Calibration, a classifier's non-probabilistic outputs, i.e., logits, are
  scaled by a trainable parameter called Temperature. The softmax is applied to the rescaled
  logits to calculate the probabilistic output. As noted in the cited paper, Temperature
  Scaling does not change the maximum of the softmax function so the classifier's prediction
  remain the same.
  
  .. seealso::
  
     `Examples using calibration <https://stellargraph.readthedocs.io/en/stable/demos/calibration/index.html>`__.
  
     Related functionality: :func:`.expected_calibration_error`, :func:`.plot_reliability_diagram`, :class:`.IsotonicCalibration`.
class_docstr_tok: ["A","class","for","temperature","calibration","for","binary","and","multi-class","classification","problems",".","For","binary","classification",",","Platt","Scaling","is","used","for","calibration",".","Platt","Scaling","was","proposed","in","the","paper","Probabilistic","outputs","for","support","vector","machines","and","comparisons","to","regularized","likelihood","methods",",","J.","C.","Platt",",","Advances","in","large","margin","classifiers",",","10","(","3",")",":","61-74",",","1999",".","For","multi-class","classification",",","Temperature","Calibration","is","used",".","It","is","an","extension","of","Platt","Scaling","and","it","was","proposed","in","the","paper","On","Calibration","of","Modern","Neural","Networks",",","C.","Guo","et",".","al.",",","ICML",",","2017",".","In","Temperature","Calibration",",","a","classifier","'s","non-probabilistic","outputs",",","i.e.",",","logits",",","are","scaled","by","a","trainable","parameter","called","Temperature",".","The","softmax","is","applied","to","the","rescaled","logits","to","calculate","the","probabilistic","output",".","As","noted","in","the","cited","paper",",","Temperature","Scaling","does","not","change","the","maximum","of","the","softmax","function","so","the","classifier","'s","prediction","remain","the","same",".","..","seealso",":",":","`","Examples","using","calibration","<","https",":","\/\/stellargraph.readthedocs.io\/en\/stable\/demos\/calibration\/index.html",">","`","__",".","Related","functionality",":",":","func",":","`",".expected_calibration_error","`",",",":","func",":","`",".plot_reliability_diagram","`",",",":","class",":","`",".IsotonicCalibration","`","."]
docstr: Train the calibration model using Temperature Scaling.
  
  If validation data is given, then training stops when the validation accuracy starts increasing.
  
  Args:
      x_train (numpy array): The training data that should be a classifier's non-probabilistic outputs. It should
          have shape (N, C) where N is the number of samples and C is the number of classes.
      y_train (numpy array): The training data class labels. It should have shape (N, C) where N is the number
          of samples and C is the number of classes and the class labels are one-hot encoded.
      x_val (numpy array or None): The validation data used for early stopping. It should have shape (M, C) where
          M is the number of validation samples and C is the number of classes and the class labels are one-hot
          encoded.
      y_val (numpy array or None): The validation data class labels. It should have shape (M, C) where M is the
          number of validation samples and C is the number of classes and the class labels are one-hot encoded.
docstr_tok: ["Train","the","calibration","model","using","Temperature","Scaling",".","If","validation","data","is","given",",","then","training","stops","when","the","validation","accuracy","starts","increasing",".","Args",":","x_train","(","numpy","array",")",":","The","training","data","that","should","be","a","classifier","'s","non-probabilistic","outputs",".","It","should","have","shape","(","N",",","C",")","where","N","is","the","number","of","samples","and","C","is","the","number","of","classes",".","y_train","(","numpy","array",")",":","The","training","data","class","labels",".","It","should","have","shape","(","N",",","C",")","where","N","is","the","number","of","samples","and","C","is","the","number","of","classes","and","the","class","labels","are","one-hot","encoded",".","x_val","(","numpy","array","or","None",")",":","The","validation","data","used","for","early","stopping",".","It","should","have","shape","(","M",",","C",")","where","M","is","the","number","of","validation","samples","and","C","is","the","number","of","classes","and","the","class","labels","are","one-hot","encoded",".","y_val","(","numpy","array","or","None",")",":","The","validation","data","class","labels",".","It","should","have","shape","(","M",",","C",")","where","M","is","the","number","of","validation","samples","and","C","is","the","number","of","classes","and","the","class","labels","are","one-hot","encoded","."]
code: def _fit_temperature_scaling(
      self, x_train, y_train, x_val=None, y_val=None
  ):
      """
      Train the calibration model using Temperature Scaling.
  
      If validation data is given, then training stops when the validation accuracy starts increasing.
  
      Args:
          x_train (numpy array): The training data that should be a classifier's non-probabilistic outputs. It should
              have shape (N, C) where N is the number of samples and C is the number of classes.
          y_train (numpy array): The training data class labels. It should have shape (N, C) where N is the number
              of samples and C is the number of classes and the class labels are one-hot encoded.
          x_val (numpy array or None): The validation data used for early stopping. It should have shape (M, C) where
              M is the number of validation samples and C is the number of classes and the class labels are one-hot
              encoded.
          y_val (numpy array or None): The validation data class labels. It should have shape (M, C) where M is the
              number of validation samples and C is the number of classes and the class labels are one-hot encoded.
      """
  
      T = tf.Variable(tf.ones(shape=(1,)), name="T")
  
      def cost(T, x, y):
          scaled_logits = tf.multiply(name="z", x=x, y=1.0 / T)
  
          cost_value = tf.reduce_mean(
              tf.nn.softmax_cross_entropy_with_logits(
                  logits=scaled_logits, labels=y
              )
          )
  
          return cost_value
  
      def grad(T, x, y):
          with tf.GradientTape() as tape:
              cost_value = cost(T, x, y)
  
          return cost_value, tape.gradient(cost_value, T)
  
      optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
  
      for epoch in range(self.epochs):
          train_cost, grads = grad(T, x_train, y_train)
          optimizer.apply_gradients(zip([grads], [T]))
          if self.early_stopping:
              val_cost = cost(T, x_val, y_val)
              if (len(self.history) > 0) and (
                  val_cost > self.history[-1][1]
              ):
                  break
              else:  # keep going
                  self.history.append(
                      [train_cost, val_cost, T.numpy()[0]]
                  )
          else:
              self.history.append([train_cost, T.numpy()[0]])
  
      self.history = np.array(self.history)
      self.temperature = self.history[-1, -1]
  
imports: import numpy
  import matplotlib.pyplot
  import tensorflow
  from sklearn.isotonic import IsotonicRegression
  from sklearn.linear_model import LogisticRegression
label_desc: Use image node_id 2 for calling the TemperatureCalibration obj's underlying member method code with example usage: obj._fit_temperature_scaling(x_train, y_train, x_val, y_val) and returns: cost_value, cost_value,  tape