jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def tsne(data, no_dims=2, perplexity=30.0, max_iter=800):
    '''

    :param data: (n_samples, n_features)
    :param no_dims: target dimension
    :param perplexity:
    :param max_iter:
    :return: (n_samples, no_dims)
    '''

    # init
    tf.reset_default_graph()

    (n, d) = data.shape
    print(n, d)

    # 对称化
    P = seach_prob(data, 1e-5, perplexity)
    P = P + np.transpose(P)
    P = P / np.sum(P)  # pij
    P = np.maximum(P, 1e-12)
    # 随机初始化Y y = np.random.randn(n, no_dims)
    # tf.random_normal_initializer(mean=0.0, stddev=1.0, seed=None, dtype=tf.float32)

    X = tf.placeholder(name="X", dtype=tf.float32, shape=[n, n])

    Y = tf.get_variable(name="Y", shape=[n, no_dims],
                        initializer=tf.random_normal_initializer())

    sum_y = tf.reduce_sum(tf.square(Y), 1)
    temp = tf.add(tf.transpose(tf.add(-2 * tf.matmul(Y, tf.transpose(Y)), sum_y)), sum_y)
    num = tf.div(1, 1 + temp)
    # 不知道这句能不能执行
    one_ = tf.constant([x for x in range(n)])
    one_hot = tf.one_hot(one_, n)
    num = num - num * one_hot

    Q = num / tf.reduce_sum(num)
    Q = tf.maximum(Q, 1e-12)

    learning_rate = 500
    loss = tf.reduce_sum(X * tf.log(tf.div(X, Q)))

    optimizer = tf.train.AdagradOptimizer(learning_rate=learning_rate)
    train_op = optimizer.minimize(loss)

    init = tf.global_variables_initializer()
    print("begin")
    with tf.Session() as sess:
        init.run()
        for iter in range(max_iter):
            sess.run(train_op, feed_dict={X: P})
            if iter % 50 == 0:
                l = sess.run(loss, feed_dict={X: P})
                print("%d\t%f" % (iter, l))
        y = sess.run(Y)
    print("finished")

    return y
//  [python]
def tsne(data, no_dims=2, perplexity=30.0, max_iter=800):
    '''

    :param data: (n_samples, n_features)
    :param no_dims: target dimension
    :param perplexity:
    :param max_iter:
    :return: (n_samples, no_dims)
    '''

    # init
    tf.reset_default_graph()

    (n, d) = data.shape
    print(n, d)

    # 对称化
    P = seach_prob(data, 1e-5, perplexity)
    P = P + np.transpose(P)
    P = P / np.sum(P)  # pij
    P = np.maximum(P, 1e-12)
    # 随机初始化Y y = np.random.randn(n, no_dims)
    # tf.random_normal_initializer(mean=0.0, stddev=1.0, seed=None, dtype=tf.float32)

    X = tf.placeholder(name="X", dtype=tf.float32, shape=[n, n])

    Y = tf.get_variable(name="Y", shape=[n, no_dims],
                        initializer=tf.random_normal_initializer())

    sum_y = tf.reduce_sum(tf.square(Y), 1)
    temp = tf.add(tf.transpose(tf.add(-2 * tf.matmul(Y, tf.transpose(Y)), sum_y)), sum_y)
    num = tf.divide(1, 1 + temp)
    # 不知道这句能不能执行
    one_ = tf.constant([x for x in range(n)])
    one_hot = tf.one_hot(one_, n)
    num = num - num * one_hot

    Q = num / tf.reduce_sum(num)
    Q = tf.maximum(Q, 1e-12)

    learning_rate = 500
    loss = tf.reduce_sum(X * tf.log(tf.divide(X, Q)))

    optimizer = tf.train.AdagradOptimizer(learning_rate=learning_rate)
    train_op = optimizer.minimize(loss)

    init = tf.global_variables_initializer()
    print("begin")
    with tf.Session() as sess:
        init.run()
        for iter in range(max_iter):
            sess.run(train_op, feed_dict={X: P})
            if iter % 50 == 0:
                l = sess.run(loss, feed_dict={X: P})
                print("%d\t%f" % (iter, l))
        y = sess.run(Y)
    print("finished")

    return y
//  [python]
def rectify_scores(self, x, miu=0.70, sigma=0.15):
        alpha = tf.exp(-(1 - miu) / sigma) + 1
        x = tf.div(alpha, tf.exp(-(x - miu) / sigma) + 1)
        return x
//  [python]
def rectify_scores(self, x, miu=0.70, sigma=0.15):
        alpha = tf.exp(-(1 - miu) / sigma) + 1
        x = tf.divide(alpha, tf.exp(-(x - miu) / sigma) + 1)
        return x
//  [python]
def _load_inception(self, proto_file):
    graph_def = tf.GraphDef.FromString(open(proto_file, 'rb').read())
    self._inception_graph = tf.Graph()
    with self._inception_graph.as_default():
      _ = tf.import_graph_def(graph_def, name='')
      self.session = tf.Session()
      Frame_Features = self.session.graph.get_tensor_by_name(
          'pool_3/_reshape:0')
      Pca_Mean = tf.constant(value=self.pca_mean, dtype=tf.float32)
      Pca_Eigenvecs = tf.constant(value=self.pca_eigenvecs, dtype=tf.float32)
      Pca_Eigenvals = tf.constant(value=self.pca_eigenvals, dtype=tf.float32)
      Feats = Frame_Features[0] - Pca_Mean
      Feats = tf.reshape(
          tf.matmul(tf.reshape(Feats, [1, 2048]), Pca_Eigenvecs), [
              1024,
          ])
      tf.div(Feats, tf.sqrt(Pca_Eigenvals + 1e-4), name='pca_final_feature')
