class_docstr: A combination loss of Soft Nearest Neighbor Loss calculated at every layer
  in the network, and standard cross entropy of the logits. Presented in
  "Analyzing and Improving Representations with the Soft Nearest Neighbor Loss"
  by Nicholas Frosst, Nicolas Papernot, and Geoffrey Hinton.
  arXiv preprint arXiv:1902.01889 (2019).
class_docstr_tok: ["A","combination","loss","of","Soft","Nearest","Neighbor","Loss","calculated","at","every","layer","in","the","network",",","and","standard","cross","entropy","of","the","logits",".","Presented","in","''","Analyzing","and","Improving","Representations","with","the","Soft","Nearest","Neighbor","Loss","''","by","Nicholas","Frosst",",","Nicolas","Papernot",",","and","Geoffrey","Hinton",".","arXiv","preprint","arXiv:1902.01889","(","2019",")","."]
docstr: The optimized variant of Soft Nearest Neighbor Loss. Every time this
  tensor is evaluated, the temperature is optimized to minimize the loss
  value, this results in more numerically stable calculations of the SNNL.
  :param x: a matrix.
  :param y: a list of labels for each element of x.
  :param initial_temp: Temperature.
  :cos_distance: Boolean for using cosine or Euclidean distance.
  
  :returns: A tensor for the Soft Nearest Neighbor Loss of the points
            in x with labels y, optimized for temperature.
docstr_tok: ["The","optimized","variant","of","Soft","Nearest","Neighbor","Loss",".","Every","time","this","tensor","is","evaluated",",","the","temperature","is","optimized","to","minimize","the","loss","value",",","this","results","in","more","numerically","stable","calculations","of","the","SNNL",".",":","param","x",":","a","matrix",".",":","param","y",":","a","list","of","labels","for","each","element","of","x.",":","param","initial_temp",":","Temperature",".",":","cos_distance",":","Boolean","for","using","cosine","or","Euclidean","distance",".",":","returns",":","A","tensor","for","the","Soft","Nearest","Neighbor","Loss","of","the","points","in","x","with","labels","y",",","optimized","for","temperature","."]
code: def optimized_temp_SNNL(x, y, initial_temp, cos_distance):
      """The optimized variant of Soft Nearest Neighbor Loss. Every time this
      tensor is evaluated, the temperature is optimized to minimize the loss
      value, this results in more numerically stable calculations of the SNNL.
      :param x: a matrix.
      :param y: a list of labels for each element of x.
      :param initial_temp: Temperature.
      :cos_distance: Boolean for using cosine or Euclidean distance.
  
      :returns: A tensor for the Soft Nearest Neighbor Loss of the points
                in x with labels y, optimized for temperature.
      """
      t = tf.Variable(1, dtype=tf.float32, trainable=False, name="temp")
  
      def inverse_temp(t):
          # pylint: disable=missing-docstring
          # we use inverse_temp because it was observed to be more stable when optimizing.
          return tf.div(initial_temp, t)
  
      ent_loss = SNNLCrossEntropy.SNNL(
          x, y, inverse_temp(t), cos_distance
      )
      updated_t = tf.assign(
          t, tf.subtract(t, 0.1 * tf.gradients(ent_loss, t)[0])
      )
      inverse_t = inverse_temp(updated_t)
      return SNNLCrossEntropy.SNNL(x, y, inverse_t, cos_distance)
  
imports: import copy
  import json
  import os
  import warnings
  import numpy
  import tensorflow
  from cleverhans.attacks import Attack
  from cleverhans.compat import softmax_cross_entropy_with_logits
  from cleverhans.model import Model
  from cleverhans.utils import safe_zip
label_desc: Use image node_id 9 for calling the SNNLCrossEntropy obj's underlying member method code with example usage: obj.optimized_temp_SNNL(x, y, initial_temp, cos_distance) and returns: SNNLCrossEntropy, tf