//  [python]
def _load_inception(self, proto_file):
    graph_def = tf.GraphDef.FromString(open(proto_file, 'rb').read())
    self._inception_graph = tf.Graph()
    with self._inception_graph.as_default():
      _ = tf.import_graph_def(graph_def, name='')
      self.session = tf.Session()
      Frame_Features = self.session.graph.get_tensor_by_name(
          'pool_3/_reshape:0')
      Pca_Mean = tf.constant(value=self.pca_mean, dtype=tf.float32)
      Pca_Eigenvecs = tf.constant(value=self.pca_eigenvecs, dtype=tf.float32)
      Pca_Eigenvals = tf.constant(value=self.pca_eigenvals, dtype=tf.float32)
      Feats = Frame_Features[0] - Pca_Mean
      Feats = tf.reshape(
          tf.matmul(tf.reshape(Feats, [1, 2048]), Pca_Eigenvecs), [
              1024,
          ])
      tf.divide(Feats, tf.sqrt(Pca_Eigenvals + 1e-4), name='pca_final_feature')
//  [python]
def shape2image(self, channels, height, width, resize=None):
        """ This function shapes the input instance to image tensor

        :param channels:
        :param height:
        :param width:
        :param resize: list of tuple
        :type resize: list, tuple
        :return:
        """

        def image_preprocessor(image):
            # scale image to [-1,1]
            image = tf.subtract(tf.truediv(image, 127.5), 1)
            # reshape - note this is determined by how the data is stored in tfrecords, modify with caution
            image = tf.reshape(image, (channels, height, width)) \
                if FLAGS.IMAGE_FORMAT == 'channels_first' else tf.reshape(image, (height, width, channels))
            # resize
            if isinstance(resize, (list, tuple)):
                if FLAGS.IMAGE_FORMAT == 'channels_first':
                    image = tf.transpose(
                        tf.image.resize_images(  # resize only support HWC
                            tf.transpose(image, perm=(1, 2, 0)), resize, align_corners=True), perm=(2, 0, 1))
                else:
                    image = tf.image.resize_images(image, resize, align_corners=True)

            return image

        # do image pre-processing
        if self.num_labels == 0:
            self.dataset = self.dataset.map(
                lambda image_data: image_preprocessor(image_data),
                num_parallel_calls=self.num_threads)
        else:
            self.dataset = self.dataset.map(
                lambda image_data, label: (image_preprocessor(image_data), label),
                num_parallel_calls=self.num_threads)

        # write batch shape
        if isinstance(resize, (list, tuple)):
            height, width = resize
        self.batch_shape = [self.batch_size, height, width, channels] \
            if FLAGS.IMAGE_FORMAT == 'channels_last' else [self.batch_size, channels, height, width]
//  [python]
def shape2image(self, channels, height, width, resize=None):
        """ This function shapes the input instance to image tensor

        :param channels:
        :param height:
        :param width:
        :param resize: list of tuple
        :type resize: list, tuple
        :return:
        """

        def image_preprocessor(image):
            # scale image to [-1,1]
            image = tf.subtract(tf.divide(image, 127.5), 1)
            # reshape - note this is determined by how the data is stored in tfrecords, modify with caution
            image = tf.reshape(image, (channels, height, width)) \
                if FLAGS.IMAGE_FORMAT == 'channels_first' else tf.reshape(image, (height, width, channels))
            # resize
            if isinstance(resize, (list, tuple)):
                if FLAGS.IMAGE_FORMAT == 'channels_first':
                    image = tf.transpose(
                        tf.image.resize_images(  # resize only support HWC
                            tf.transpose(image, perm=(1, 2, 0)), resize, align_corners=True), perm=(2, 0, 1))
                else:
                    image = tf.image.resize_images(image, resize, align_corners=True)

            return image

        # do image pre-processing
        if self.num_labels == 0:
            self.dataset = self.dataset.map(
                lambda image_data: image_preprocessor(image_data),
                num_parallel_calls=self.num_threads)
        else:
            self.dataset = self.dataset.map(
                lambda image_data, label: (image_preprocessor(image_data), label),
                num_parallel_calls=self.num_threads)

        # write batch shape
        if isinstance(resize, (list, tuple)):
            height, width = resize
        self.batch_shape = [self.batch_size, height, width, channels] \
            if FLAGS.IMAGE_FORMAT == 'channels_last' else [self.batch_size, channels, height, width]
//  [python]
def _compute_loss(self, prediction_tensor, target_tensor, weights):
    """Compute loss function.

    Args:
      prediction_tensor: A float tensor of shape [batch_size, num_anchors,
        num_classes] representing the predicted logits for each class
      target_tensor: A float tensor of shape [batch_size, num_anchors,
        num_classes] representing logit classification targets
      weights: a float tensor of shape [batch_size, num_anchors]

    Returns:
      loss: a float tensor of shape [batch_size, num_anchors]
        representing the value of the loss function.
    """
    num_classes = prediction_tensor.get_shape().as_list()[-1]
    target_tensor = self._scale_and_softmax_logits(target_tensor)
    prediction_tensor = tf.div(prediction_tensor, self._logit_scale,
                                  name='scale_logits')

    per_row_cross_ent = (tf.nn.softmax_cross_entropy_with_logits(
        labels=tf.reshape(target_tensor, [-1, num_classes]),
        logits=tf.reshape(prediction_tensor, [-1, num_classes])))
    return tf.reshape(per_row_cross_ent, tf.shape(weights)) * weights
//  [python]
def _compute_loss(self, prediction_tensor, target_tensor, weights):
    """Compute loss function.

    Args:
      prediction_tensor: A float tensor of shape [batch_size, num_anchors,
        num_classes] representing the predicted logits for each class
      target_tensor: A float tensor of shape [batch_size, num_anchors,
        num_classes] representing logit classification targets
      weights: a float tensor of shape [batch_size, num_anchors]

    Returns:
      loss: a float tensor of shape [batch_size, num_anchors]
        representing the value of the loss function.
    """
    num_classes = prediction_tensor.get_shape().as_list()[-1]
    target_tensor = self._scale_and_softmax_logits(target_tensor)
    prediction_tensor = tf.divide(prediction_tensor, self._logit_scale,
                                  name='scale_logits')

    per_row_cross_ent = (tf.nn.softmax_cross_entropy_with_logits(
        labels=tf.reshape(target_tensor, [-1, num_classes]),
        logits=tf.reshape(prediction_tensor, [-1, num_classes])))
    return tf.reshape(per_row_cross_ent, tf.shape(weights)) * weights
//  [python]
def _make_activity_op(self, input_tensor):
        """ Creates the op for calculating the activity of a SOM
        :param input_tensor: A tensor to calculate the activity of. Must be of shape `[batch_size, dim]` where `dim` is
        the dimensionality of the SOM's weights.
        :return A handle to the newly created activity op:
        """
        with self._graph.as_default():
            with tf.name_scope("Activity"):
                # This constant controls the width of the gaussian.
                # The closer to 0 it is, the wider it is.
                c = tf.constant(self._c, dtype="float32")
                # Get the euclidean distance between each neuron and the input vectors
                dist = tf.norm(tf.subtract(
                    tf.expand_dims(self._weights, axis=0),
                    tf.expand_dims(input_tensor, axis=1)),
                    name="Distance", axis=2)  # [batch_size, neurons]

                # Calculate the Gaussian of the activity. Units with distances closer to 0 will have activities
                # closer to 1.
                activity = tf.exp(tf.multiply(
                    tf.pow(dist, 2), c), name="Gaussian")

                # Convert the activity into a softmax probability distribution
                if self._softmax_activity:
                    activity = tf.div(tf.exp(activity),
                                         tf.expand_dims(tf.reduce_sum(
                                             tf.exp(activity), axis=1), axis=-1),
                                         name="Softmax")

                return tf.identity(activity, name="Output")
//  [python]
def _make_activity_op(self, input_tensor):
        """ Creates the op for calculating the activity of a SOM
        :param input_tensor: A tensor to calculate the activity of. Must be of shape `[batch_size, dim]` where `dim` is
        the dimensionality of the SOM's weights.
        :return A handle to the newly created activity op:
        """
        with self._graph.as_default():
            with tf.name_scope("Activity"):
                # This constant controls the width of the gaussian.
                # The closer to 0 it is, the wider it is.
                c = tf.constant(self._c, dtype="float32")
                # Get the euclidean distance between each neuron and the input vectors
                dist = tf.norm(tf.subtract(
                    tf.expand_dims(self._weights, axis=0),
                    tf.expand_dims(input_tensor, axis=1)),
                    name="Distance", axis=2)  # [batch_size, neurons]

                # Calculate the Gaussian of the activity. Units with distances closer to 0 will have activities
                # closer to 1.
                activity = tf.exp(tf.multiply(
                    tf.pow(dist, 2), c), name="Gaussian")

                # Convert the activity into a softmax probability distribution
                if self._softmax_activity:
                    activity = tf.divide(tf.exp(activity),
                                         tf.expand_dims(tf.reduce_sum(
                                             tf.exp(activity), axis=1), axis=-1),
                                         name="Softmax")

                return tf.identity(activity, name="Output")
//  [python]
def result(self):
    true_mean = tf.truediv(self._true_sum, self._count)
    true_mean2 = tf.math.square(true_mean)
    pred_mean = tf.truediv(self._pred_sum, self._count)
    pred_mean2 = tf.math.square(pred_mean)

    term1 = self._product
    term2 = -tf.multiply(true_mean, self._pred_sum)
    term3 = -tf.multiply(pred_mean, self._true_sum)
    term4 = tf.multiply(self._count, tf.multiply(true_mean, pred_mean))
    covariance = term1 + term2 + term3 + term4

    true_var = self._true_sumsq - tf.multiply(self._count, true_mean2)
    pred_var = self._pred_sumsq - tf.multiply(self._count, pred_mean2)
    pred_var = tf.where(tf.greater(pred_var, 1e-12),
                        pred_var,
                        np.inf*tf.ones_like(pred_var))
    
    tp_var = tf.multiply(tf.math.sqrt(true_var), tf.math.sqrt(pred_var))
    correlation = tf.truediv(covariance, tp_var)

    if self._summarize:
        return tf.reduce_mean(correlation)
    else:
        return correlation
//  [python]
def result(self):
    true_mean = tf.divide(self._true_sum, self._count)
    true_mean2 = tf.math.square(true_mean)
    pred_mean = tf.divide(self._pred_sum, self._count)
    pred_mean2 = tf.math.square(pred_mean)

    term1 = self._product
    term2 = -tf.multiply(true_mean, self._pred_sum)
    term3 = -tf.multiply(pred_mean, self._true_sum)
    term4 = tf.multiply(self._count, tf.multiply(true_mean, pred_mean))
    covariance = term1 + term2 + term3 + term4

    true_var = self._true_sumsq - tf.multiply(self._count, true_mean2)
    pred_var = self._pred_sumsq - tf.multiply(self._count, pred_mean2)
    pred_var = tf.where(tf.greater(pred_var, 1e-12),
                        pred_var,
                        np.inf*tf.ones_like(pred_var))
    
    tp_var = tf.multiply(tf.math.sqrt(true_var), tf.math.sqrt(pred_var))
    correlation = tf.divide(covariance, tp_var)

    if self._summarize:
        return tf.reduce_mean(correlation)
    else:
        return correlation
//  [python]
def get_place_holders():
  #with tf.variable_scope(tf.get_variable_scope()):
  batch = None
  height = None
  width = None
  if user_config.autoencoder == 'SegNet':
    batch = 1
    height = FLAGS.input_height
    width = FLAGS.input_width
  input_ph = tf.placeholder(tf.float32, [batch, height, width, 3], name='input_ph')
  if FLAGS.input_pre_process == 'resNet_pre':
    pre_processed_input = tf.multiply(tf.subtract(tf.truediv(input_ph, 255), 0.5), 2)
  else:
    pre_processed_input = input_ph
  labelsID_ph = tf.placeholder(tf.float32, [batch, None, None, 1], name='labelsID_ph')  # TODO: add 3 to config
  InstanceID_ph = tf.placeholder(tf.float32, [batch, None, None, 3], name='InstanceID_ph')  # TODO: add 3 to config
  Disparity_ph = tf.placeholder(tf.float32, [batch, None, None, 2], name='Disparity_ph')  # TODO: add 1 to config
  if FLAGS.need_resize:
    labelsID_out = tf.image.resize_images(labelsID_ph, [FLAGS.output_height, FLAGS.output_width], method=tf.image.ResizeMethod.BILINEAR,align_corners=False)
    InstanceID_out = tf.image.resize_images(InstanceID_ph, [FLAGS.output_height, FLAGS.output_width], method=tf.image.ResizeMethod.BILINEAR, align_corners=False)
    Disparity_out = tf.image.resize_images(Disparity_ph, [FLAGS.output_height, FLAGS.output_width], method=tf.image.ResizeMethod.BILINEAR, align_corners=False)
  else:
    labelsID_out = labelsID_ph
    InstanceID_out = InstanceID_ph
    Disparity_out = Disparity_ph
  ground_truths_ph = [labelsID_ph, InstanceID_ph, Disparity_ph]
  ground_truths    = [labelsID_out, InstanceID_out, Disparity_out]
  return input_ph, ground_truths_ph, ground_truths, pre_processed_input
//  [python]
def get_place_holders():
  #with tf.variable_scope(tf.get_variable_scope()):
  batch = None
  height = None
  width = None
  if user_config.autoencoder == 'SegNet':
    batch = 1
    height = FLAGS.input_height
    width = FLAGS.input_width
  input_ph = tf.placeholder(tf.float32, [batch, height, width, 3], name='input_ph')
  if FLAGS.input_pre_process == 'resNet_pre':
    pre_processed_input = tf.multiply(tf.subtract(tf.divide(input_ph, 255), 0.5), 2)
  else:
    pre_processed_input = input_ph
  labelsID_ph = tf.placeholder(tf.float32, [batch, None, None, 1], name='labelsID_ph')  # TODO: add 3 to config
  InstanceID_ph = tf.placeholder(tf.float32, [batch, None, None, 3], name='InstanceID_ph')  # TODO: add 3 to config
  Disparity_ph = tf.placeholder(tf.float32, [batch, None, None, 2], name='Disparity_ph')  # TODO: add 1 to config
  if FLAGS.need_resize:
    labelsID_out = tf.image.resize_images(labelsID_ph, [FLAGS.output_height, FLAGS.output_width], method=tf.image.ResizeMethod.BILINEAR,align_corners=False)
    InstanceID_out = tf.image.resize_images(InstanceID_ph, [FLAGS.output_height, FLAGS.output_width], method=tf.image.ResizeMethod.BILINEAR, align_corners=False)
    Disparity_out = tf.image.resize_images(Disparity_ph, [FLAGS.output_height, FLAGS.output_width], method=tf.image.ResizeMethod.BILINEAR, align_corners=False)
  else:
    labelsID_out = labelsID_ph
    InstanceID_out = InstanceID_ph
    Disparity_out = Disparity_ph
  ground_truths_ph = [labelsID_ph, InstanceID_ph, Disparity_ph]
  ground_truths    = [labelsID_out, InstanceID_out, Disparity_out]
  return input_ph, ground_truths_ph, ground_truths, pre_processed_input
//  [python]
def inverse_tukey_hh(x, hl, hr):
  """Compute the inverse of the Tukey HH function.

  The Tukey HH function transforms a standard Gaussian distribution into the
  Tukey HH distribution and it's defined as:

  x = u * exp(hl * u ^ 2) for u < 0 and x = u * exp(hr * u ^ 2) for u >= 0.

  Given the values of x, this function computes the corresponding values of u.

  Args:
    x: The input `Tensor`.
    hl: The "left" parameter of the distribution. It must have the same dtype
      and shape of x (or a broadcastable shape) or be a scalar.
    hr: The "right" parameter of the distribution. It must have the same dtype
      and shape of x (or a broadcastable shape) or be a scalar.

  Returns:
    The inverse of the Tukey HH function.
  """
  def one_side(x, h):
    h_x_square = tf.multiply(h, tf.square(x))
    return tf.where(
        # Prevents the 0 / 0 form for small values of x..
        tf.less(h_x_square, 1.0e-7),
        x,  # The error is < 1e-14 for this case.
        tf.sqrt(t.divide(lambert_w(h_x_square), h)))

  return tf.where(tf.less(x, 0.0), -one_side(-x, hl), one_side(x, hr))
//  [python]
def inverse_tukey_hh(x, hl, hr):
  """Compute the inverse of the Tukey HH function.

  The Tukey HH function transforms a standard Gaussian distribution into the
  Tukey HH distribution and it's defined as:

  x = u * exp(hl * u ^ 2) for u < 0 and x = u * exp(hr * u ^ 2) for u >= 0.

  Given the values of x, this function computes the corresponding values of u.

  Args:
    x: The input `Tensor`.
    hl: The "left" parameter of the distribution. It must have the same dtype
      and shape of x (or a broadcastable shape) or be a scalar.
    hr: The "right" parameter of the distribution. It must have the same dtype
      and shape of x (or a broadcastable shape) or be a scalar.

  Returns:
    The inverse of the Tukey HH function.
  """
  def one_side(x, h):
    h_x_square = tf.multiply(h, tf.square(x))
    return tf.where(
        # Prevents the 0 / 0 form for small values of x..
        tf.less(h_x_square, 1.0e-7),
        x,  # The error is < 1e-14 for this case.
        tf.sqrt(tf.divide(lambert_w(h_x_square), h)))

  return tf.where(tf.less(x, 0.0), -one_side(-x, hl), one_side(x, hr))
//  [python]
def compute_intersections(half_planes, couples):
    # compute the intersections between the couples of half planes
    inter00 = tf.linalg.cross(tf.gather(half_planes,couples[:,0], axis=1), tf.gather(half_planes,couples[:,1], axis=1))
    inter0 = gradient_clipping(inter00)
    mask = tf.abs(inter0[:,:,2])<config.EPS
    inter1 = tf.truediv(inter0,tf.tile(tf.expand_dims(tf.where(mask, tf.ones_like(inter0[:,:,2]),inter0[:,:,2]), 2),[1, 1,3]))
    inter = tf.where(tf.tile(mask[:,:,tf.newaxis], [1, 1, 3]), tf.ones_like(inter1)*1e7,inter1)
    return inter
//  [python]
def compute_intersections(half_planes, couples):
    # compute the intersections between the couples of half planes
    inter00 = tf.linalg.cross(tf.gather(half_planes,couples[:,0], axis=1), tf.gather(half_planes,couples[:,1], axis=1))
    inter0 = gradient_clipping(inter00)
    mask = tf.abs(inter0[:,:,2])<config.EPS
    inter1 = tf.divide(inter0,tf.tile(tf.expand_dims(tf.where(mask, tf.ones_like(inter0[:,:,2]),inter0[:,:,2]), 2),[1, 1,3]))
    inter = tf.where(tf.tile(mask[:,:,tf.newaxis], [1, 1, 3]), tf.ones_like(inter1)*1e7,inter1)
    return inter
//  [python]
def lorentz_centroid(x, n, d):
    # input weighted matrix: [nodes, features]
    coef = tf.sqrt(tf.abs(lorentz_inner(x, x, n, d)))
    coef = tf.clip_by_value(coef, clip_value_min=1e-8, clip_value_max=coef)
    res = tf.div(x, tf.expand_dims(tf.transpose(coef), 1))
    return res
//  [python]
def lorentz_centroid(x, n, d):
    # input weighted matrix: [nodes, features]
    coef = tf.sqrt(tf.abs(lorentz_inner(x, x, n, d)))
    coef = tf.clip_by_value(coef, clip_value_min=1e-8, clip_value_max=coef)
    res = tf.divide(x, tf.expand_dims(tf.transpose(coef), 1))
    return res
//  [python]
def get_middle_points_weighted(center_point, B, square_rB, w_center_point, is_boundary_center=None, is_boundary_B=None):
    n_points = center_point.shape[0]
    n_neighbors = B.shape[1]
    A = tf.tile(center_point[:,tf.newaxis,:], [1,n_neighbors, 1])
    square_rA = tf.tile(w_center_point[:,tf.newaxis], [1, n_neighbors])
    AB_square = tf.square(A - B)
    alpha = tf.where(tf.reduce_sum(AB_square, -1)<1e-8, tf.ones_like(square_rA)*0.5, 0.5 - tf.truediv(square_rA - square_rB, 2.0*(tf.reduce_sum(AB_square, -1))))
    alpha = tf.tile(alpha[:,:,tf.newaxis], [1,1,2])
    middle_points = tf.multiply(A, alpha) + tf.multiply(1-alpha, B)
    return middle_points
//  [python]
def get_middle_points_weighted(center_point, B, square_rB, w_center_point, is_boundary_center=None, is_boundary_B=None):
    n_points = center_point.shape[0]
    n_neighbors = B.shape[1]
    A = tf.tile(center_point[:,tf.newaxis,:], [1,n_neighbors, 1])
    square_rA = tf.tile(w_center_point[:,tf.newaxis], [1, n_neighbors])
    AB_square = tf.square(A - B)
    alpha = tf.where(tf.reduce_sum(AB_square, -1)<1e-8, tf.ones_like(square_rA)*0.5, 0.5 - tf.divide(square_rA - square_rB, 2.0*(tf.reduce_sum(AB_square, -1))))
    alpha = tf.tile(alpha[:,:,tf.newaxis], [1,1,2])
    middle_points = tf.multiply(A, alpha) + tf.multiply(1-alpha, B)
    return middle_points
//  [python]
def comp_half_planes(nn_coord, weights,center_point, w_center_point,is_boundary_center=None, is_boundary_B=None):
    # compute the equations of the half planes
    n_points = nn_coord.shape[0]
    n_neighbors = nn_coord.shape[1]
    middle_points = get_middle_points_weighted(center_point, nn_coord, weights, w_center_point,is_boundary_center=is_boundary_center, is_boundary_B=is_boundary_B)
    dir_vec=  nn_coord - center_point[:,tf.newaxis,:]
    dir_vec = gradient_clipping(dir_vec)
    half_planes_normal =  tf.truediv(dir_vec,tf.maximum(tf.tile(safe_norm(dir_vec, axis = -1)[:,:,tf.newaxis],[1,1, 2]),config.EPS))

    col3 = - (middle_points[:,:,0]*half_planes_normal[:,:,0] + middle_points[:,:,1]*half_planes_normal[:,:,1] )
    half_planes = tf.concat([half_planes_normal, col3[:,:,tf.newaxis]], axis=-1)
    return half_planes
//  [python]
def comp_half_planes(nn_coord, weights,center_point, w_center_point,is_boundary_center=None, is_boundary_B=None):
    # compute the equations of the half planes
    n_points = nn_coord.shape[0]
    n_neighbors = nn_coord.shape[1]
    middle_points = get_middle_points_weighted(center_point, nn_coord, weights, w_center_point,is_boundary_center=is_boundary_center, is_boundary_B=is_boundary_B)
    dir_vec=  nn_coord - center_point[:,tf.newaxis,:]
    dir_vec = gradient_clipping(dir_vec)
    half_planes_normal =  tf.divide(dir_vec,tf.maximum(tf.tile(safe_norm(dir_vec, axis = -1)[:,:,tf.newaxis],[1,1, 2]),config.EPS))

    col3 = - (middle_points[:,:,0]*half_planes_normal[:,:,0] + middle_points[:,:,1]*half_planes_normal[:,:,1] )
    half_planes = tf.concat([half_planes_normal, col3[:,:,tf.newaxis]], axis=-1)
    return half_planes
//  [python]
def compute_proximity_loss(self):
        """Compute the second part (distance from x1) of the loss function."""
        proximity_loss = 0.0
        for i in range(self.total_CFs):
            proximity_loss = tf.add(proximity_loss, self.compute_dist(
                self.cfs_frozen[i], self.x1))
        return tf.truediv(proximity_loss, tf.cast(tf.multiply(len(self.minx[0]), self.total_CFs), dtype=tf.float32))
//  [python]
def compute_proximity_loss(self):
        """Compute the second part (distance from x1) of the loss function."""
        proximity_loss = 0.0
        for i in range(self.total_CFs):
            proximity_loss = tf.add(proximity_loss, self.compute_dist(
                self.cfs_frozen[i], self.x1))
        return tf.divide(proximity_loss, tf.cast(tf.multiply(len(self.minx[0]), self.total_CFs), dtype=tf.float32))
//  [python]
def dpp_style(self, submethod):
        """Computes the DPP of a matrix."""
        det_entries = []
        if submethod == "inverse_dist":
            for i in range(self.total_CFs):
                for j in range(self.total_CFs):
                    det_temp_entry = tf.div(1.0, tf.add(
                        1.0, self.compute_dist(self.cfs[i], self.cfs[j])))
                    if i == j:
                        det_temp_entry = tf.add(det_temp_entry, 0.0001)
                    det_entries.append(det_temp_entry)

        elif submethod == "exponential_dist":
            for i in range(self.total_CFs):
                for j in range(self.total_CFs):
                    det_temp_entry = tf.div(1.0, tf.exp(
                        self.compute_dist(self.cfs[i], self.cfs[j])))
                    det_entries.append(det_temp_entry)

        det_entries = tf.reshape(det_entries, [self.total_CFs, self.total_CFs])
        diversity_loss = tf.compat.v1.matrix_determinant(det_entries)
        return diversity_loss
//  [python]
def dpp_style(self, submethod):
        """Computes the DPP of a matrix."""
        det_entries = []
        if submethod == "inverse_dist":
            for i in range(self.total_CFs):
                for j in range(self.total_CFs):
                    det_temp_entry = tf.divide(1.0, tf.add(
                        1.0, self.compute_dist(self.cfs[i], self.cfs[j])))
                    if i == j:
                        det_temp_entry = tf.add(det_temp_entry, 0.0001)
                    det_entries.append(det_temp_entry)

        elif submethod == "exponential_dist":
            for i in range(self.total_CFs):
                for j in range(self.total_CFs):
                    det_temp_entry = tf.divide(1.0, tf.exp(
                        self.compute_dist(self.cfs[i], self.cfs[j])))
                    det_entries.append(det_temp_entry)

        det_entries = tf.reshape(det_entries, [self.total_CFs, self.total_CFs])
        diversity_loss = tf.compat.v1.matrix_determinant(det_entries)
        return diversity_loss
//  [python]
def __div__(self, other):
        return tf.divide(self, other)
//  [python]
def _predict_mean_reward_and_variance(
      self, global_observation: tf.Tensor, arm_observations: Optional[tf.Tensor]
  ) -> Tuple[tf.Tensor, tf.Tensor]:
    """Predicts mean reward and variance.

    Args:
      global_observation: A `tf.Tensor` shaped as [batch_size,
        self._global_context_dim].
      arm_observations: An optional `tf.Tensor` shaped as [batch_size,
        self._num_actions, self._arm_context_dim]. Expected to be supplied only
        when self._accepts_per_arm_features is true.

    Returns:
      A size-2 tuple of `tf.Tensor`'s representing the predicted reward and
      variance, both shaped as [batch_size, self._num_actions].
    """
    est_reward = []
    est_variance = []
    for k in range(self._num_actions):
      current_observation = tf.linalg.matrix_transpose(
          self._get_current_observation(global_observation, arm_observations, k)
      )
      model_index = policy_utilities.get_model_index(
          k, self._accepts_per_arm_features
      )
      if self._use_eigendecomp:
        q_t_b = tf.matmul(
            self._eig_matrix[model_index], current_observation, transpose_a=True
        )
        lambda_inv = tf.truediv(
            tf.ones_like(self._eig_vals[model_index]),
            self._eig_vals[model_index] + self._tikhonov_weight,
        )
        a_inv_x = tf.matmul(
            self._eig_matrix[model_index],
            tf.einsum('j,jk->jk', lambda_inv, q_t_b),
        )
      else:
        a_inv_x = linalg.conjugate_gradient(
            self._cov_matrix[model_index]
            + self._tikhonov_weight
            * tf.eye(self._overall_context_dim, dtype=self._dtype),
            current_observation,
        )
      est_mean_reward = tf.einsum(
          'j,jk->k', self._data_vector[model_index], a_inv_x
      )
      est_reward.append(est_mean_reward)
      var = tf.einsum('ij,ij->j', current_observation, a_inv_x)
      est_variance.append(var)

    return (tf.stack(est_reward, axis=-1), tf.stack(est_variance, axis=-1))
//  [python]
def _predict_mean_reward_and_variance(
      self, global_observation: tf.Tensor, arm_observations: Optional[tf.Tensor]
  ) -> Tuple[tf.Tensor, tf.Tensor]:
    """Predicts mean reward and variance.

    Args:
      global_observation: A `tf.Tensor` shaped as [batch_size,
        self._global_context_dim].
      arm_observations: An optional `tf.Tensor` shaped as [batch_size,
        self._num_actions, self._arm_context_dim]. Expected to be supplied only
        when self._accepts_per_arm_features is true.

    Returns:
      A size-2 tuple of `tf.Tensor`'s representing the predicted reward and
      variance, both shaped as [batch_size, self._num_actions].
    """
    est_reward = []
    est_variance = []
    for k in range(self._num_actions):
      current_observation = tf.linalg.matrix_transpose(
          self._get_current_observation(global_observation, arm_observations, k)
      )
      model_index = policy_utilities.get_model_index(
          k, self._accepts_per_arm_features
      )
      if self._use_eigendecomp:
        q_t_b = tf.matmul(
            self._eig_matrix[model_index], current_observation, transpose_a=True
        )
        lambda_inv = tf.divide(
            tf.ones_like(self._eig_vals[model_index]),
            self._eig_vals[model_index] + self._tikhonov_weight,
        )
        a_inv_x = tf.matmul(
            self._eig_matrix[model_index],
            tf.einsum('j,jk->jk', lambda_inv, q_t_b),
        )
      else:
        a_inv_x = linalg.conjugate_gradient(
            self._cov_matrix[model_index]
            + self._tikhonov_weight
            * tf.eye(self._overall_context_dim, dtype=self._dtype),
            current_observation,
        )
      est_mean_reward = tf.einsum(
          'j,jk->k', self._data_vector[model_index], a_inv_x
      )
      est_reward.append(est_mean_reward)
      var = tf.einsum('ij,ij->j', current_observation, a_inv_x)
      est_variance.append(var)

    return (tf.stack(est_reward, axis=-1), tf.stack(est_variance, axis=-1))
//  [python]
def __rdiv__(self, other):
        return tf.divide(other, self)
//  [python]
def compute_diversity_loss(self, method):
        """Computes the third part (diversity) of the loss function."""
        if self.total_CFs == 1:
            return tf.constant(0.0)

        if "dpp" in method:
            submethod = method.split(':')[1]
            return tf.reduce_sum(self.dpp_style(submethod))
        elif method == "avg_dist":
            diversity_loss = 0.0
            count = 0.0
            # computing pairwise distance and transforming it to normalized similarity
            for i in range(self.total_CFs):
                for j in range(i+1, self.total_CFs):
                    count += 1.0
                    diversity_loss = tf.add(
                        diversity_loss,
                        tf.truediv(1.0,
                                  tf.add(1.0,
                                         self.compute_dist(self.cfs_frozen[i], self.cfs_frozen[j]))))
            return tf.subtract(1.0, tf.truediv(diversity_loss, count))
//  [python]
def compute_diversity_loss(self, method):
        """Computes the third part (diversity) of the loss function."""
        if self.total_CFs == 1:
            return tf.constant(0.0)

        if "dpp" in method:
            submethod = method.split(':')[1]
            return tf.reduce_sum(self.dpp_style(submethod))
        elif method == "avg_dist":
            diversity_loss = 0.0
            count = 0.0
            # computing pairwise distance and transforming it to normalized similarity
            for i in range(self.total_CFs):
                for j in range(i+1, self.total_CFs):
                    count += 1.0
                    diversity_loss = tf.add(
                        diversity_loss,
                        tf.divide(1.0,
                                  tf.add(1.0,
                                         self.compute_dist(self.cfs_frozen[i], self.cfs_frozen[j]))))
            return tf.subtract(1.0, tf.divide(diversity_loss, count))
//  [python]
def random_keep(self, mask, num_keep):
        curr_num = tf.reduce_sum(
                         tf.cast(mask, tf.float32))
        keep_ratio = tf.truediv(num_keep,
                         curr_num + 1.0)
        rand_select = tf.random.uniform(
                         shape=tf.shape(mask),
                         minval=0,
                         maxval=1)
        keep = tf.less(rand_select, keep_ratio)
        mask = tf.logical_and(mask, keep)
        return mask
//  [python]
def random_keep(self, mask, num_keep):
        curr_num = tf.reduce_sum(
                         tf.cast(mask, tf.float32))
        keep_ratio = tf.divide(num_keep,
                         curr_num + 1.0)
        rand_select = tf.random.uniform(
                         shape=tf.shape(mask),
                         minval=0,
                         maxval=1)
        keep = tf.less(rand_select, keep_ratio)
        mask = tf.logical_and(mask, keep)
        return mask
//  [python]
def tone_mapper(image, gamma):
  """Applies gamma correction to the input image.

  Tone maps the input image batch in order to make scenes with a high dynamic
  range viewable. The gamma correction factor is computed separately per image,
  but is shared between all provided channels. The exact function computed is:

  image_out = A*image_in^gamma, where A is an image-wide constant computed so
  that the maximum image value is approximately 1. The correction is applied
  to all channels.

  Args:
    image: 4-D float32 tensor with shape [batch_size, image_height,
        image_width, channel_count]. The batch of images to tone map.
    gamma: 0-D float32 nonnegative tensor. Values of gamma below one compress
        relative contrast in the image, and values above one increase it. A
        value of 1 is equivalent to scaling the image to have a maximum value
        of 1.
  Returns:
    4-D float32 tensor with shape [batch_size, image_height, image_width,
    channel_count]. Contains the gamma-corrected images, clipped to the range
    [0, 1].
  """
  batch_size = image.shape[0].value
  corrected_image = tf.pow(image, gamma)
  image_max = tf.reduce_max(tf.reshape(corrected_image, [batch_size, -1]),
                            axis=1)
  scaled_image = tf.truediv(corrected_image,
                           tf.reshape(image_max, [batch_size, 1, 1, 1]))
  return tf.clip_by_value(scaled_image, 0.0, 1.0)
//  [python]
def tone_mapper(image, gamma):
  """Applies gamma correction to the input image.

  Tone maps the input image batch in order to make scenes with a high dynamic
  range viewable. The gamma correction factor is computed separately per image,
  but is shared between all provided channels. The exact function computed is:

  image_out = A*image_in^gamma, where A is an image-wide constant computed so
  that the maximum image value is approximately 1. The correction is applied
  to all channels.

  Args:
    image: 4-D float32 tensor with shape [batch_size, image_height,
        image_width, channel_count]. The batch of images to tone map.
    gamma: 0-D float32 nonnegative tensor. Values of gamma below one compress
        relative contrast in the image, and values above one increase it. A
        value of 1 is equivalent to scaling the image to have a maximum value
        of 1.
  Returns:
    4-D float32 tensor with shape [batch_size, image_height, image_width,
    channel_count]. Contains the gamma-corrected images, clipped to the range
    [0, 1].
  """
  batch_size = image.shape[0].value
  corrected_image = tf.pow(image, gamma)
  image_max = tf.reduce_max(tf.reshape(corrected_image, [batch_size, -1]),
                            axis=1)
  scaled_image = tf.divide(corrected_image,
                           tf.reshape(image_max, [batch_size, 1, 1, 1]))
  return tf.clip_by_value(scaled_image, 0.0, 1.0)
//  [python]
def drop_path(x, drop_prob=0.0, training=False):
    """
    Obtained from: github.com:rwightman/pytorch-image-models
    Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
    This is the same as the DropConnect impl I created for EfficientNet, etc networks, however,
    the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
    See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for
    changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use
    'survival rate' as the argument.
    """
    if drop_prob == 0.0 or not training:
        return x
    keep_prob = 1 - drop_prob
    shape = [x.shape[0]] + [1] * (tf.rank(x).numpy() - 1)  # work with diff dim tensors, not just 2D ConvNets
    random_tensor = keep_prob + tf.random.uniform(shape=shape, minval=0.0, maxval=1.0, dtype=x.dtype)
    random_tensor = tf.floor(random_tensor) # binarize
    x = tf.truediv(x, keep_prob) * random_tensor
    return x
//  [python]
def drop_path(x, drop_prob=0.0, training=False):
    """
    Obtained from: github.com:rwightman/pytorch-image-models
    Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
    This is the same as the DropConnect impl I created for EfficientNet, etc networks, however,
    the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
    See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for
    changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use
    'survival rate' as the argument.
    """
    if drop_prob == 0.0 or not training:
        return x
    keep_prob = 1 - drop_prob
    shape = [x.shape[0]] + [1] * (tf.rank(x).numpy() - 1)  # work with diff dim tensors, not just 2D ConvNets
    random_tensor = keep_prob + tf.random.uniform(shape=shape, minval=0.0, maxval=1.0, dtype=x.dtype)
    random_tensor = tf.floor(random_tensor) # binarize
    x = tf.divide(x, keep_prob) * random_tensor
    return x
//  [python]
with tf.name_scope("a_name_scope"):
    initializer = tf.constant_initializer(value=1)
    var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32, initializer=initializer)
    var2 = tf.Variable(name='var2', initial_value=[2], dtype=tf.float32)
    var21 = tf.Variable(name='var2', initial_value=[2.1], dtype=tf.float32)
    var22 = tf.Variable(name='var2', initial_value=[2.2], dtype=tf.float32)


with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    print(var1.name)        # var1:0
    print(sess.run(var1))   # [ 1.]
    print(var2.name)        # a_name_scope/var2:0
    print(sess.run(var2))   # [ 2.]
    print(var21.name)       # a_name_scope/var2_1:0
    print(sess.run(var21))  # [ 2.0999999]
    print(var22.name)       # a_name_scope/var2_2:0
    print(sess.run(var22))
//  [python]
with tf.name_scope("a_name_scope"):
    initializer = tf.constant_initializer(value=1)
    var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32, initializer=initializer)
    var2 = tf.Variable(name='var2', initial_value=[2], dtype=tf.float32)
    var21 = tf.Variable(name='var2', initial_value=[2.1], dtype=tf.float32)
    var22 = tf.Variable(name='var2', initial_value=[2.2], dtype=tf.float32)


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(var1.name)        # var1:0
    print(sess.run(var1))   # [ 1.]
    print(var2.name)        # a_name_scope/var2:0
    print(sess.run(var2))   # [ 2.]
    print(var21.name)       # a_name_scope/var2_1:0
    print(sess.run(var21))  # [ 2.0999999]
    print(var22.name)       # a_name_scope/var2_2:0
    print(sess.run(var22))
//  [python]
def lossless_loss_fn(batch_size):
    def lossless_loss(y_true, y_pred):
        N = tf.constant(4096.0, shape=[1], dtype=tf.float32)
        beta = tf.constant(4096.0, shape=[1], dtype=tf.float32)
        y_pred = K.clip(y_pred, _EPSILON, 1.0-_EPSILON)
        loss =  tf.convert_to_tensor(0,dtype=tf.float32)
        g = tf.constant(1.0, shape=[1], dtype=tf.float32)
        const1 = tf.constant(1.0, shape=[1], dtype=tf.float32)
        for i in range(0,batch_size,3):
            try:
                anchor = y_pred[i+0]
                positive =  y_pred[i+1]
                negative = y_pred[i+2]

                pos_dist = K.sum(K.square(anchor-positive),1)
                neg_dist = K.sum(K.square(anchor,negative),1)

                pos_dist = -tf.log(-tf.truediv((pos_dist), beta)+const1+epsilon)
                neg_dist = -tf.log(-tf.truediv((N-neg_dist), beta)+const1+epsilon)

                _loss = neg_dist + pos_dist

                loss = (loss + g + _loss)
            except:
                continue
        loss = loss/(batch_size/3)
        zero = tf.constant(0.0, shape=[1], dtype=tf.float32)
        return tf.maximum(loss,zero)
    return lossless_loss
//  [python]
def lossless_loss_fn(batch_size):
    def lossless_loss(y_true, y_pred):
        N = tf.constant(4096.0, shape=[1], dtype=tf.float32)
        beta = tf.constant(4096.0, shape=[1], dtype=tf.float32)
        y_pred = K.clip(y_pred, _EPSILON, 1.0-_EPSILON)
        loss =  tf.convert_to_tensor(0,dtype=tf.float32)
        g = tf.constant(1.0, shape=[1], dtype=tf.float32)
        const1 = tf.constant(1.0, shape=[1], dtype=tf.float32)
        for i in range(0,batch_size,3):
            try:
                anchor = y_pred[i+0]
                positive =  y_pred[i+1]
                negative = y_pred[i+2]

                pos_dist = K.sum(K.square(anchor-positive),1)
                neg_dist = K.sum(K.square(anchor,negative),1)

                pos_dist = -tf.log(-tf.divide((pos_dist), beta)+const1+epsilon)
                neg_dist = -tf.log(-tf.divide((N-neg_dist), beta)+const1+epsilon)

                _loss = neg_dist + pos_dist

                loss = (loss + g + _loss)
            except:
                continue
        loss = loss/(batch_size/3)
        zero = tf.constant(0.0, shape=[1], dtype=tf.float32)
        return tf.maximum(loss,zero)
    return lossless_loss
//  [python]
def testVar(self):

    @function.Defun(tf.float32)
    def Foo(x):
      return x * x + 1

    g = tf.Graph()
    with g.as_default():
      v = tf.Variable(tf.constant(10.0))
      z = Foo(v)

    with self.test_session(graph=g):
      tf.initialize_all_variables().run()
      self.assertAllEqual(z.eval(), 101.)
//  [python]
def testVar(self):

    @function.Defun(tf.float32)
    def Foo(x):
      return x * x + 1

    g = tf.Graph()
    with g.as_default():
      v = tf.Variable(tf.constant(10.0))
      z = Foo(v)

    with self.test_session(graph=g):
      tf.global_variables_initializer().run()
      self.assertAllEqual(z.eval(), 101.)