jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def __call__(self, labels, feature_vectors, sample_weight=None):
        # Normalize feature vectors
        feature_vectors_normalized = tf.math.l2_normalize(feature_vectors, axis=1)
        # Compute logits
        logits = tf.truediv(
            tf.matmul(
                feature_vectors_normalized, tf.transpose(feature_vectors_normalized)
            ),
            self.temperature,
        )
        return tfa.losses.npairs_loss(tf.squeeze(labels), logits)
//  [python]
def __call__(self, labels, feature_vectors, sample_weight=None):
        # Normalize feature vectors
        feature_vectors_normalized = tf.math.l2_normalize(feature_vectors, axis=1)
        # Compute logits
        logits = tf.divide(
            tf.matmul(
                feature_vectors_normalized, tf.transpose(feature_vectors_normalized)
            ),
            self.temperature,
        )
        return tfa.losses.npairs_loss(tf.squeeze(labels), logits)
//  [python]
def _discretize_struct(struct, step_size):
  """Scales and rounds each tensor of the structure to the integer grid."""

  def discretize_tensor(x):
    scaled_x = tf.truediv(tf.cast(x, tf.float32), step_size)
    discretized_x = tf.round(scaled_x)
    return tf.cast(discretized_x, OUTPUT_TF_TYPE)

  return tf.nest.map_structure(discretize_tensor, struct)
//  [python]
def _discretize_struct(struct, step_size):
  """Scales and rounds each tensor of the structure to the integer grid."""

  def discretize_tensor(x):
    scaled_x = tf.divide(tf.cast(x, tf.float32), step_size)
    discretized_x = tf.round(scaled_x)
    return tf.cast(discretized_x, OUTPUT_TF_TYPE)

  return tf.nest.map_structure(discretize_tensor, struct)
//  [python]
def global_reduce_mean(self, tensor, axis=None, replica_context=None):
    """Return global mean across multiple replica."""
    return tf.truediv(
        tf.reduce_sum(tensor, axis=axis),
        self.cross_replica_concat(tensor,
                                  replica_context=replica_context).shape[0])
//  [python]
def global_reduce_mean(self, tensor, axis=None, replica_context=None):
    """Return global mean across multiple replica."""
    return tf.divide(
        tf.reduce_sum(tensor, axis=axis),
        self.cross_replica_concat(tensor,
                                  replica_context=replica_context).shape[0])
//  [python]
def calculate_contextual_similarity(scaled_distances, sigma=float(0.1), b=float(1.0)):
  cs = tf.exp((b - scaled_distances) / sigma)
  reduce_sum = tf.reduce_sum(cs, axis=3, keepdims=True)
  cs_norm = tf.truediv(cs, reduce_sum)
  return cs_norm
//  [python]
def calculate_contextual_similarity(scaled_distances, sigma=float(0.1), b=float(1.0)):
  cs = tf.exp((b - scaled_distances) / sigma)
  reduce_sum = tf.reduce_sum(cs, axis=3, keepdims=True)
  cs_norm = tf.divide(cs, reduce_sum)
  return cs_norm
//  [python]
def l1_masked_loss(logits, gt, mask):
  valus_diff = tf.abs(tf.subtract(logits, gt))
  L1_loss = tf.truediv(tf.reduce_sum(valus_diff), tf.add(tf.reduce_sum(mask[:, :, :, 0]), 0.0001))
  return L1_loss
//  [python]
def l1_masked_loss(logits, gt, mask):
  valus_diff = tf.abs(tf.subtract(logits, gt))
  L1_loss = tf.divide(tf.reduce_sum(valus_diff), tf.add(tf.reduce_sum(mask[:, :, :, 0]), 0.0001))
  return L1_loss
//  [python]
def _call(self, inputs):
        coord = inputs
        X = inputs[:, 0]
        Y = inputs[:, 1]
        Z = inputs[:, 2]

        h = 112.0 * tf.div(-Y, -Z) + 111.5
        w = 112.0 * tf.div(X, -Z) + 111.5

        h = tf.minimum(tf.maximum(h, 0), 223)
        w = tf.minimum(tf.maximum(w, 0), 223)

        x = h/(224.0/56)
        y = w/(224.0/56)
        out1 = self.project(self.img_feat[0], x, y, 64)

        x = h/(224.0/28)
        y = w/(224.0/28)
        out2 = self.project(self.img_feat[1], x, y, 128)

        x = h/(224.0/14)
        y = w/(224.0/14)
        out3 = self.project(self.img_feat[2], x, y, 256)

        x = h/(224.0/7)
        y = w/(224.0/7)
        out4 = self.project(self.img_feat[3], x, y, 512)
        outputs = tf.concat([coord,out1,out2,out3,out4], 1)
        return outputs
//  [python]
def _call(self, inputs):
        coord = inputs
        X = inputs[:, 0]
        Y = inputs[:, 1]
        Z = inputs[:, 2]

        h = 112.0 * tf.divide(-Y, -Z) + 111.5
        w = 112.0 * tf.divide(X, -Z) + 111.5

        h = tf.minimum(tf.maximum(h, 0), 223)
        w = tf.minimum(tf.maximum(w, 0), 223)

        x = h/(224.0/56)
        y = w/(224.0/56)
        out1 = self.project(self.img_feat[0], x, y, 64)

        x = h/(224.0/28)
        y = w/(224.0/28)
        out2 = self.project(self.img_feat[1], x, y, 128)

        x = h/(224.0/14)
        y = w/(224.0/14)
        out3 = self.project(self.img_feat[2], x, y, 256)

        x = h/(224.0/7)
        y = w/(224.0/7)
        out4 = self.project(self.img_feat[3], x, y, 512)
        outputs = tf.concat([coord,out1,out2,out3,out4], 1)
        return outputs
//  [python]
def normalize_vgg(self, input, name):
        # normalize input -1.0 ~ 1.0
        input = tf.div(input, 255.0, name=name + '_divide')
        input = tf.subtract(input, 0.5, name=name + '_subtract')
        input = tf.multiply(input, 2.0, name=name + '_multiply')
        return input
//  [python]
def normalize_vgg(self, input, name):
        # normalize input -1.0 ~ 1.0
        input = tf.divide(input, 255.0, name=name + '_divide')
        input = tf.subtract(input, 0.5, name=name + '_subtract')
        input = tf.multiply(input, 2.0, name=name + '_multiply')
        return input
//  [python]
def train_step(self, iterator):
    """Train step."""

    def step_fn(data):
      x1, x2 = data[0], data[1]
      replica_context = tf.distribute.get_replica_context()
      y = self.get_target_labels(
          x1, is_onehot=True, replica_context=replica_context)
      with tf.GradientTape() as tape:
        xc = tf.concat((x1, x2), axis=0)
        embeds = self.model(xc, training=True)['embeds']
        embeds = tf.nn.l2_normalize(embeds, axis=1)
        embeds1, embeds2 = tf.split(embeds, 2)
        embeds2_concat = self.cross_replica_concat(
            embeds2, replica_context=replica_context)
        ip = tf.matmul(embeds1, embeds2_concat, transpose_b=True)
        loss_xe = tf.keras.losses.categorical_crossentropy(
            y, tf.truediv(ip, self.temperature), from_logits=True)
        loss_xe = self.global_reduce_mean(loss_xe)
        loss_l2 = self.loss_l2(self.model.trainable_weights)
        loss = loss_xe + self.weight_decay * loss_l2
      grad = tape.gradient(loss, self.model.trainable_weights)
      self.optimizer.apply_gradients(zip(grad, self.model.trainable_weights))
      # monitor
      self.metrics['loss.train'].update_state(loss)
      self.metrics['loss.xe'].update_state(loss_xe)
      self.metrics['loss.L2'].update_state(loss_l2)
      self.metrics['acc.train'].update_state(
          tf.argmax(y, axis=1), tf.argmax(ip, axis=1))

    # Call one step
    self.strategy.run(step_fn, args=(next(iterator),))
//  [python]
def train_step(self, iterator):
    """Train step."""

    def step_fn(data):
      x1, x2 = data[0], data[1]
      replica_context = tf.distribute.get_replica_context()
      y = self.get_target_labels(
          x1, is_onehot=True, replica_context=replica_context)
      with tf.GradientTape() as tape:
        xc = tf.concat((x1, x2), axis=0)
        embeds = self.model(xc, training=True)['embeds']
        embeds = tf.nn.l2_normalize(embeds, axis=1)
        embeds1, embeds2 = tf.split(embeds, 2)
        embeds2_concat = self.cross_replica_concat(
            embeds2, replica_context=replica_context)
        ip = tf.matmul(embeds1, embeds2_concat, transpose_b=True)
        loss_xe = tf.keras.losses.categorical_crossentropy(
            y, tf.divide(ip, self.temperature), from_logits=True)
        loss_xe = self.global_reduce_mean(loss_xe)
        loss_l2 = self.loss_l2(self.model.trainable_weights)
        loss = loss_xe + self.weight_decay * loss_l2
      grad = tape.gradient(loss, self.model.trainable_weights)
      self.optimizer.apply_gradients(zip(grad, self.model.trainable_weights))
      # monitor
      self.metrics['loss.train'].update_state(loss)
      self.metrics['loss.xe'].update_state(loss_xe)
      self.metrics['loss.L2'].update_state(loss_l2)
      self.metrics['acc.train'].update_state(
          tf.argmax(y, axis=1), tf.argmax(ip, axis=1))

    # Call one step
    self.strategy.run(step_fn, args=(next(iterator),))
//  [python]
def normalize_mobilenet(self, input, name):
        input = tf.truediv(input, 255.0, name=name + '_divide')
        input = tf.subtract(input, 0.5, name=name + '_subtract')
        input = tf.multiply(input, 2.0, name=name + '_multiply')
        return input
//  [python]
def normalize_mobilenet(self, input, name):
        input = tf.divide(input, 255.0, name=name + '_divide')
        input = tf.subtract(input, 0.5, name=name + '_subtract')
        input = tf.multiply(input, 2.0, name=name + '_multiply')
        return input
//  [python]
def r2_op(predictions, targets):
    """ r2_op.

    An op that calculates the standard error.

    Examples:
        
        input_data = placeholder(shape=[None, 784])
        y_pred = my_network(input_data) # Apply some ops
        y_true = placeholder(shape=[None, 10]) # Labels
        stderr_op = r2_op(y_pred, y_true)

        # Calculate standard error by feeding data X and labels Y
        std_error = sess.run(stderr_op, feed_dict={input_data: X, y_true: Y})
        

    Arguments:
        predictions: `Tensor`.
        targets: `Tensor`.

    Returns:
        `Float`. The standard error.

    """
    with tf.name_scope('StandardError'):
        a = tf.reduce_sum(tf.square(predictions))
        b = tf.reduce_sum(tf.square(targets))
        return tf.truediv(a, b)
//  [python]
def r2_op(predictions, targets):
    """ r2_op.

    An op that calculates the standard error.

    Examples:
        ```python
        input_data = placeholder(shape=[None, 784])
        y_pred = my_network(input_data) # Apply some ops
        y_true = placeholder(shape=[None, 10]) # Labels
        stderr_op = r2_op(y_pred, y_true)

        # Calculate standard error by feeding data X and labels Y
        std_error = sess.run(stderr_op, feed_dict={input_data: X, y_true: Y})
        ```

    Arguments:
        predictions: `Tensor`.
        targets: `Tensor`.

    Returns:
        `Float`. The standard error.

    """
    with tf.name_scope('StandardError'):
        a = tf.reduce_sum(tf.square(predictions))
        b = tf.reduce_sum(tf.square(targets))
        return tf.divide(a, b)
//  [python]
def normalize_nasnet(self, input, name):
        input = tf.truediv(input, 255.0, name=name + '_divide')
        input = tf.subtract(input, 0.5, name=name + '_subtract')
        input = tf.multiply(input, 2.0, name=name + '_multiply')
        return input
//  [python]
def normalize_nasnet(self, input, name):
        input = tf.divide(input, 255.0, name=name + '_divide')
        input = tf.subtract(input, 0.5, name=name + '_subtract')
        input = tf.multiply(input, 2.0, name=name + '_multiply')
        return input
//  [python]
def weighted_r2_op(predictions, targets, inputs):
    """ weighted_r2_op.

    An op that calculates the standard error.

    Examples:
        
        input_data = placeholder(shape=[None, 784])
        y_pred = my_network(input_data) # Apply some ops
        y_true = placeholder(shape=[None, 10]) # Labels
        stderr_op = weighted_r2_op(y_pred, y_true, input_data)

        # Calculate standard error by feeding data X and labels Y
        std_error = sess.run(stderr_op, feed_dict={input_data: X, y_true: Y})
        

    Arguments:
        predictions: `Tensor`.
        targets: `Tensor`.
        inputs: `Tensor`.

    Returns:
        `Float`. The standard error.

    """
    with tf.name_scope('WeightedStandardError'):
        if hasattr(inputs, '__len__'):
            inputs = tf.add_n(inputs)
        if inputs.get_shape().as_list() != targets.get_shape().as_list():
            raise Exception("Weighted R2 metric requires Inputs and Targets to "
                            "have same shape.")
        a = tf.reduce_sum(tf.square(predictions - inputs))
        b = tf.reduce_sum(tf.square(targets - inputs))
        return tf.truediv(a, b)
//  [python]
def weighted_r2_op(predictions, targets, inputs):
    """ weighted_r2_op.

    An op that calculates the standard error.

    Examples:
        ```python
        input_data = placeholder(shape=[None, 784])
        y_pred = my_network(input_data) # Apply some ops
        y_true = placeholder(shape=[None, 10]) # Labels
        stderr_op = weighted_r2_op(y_pred, y_true, input_data)

        # Calculate standard error by feeding data X and labels Y
        std_error = sess.run(stderr_op, feed_dict={input_data: X, y_true: Y})
        ```

    Arguments:
        predictions: `Tensor`.
        targets: `Tensor`.
        inputs: `Tensor`.

    Returns:
        `Float`. The standard error.

    """
    with tf.name_scope('WeightedStandardError'):
        if hasattr(inputs, '__len__'):
            inputs = tf.add_n(inputs)
        if inputs.get_shape().as_list() != targets.get_shape().as_list():
            raise Exception("Weighted R2 metric requires Inputs and Targets to "
                            "have same shape.")
        a = tf.reduce_sum(tf.square(predictions - inputs))
        b = tf.reduce_sum(tf.square(targets - inputs))
        return tf.divide(a, b)
//  [python]
def kl_divergence(p, q):
    tf.assert_rank(p,2)
    tf.assert_rank(q,2)
    
    p_shape = tf.shape(p)
    q_shape = tf.shape(q)
    tf.assert_equal(p_shape, q_shape)
    
    # normalize sum to 1
    p_ = tf.div(p, tf.tile(tf.expand_dims(tf.reduce_sum(p,axis=1), 1), [1,p_shape[1]]))
    q_ = tf.div(q, tf.tile(tf.expand_dims(tf.reduce_sum(q,axis=1), 1), [1,p_shape[1]]))
    
    return tf.reduce_sum(tf.multiply(p_, tf.log(tf.div(p_, q_))), axis=1)
//  [python]
def kl_divergence(p, q):
    tf.assert_rank(p,2)
    tf.assert_rank(q,2)
    
    p_shape = tf.shape(p)
    q_shape = tf.shape(q)
    tf.assert_equal(p_shape, q_shape)
    
    # normalize sum to 1
    p_ = tf.divide(p, tf.tile(tf.expand_dims(tf.reduce_sum(p,axis=1), 1), [1,p_shape[1]]))
    q_ = tf.divide(q, tf.tile(tf.expand_dims(tf.reduce_sum(q,axis=1), 1), [1,p_shape[1]]))
    
    return tf.reduce_sum(tf.multiply(p_, tf.log(tf.divide(p_, q_))), axis=1)
//  [python]
def norm_boxes_graph(boxes, shape):
    """Converts boxes from pixel coordinates to normalized coordinates.
    boxes: [..., (x1, y1, x2, y2)] in pixel coordinates
    shape: [..., (width, height)] in pixels

    Note: In pixel coordinates (x2, y2) is outside the box. But in normalized
    coordinates it's inside the box.

    Returns:
        [..., (x1, y1, x2, y2)] in normalized coordinates
    """
    w, h = tf.split(tf.cast(shape, tf.float32), 2)
    scale = tf.concat([w, h, w, h], axis=-1) - tf.constant(1.0)
    shift = tf.constant([0., 0., 1., 1.])
    return tf.truediv(boxes - shift, scale)
//  [python]
def norm_boxes_graph(boxes, shape):
    """Converts boxes from pixel coordinates to normalized coordinates.
    boxes: [..., (x1, y1, x2, y2)] in pixel coordinates
    shape: [..., (width, height)] in pixels

    Note: In pixel coordinates (x2, y2) is outside the box. But in normalized
    coordinates it's inside the box.

    Returns:
        [..., (x1, y1, x2, y2)] in normalized coordinates
    """
    w, h = tf.split(tf.cast(shape, tf.float32), 2)
    scale = tf.concat([w, h, w, h], axis=-1) - tf.constant(1.0)
    shift = tf.constant([0., 0., 1., 1.])
    return tf.divide(boxes - shift, scale)
//  [python]
def normalize_to_unit_sum(x, EPS=1e-10):
    ''' Along the last dim '''
    EPS = tf.constant(EPS, dtype=tf.float32)
    x = x + EPS
    x_sum = tf.reduce_sum(x, -1, keep_dims=True)
    x = tf.truediv(x, x_sum)
    return x
//  [python]
def normalize_to_unit_sum(x, EPS=1e-10):
    ''' Along the last dim '''
    EPS = tf.constant(EPS, dtype=tf.float32)
    x = x + EPS
    x_sum = tf.reduce_sum(x, -1, keep_dims=True)
    x = tf.divide(x, x_sum)
    return x
//  [python]
def go_for_video(self, video_path, detect_faces_first: bool = True):
		y_map = {0: "REAL", 1: "FAKE"}
		all_frames = self.faceExtractor.extract_frames(video_path, 20)

		for face in all_frames:
			try:
				if detect_faces_first:
					faces_all, frames = self.faceExtractor.extract([face])
					for face in faces_all:
						face = face[0]

						face = tf.image.resize(face, (self.input_shape[0], self.input_shape[1]), method="nearest")
						aa = tf.nn.softmax(self.model(tf.expand_dims(tf.truediv(tf.cast(face, tf.float32), 255.), 0)))

						if self.json[video_path.split("/")[-1]]["label"] == y_map[np.argmax(aa)]:  # self.json[video_path.split("/")[-1]]["label"]
							return True
						else:
							return False
			except:
				continue
//  [python]
def go_for_video(self, video_path, detect_faces_first: bool = True):
		y_map = {0: "REAL", 1: "FAKE"}
		all_frames = self.faceExtractor.extract_frames(video_path, 20)

		for face in all_frames:
			try:
				if detect_faces_first:
					faces_all, frames = self.faceExtractor.extract([face])
					for face in faces_all:
						face = face[0]

						face = tf.image.resize(face, (self.input_shape[0], self.input_shape[1]), method="nearest")
						aa = tf.nn.softmax(self.model(tf.expand_dims(tf.divide(tf.cast(face, tf.float32), 255.), 0)))

						if self.json[video_path.split("/")[-1]]["label"] == y_map[np.argmax(aa)]:  # self.json[video_path.split("/")[-1]]["label"]
							return True
						else:
							return False
			except:
				continue
//  [python]
def get_coords_2d(map, camera):
    # Creates a grid containing pixel coordinates normalized by the camera focal length

    b, h, w, c = map.get_shape().as_list()
    h_range = tf.range(0., h, 1.0, dtype=tf.float32) + 0.5
    w_range = tf.range(0., w, 1.0, dtype=tf.float32) + 0.5
    grid_x, grid_y = tf.meshgrid(w_range, h_range)
    mesh = tf.reshape(tf.stack([grid_x, grid_y], axis=2), [1, h, w, 2]) - tf.reshape(camera["c"], [b, 1, 1, 2])

    coords_2d = tf.concat([tf.truediv(mesh, tf.reshape(camera["f"], [b, 1, 1, 2])), tf.ones([b,h,w,1])], axis=-1)
    coords_2d = tf.expand_dims(coords_2d, -1)
    return coords_2d, mesh
//  [python]
def get_coords_2d(map, camera):
    # Creates a grid containing pixel coordinates normalized by the camera focal length

    b, h, w, c = map.get_shape().as_list()
    h_range = tf.range(0., h, 1.0, dtype=tf.float32) + 0.5
    w_range = tf.range(0., w, 1.0, dtype=tf.float32) + 0.5
    grid_x, grid_y = tf.meshgrid(w_range, h_range)
    mesh = tf.reshape(tf.stack([grid_x, grid_y], axis=2), [1, h, w, 2]) - tf.reshape(camera["c"], [b, 1, 1, 2])

    coords_2d = tf.concat([tf.divide(mesh, tf.reshape(camera["f"], [b, 1, 1, 2])), tf.ones([b,h,w,1])], axis=-1)
    coords_2d = tf.expand_dims(coords_2d, -1)
    return coords_2d, mesh
//  [python]
def call(self, input):
        """
        Scales the input and computes input/temperature.
        Parameters
        ---------
        input: `Tensor` object
            Tensors (should be logits) as nominator in scaling operation.

        Returns
        ------
        scaled input w.r.t temperature parameter (input/temperature).
        """
        return tf.truediv(input, self.temperature_var)
//  [python]
def call(self, input):
        """
        Scales the input and computes input/temperature.
        Parameters
        ---------
        input: `Tensor` object
            Tensors (should be logits) as nominator in scaling operation.

        Returns
        ------
        scaled input w.r.t temperature parameter (input/temperature).
        """
        return tf.divide(input, self.temperature_var)
//  [python]
def eval_relevance_model(scorer: RelevanceScorer, logits: np.ndarray, labels, temperature=None):
    """Evaluates the relevance model given the logits and labels
    Parameters
    ----------
        scorer: RelevanceScorer
                Scorer of the RelevanceModel
        logits: numpy.ndarray
                input of softmax
        labels: numpy.ndarray
                class labels
        temperature: TF.Tensor
                temperature parameter of size=(1)
    Returns
    -------
        `np.ndarray`
         accuracy value,
         `tf.Tenosr`
         NLL loss,
         `tf.Tensor`
         softmaxes
    """
    # convert to TF.Tensor
    logits_tensor = tf.constant(logits, name='logits_test', dtype=tf.float32)
    labels_tensor = tf.constant(labels, name='labels_test', dtype=tf.int32)

    if temperature:
        logits_tensor = tf.truediv(logits_tensor, temperature)
    nll = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels_tensor, logits=logits_tensor))

    # getting the softmaxes using the model final activation function
    softmaxes = scorer.final_activation_op(logits_tensor, {})
    return accuracy(softmaxes, labels_tensor), tf.identity(nll), softmaxes
//  [python]
def eval_relevance_model(scorer: RelevanceScorer, logits: np.ndarray, labels, temperature=None):
    """Evaluates the relevance model given the logits and labels
    Parameters
    ----------
        scorer: RelevanceScorer
                Scorer of the RelevanceModel
        logits: numpy.ndarray
                input of softmax
        labels: numpy.ndarray
                class labels
        temperature: TF.Tensor
                temperature parameter of size=(1)
    Returns
    -------
        `np.ndarray`
         accuracy value,
         `tf.Tenosr`
         NLL loss,
         `tf.Tensor`
         softmaxes
    """
    # convert to TF.Tensor
    logits_tensor = tf.constant(logits, name='logits_test', dtype=tf.float32)
    labels_tensor = tf.constant(labels, name='labels_test', dtype=tf.int32)

    if temperature:
        logits_tensor = tf.divide(logits_tensor, temperature)
    nll = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels_tensor, logits=logits_tensor))

    # getting the softmaxes using the model final activation function
    softmaxes = scorer.final_activation_op(logits_tensor, {})
    return accuracy(softmaxes, labels_tensor), tf.identity(nll), softmaxes
//  [python]
def calculate_model_fp(input_tensor, label_tensor):
    """
    calculate fp figure
    :param input_tensor:
    :param label_tensor:
    :return:
    """
    logits = tf.nn.softmax(logits=input_tensor)
    final_output = tf.expand_dims(tf.argmax(logits, axis=-1), axis=-1)

    idx = tf.where(tf.equal(final_output, 1))
    pix_cls_ret = tf.gather_nd(final_output, idx)
    false_pred = tf.cast(tf.shape(pix_cls_ret)[0], tf.int64) - tf.count_nonzero(
        tf.gather_nd(label_tensor, idx)
    )

    return tf.truediv(false_pred, tf.cast(tf.shape(pix_cls_ret)[0], tf.int64))
//  [python]
def calculate_model_fp(input_tensor, label_tensor):
    """
    calculate fp figure
    :param input_tensor:
    :param label_tensor:
    :return:
    """
    logits = tf.nn.softmax(logits=input_tensor)
    final_output = tf.expand_dims(tf.argmax(logits, axis=-1), axis=-1)

    idx = tf.where(tf.equal(final_output, 1))
    pix_cls_ret = tf.gather_nd(final_output, idx)
    false_pred = tf.cast(tf.shape(pix_cls_ret)[0], tf.int64) - tf.count_nonzero(
        tf.gather_nd(label_tensor, idx)
    )

    return tf.divide(false_pred, tf.cast(tf.shape(pix_cls_ret)[0], tf.int64))
//  [python]
def calculate_model_precision(input_tensor, label_tensor):
    """
    calculate accuracy acc = correct_nums / ground_truth_nums
    :param input_tensor: binary segmentation logits
    :param label_tensor: binary segmentation label
    :return:
    """

    logits = tf.nn.softmax(logits=input_tensor)
    final_output = tf.expand_dims(tf.argmax(logits, axis=-1), axis=-1)

    idx = tf.where(tf.equal(final_output, 1))
    pix_cls_ret = tf.gather_nd(label_tensor, idx)
    accuracy = tf.count_nonzero(pix_cls_ret)
    accuracy = tf.truediv(
        accuracy,
        tf.cast(tf.shape(tf.gather_nd(label_tensor, tf.where(tf.equal(label_tensor, 1))))[0], tf.int64))

    return accuracy
//  [python]
def calculate_model_precision(input_tensor, label_tensor):
    """
    calculate accuracy acc = correct_nums / ground_truth_nums
    :param input_tensor: binary segmentation logits
    :param label_tensor: binary segmentation label
    :return:
    """

    logits = tf.nn.softmax(logits=input_tensor)
    final_output = tf.expand_dims(tf.argmax(logits, axis=-1), axis=-1)

    idx = tf.where(tf.equal(final_output, 1))
    pix_cls_ret = tf.gather_nd(label_tensor, idx)
    accuracy = tf.count_nonzero(pix_cls_ret)
    accuracy = tf.divide(
        accuracy,
        tf.cast(tf.shape(tf.gather_nd(label_tensor, tf.where(tf.equal(label_tensor, 1))))[0], tf.int64))

    return accuracy
//  [python]
def calculate_model_fn(input_tensor, label_tensor):
    """
    calculate fn figure
    :param input_tensor:
    :param label_tensor:
    :return:
    """
    logits = tf.nn.softmax(logits=input_tensor)
    final_output = tf.expand_dims(tf.argmax(logits, axis=-1), axis=-1)

    idx = tf.where(tf.equal(label_tensor, 1))
    pix_cls_ret = tf.gather_nd(final_output, idx)
    label_cls_ret = tf.gather_nd(label_tensor, tf.where(tf.equal(label_tensor, 1)))
    mis_pred = tf.cast(tf.shape(label_cls_ret)[0], tf.int64) - tf.count_nonzero(pix_cls_ret)

    return tf.truediv(mis_pred, tf.cast(tf.shape(label_cls_ret)[0], tf.int64))
//  [python]
def calculate_model_fn(input_tensor, label_tensor):
    """
    calculate fn figure
    :param input_tensor:
    :param label_tensor:
    :return:
    """
    logits = tf.nn.softmax(logits=input_tensor)
    final_output = tf.expand_dims(tf.argmax(logits, axis=-1), axis=-1)

    idx = tf.where(tf.equal(label_tensor, 1))
    pix_cls_ret = tf.gather_nd(final_output, idx)
    label_cls_ret = tf.gather_nd(label_tensor, tf.where(tf.equal(label_tensor, 1)))
    mis_pred = tf.cast(tf.shape(label_cls_ret)[0], tf.int64) - tf.count_nonzero(pix_cls_ret)

    return tf.divide(mis_pred, tf.cast(tf.shape(label_cls_ret)[0], tf.int64))
//  [python]
def softmax(
    x: Tensor, /, *, axis: Optional[int] = None, out: Optional[Tensor] = None
) -> Tensor:
    if axis is None:
        axis = -1
    dtype = x.dtype
    if "complex" in str(dtype):
        amax = tf_backend.max(x, axis=axis, keepdims=True)
        normalized = tf.exp(tf.subtract(x, amax))
        return tf.truediv(
            normalized, tf.reduce_sum(normalized, axis=axis, keepdims=True)
        )
    return tf.nn.softmax(x, axis)
//  [python]
def softmax(
    x: Tensor, /, *, axis: Optional[int] = None, out: Optional[Tensor] = None
) -> Tensor:
    if axis is None:
        axis = -1
    dtype = x.dtype
    if "complex" in str(dtype):
        amax = tf_backend.max(x, axis=axis, keepdims=True)
        normalized = tf.exp(tf.subtract(x, amax))
        return tf.divide(
            normalized, tf.reduce_sum(normalized, axis=axis, keepdims=True)
        )
    return tf.nn.softmax(x, axis)
//  [python]
def histogram_loss_cal(self,source,template,source_mask,template_mask):
        shape = tf.shape(source)
        source = tf.reshape(source, [1, -1])
        template = tf.reshape(template, [1, -1])
        source_mask = tf.reshape(source_mask,[-1, 256 * 256])
        template_mask = tf.reshape(template_mask,[-1,256*256])

        source = tf.boolean_mask(source, source_mask)
        template = tf.boolean_mask(template, template_mask)

        his_bins = 255

        max_value = tf.reduce_max([tf.reduce_max(source), tf.reduce_max(template)])
        min_value = tf.reduce_min([tf.reduce_min(source), tf.reduce_min(template)])

        hist_delta = (max_value - min_value) / his_bins
        hist_range = tf.range(min_value, max_value, hist_delta)
        hist_range = tf.add(hist_range, tf.divide(hist_delta, 2))

        s_hist = tf.histogram_fixed_width(source, [min_value, max_value], his_bins, dtype=tf.int32)
        t_hist = tf.histogram_fixed_width(template, [min_value, max_value], his_bins, dtype=tf.int32)

        s_quantiles = tf.cumsum(s_hist)
        s_last_element = tf.subtract(tf.size(s_quantiles), tf.constant(1))
        s_quantiles = tf.truediv(s_quantiles, tf.gather(s_quantiles, s_last_element))

        t_quantiles = tf.cumsum(t_hist)
        t_last_element = tf.subtract(tf.size(t_quantiles), tf.constant(1))
        t_quantiles = tf.truediv(t_quantiles, tf.gather(t_quantiles, t_last_element))

        nearest_indices = tf.map_fn(lambda x: tf.argmin(tf.abs(tf.subtract(t_quantiles, x))), s_quantiles,
                                    dtype=tf.int64)
        s_bin_index = tf.to_int64(tf.truediv(source, hist_delta))
        s_bin_index = tf.clip_by_value(s_bin_index, 0, 254)

        matched_to_t = tf.gather(hist_range, tf.gather(nearest_indices, s_bin_index))
        # Using the same normalization as Gatys' style transfer: A huge variation--the normalization scalar is different according to different image
        # normalization includes variation constraints may be better
        matched_to_t = tf.subtract(tf.truediv(matched_to_t,127.5),1)
        source = tf.subtract(tf.truediv(source,127.5),1)
        return tf.reduce_mean(tf.squared_difference(matched_to_t,source))
//  [python]
def histogram_loss_cal(self,source,template,source_mask,template_mask):
        shape = tf.shape(source)
        source = tf.reshape(source, [1, -1])
        template = tf.reshape(template, [1, -1])
        source_mask = tf.reshape(source_mask,[-1, 256 * 256])
        template_mask = tf.reshape(template_mask,[-1,256*256])

        source = tf.boolean_mask(source, source_mask)
        template = tf.boolean_mask(template, template_mask)

        his_bins = 255

        max_value = tf.reduce_max([tf.reduce_max(source), tf.reduce_max(template)])
        min_value = tf.reduce_min([tf.reduce_min(source), tf.reduce_min(template)])

        hist_delta = (max_value - min_value) / his_bins
        hist_range = tf.range(min_value, max_value, hist_delta)
        hist_range = tf.add(hist_range, tf.divide(hist_delta, 2))

        s_hist = tf.histogram_fixed_width(source, [min_value, max_value], his_bins, dtype=tf.int32)
        t_hist = tf.histogram_fixed_width(template, [min_value, max_value], his_bins, dtype=tf.int32)

        s_quantiles = tf.cumsum(s_hist)
        s_last_element = tf.subtract(tf.size(s_quantiles), tf.constant(1))
        s_quantiles = tf.divide(s_quantiles, tf.gather(s_quantiles, s_last_element))

        t_quantiles = tf.cumsum(t_hist)
        t_last_element = tf.subtract(tf.size(t_quantiles), tf.constant(1))
        t_quantiles = tf.divide(t_quantiles, tf.gather(t_quantiles, t_last_element))

        nearest_indices = tf.map_fn(lambda x: tf.argmin(tf.abs(tf.subtract(t_quantiles, x))), s_quantiles,
                                    dtype=tf.int64)
        s_bin_index = tf.to_int64(tf.divide(source, hist_delta))
        s_bin_index = tf.clip_by_value(s_bin_index, 0, 254)

        matched_to_t = tf.gather(hist_range, tf.gather(nearest_indices, s_bin_index))
        # Using the same normalization as Gatys' style transfer: A huge variation--the normalization scalar is different according to different image
        # normalization includes variation constraints may be better
        matched_to_t = tf.subtract(tf.div(matched_to_t,127.5),1)
        source = tf.subtract(tf.divide(source,127.5),1)
        return tf.reduce_mean(tf.squared_difference(matched_to_t,source))
//  [python]
def _accumulate_loss(config, numerator, denominator, name_prefix=''):
    """ Constructs ops to accumulate and reduce loss and maintain a memory of lowest loss achieved """

    if config['num_evaluation_invocations'] == 1:
        # return simple loss
        accumulated_loss = tf.div(numerator, denominator, name=name_prefix)
        update_op = reduce_op = tf.no_op()
    else:
        # create accumulator variables. note that tf.Variable uses name_scope (not variable_scope) for naming, which is what's desired in this instance
        numerator_accumulator   = tf.Variable(initial_value=0., trainable=False, name=name_prefix + '_numerator_accumulator')
        denominator_accumulator = tf.Variable(initial_value=0., trainable=False, name=name_prefix + '_denominator_accumulator')

        # accumulate
        with tf.control_dependencies([numerator, denominator, numerator_accumulator, denominator_accumulator]):
            accumulate_numerator   = tf.assign_add(numerator_accumulator, numerator)
            accumulate_denominator = tf.assign_add(denominator_accumulator, denominator)
            update_op = tf.group(accumulate_numerator, accumulate_denominator, name=name_prefix + '_accumulate_op')

        # divide to get final quotient
        with tf.control_dependencies([update_op]):
            accumulated_loss = tf.div(numerator_accumulator, denominator_accumulator, name=name_prefix + '_accumulated')

        # zero accumulators
        with tf.control_dependencies([accumulated_loss]):
            zero_numerator   = tf.assign(numerator_accumulator,   0.)
            zero_denominator = tf.assign(denominator_accumulator, 0.)
            reduce_op = tf.group(zero_numerator, zero_denominator, name=name_prefix + '_reduce_op')

    min_loss_achieved = tf.Variable(initial_value=float('inf'), trainable=False, name='min_' + name_prefix + '_achieved')
    min_loss_op = tf.assign(min_loss_achieved, tf.reduce_min([min_loss_achieved, accumulated_loss]), name='min_' + name_prefix + '_achieved_op')
    with tf.control_dependencies([min_loss_op]):
        min_loss_achieved = tf.identity(min_loss_achieved)

    return accumulated_loss, min_loss_achieved, min_loss_op, update_op, reduce_op
//  [python]
def _accumulate_loss(config, numerator, denominator, name_prefix=''):
    """ Constructs ops to accumulate and reduce loss and maintain a memory of lowest loss achieved """

    if config['num_evaluation_invocations'] == 1:
        # return simple loss
        accumulated_loss = tf.divide(numerator, denominator, name=name_prefix)
        update_op = reduce_op = tf.no_op()
    else:
        # create accumulator variables. note that tf.Variable uses name_scope (not variable_scope) for naming, which is what's desired in this instance
        numerator_accumulator   = tf.Variable(initial_value=0., trainable=False, name=name_prefix + '_numerator_accumulator')
        denominator_accumulator = tf.Variable(initial_value=0., trainable=False, name=name_prefix + '_denominator_accumulator')

        # accumulate
        with tf.control_dependencies([numerator, denominator, numerator_accumulator, denominator_accumulator]):
            accumulate_numerator   = tf.assign_add(numerator_accumulator, numerator)
            accumulate_denominator = tf.assign_add(denominator_accumulator, denominator)
            update_op = tf.group(accumulate_numerator, accumulate_denominator, name=name_prefix + '_accumulate_op')

        # divide to get final quotient
        with tf.control_dependencies([update_op]):
            accumulated_loss = tf.divide(numerator_accumulator, denominator_accumulator, name=name_prefix + '_accumulated')

        # zero accumulators
        with tf.control_dependencies([accumulated_loss]):
            zero_numerator   = tf.assign(numerator_accumulator,   0.)
            zero_denominator = tf.assign(denominator_accumulator, 0.)
            reduce_op = tf.group(zero_numerator, zero_denominator, name=name_prefix + '_reduce_op')

    min_loss_achieved = tf.Variable(initial_value=float('inf'), trainable=False, name='min_' + name_prefix + '_achieved')
    min_loss_op = tf.assign(min_loss_achieved, tf.reduce_min([min_loss_achieved, accumulated_loss]), name='min_' + name_prefix + '_achieved_op')
    with tf.control_dependencies([min_loss_op]):
        min_loss_achieved = tf.identity(min_loss_achieved)

    return accumulated_loss, min_loss_achieved, min_loss_op, update_op, reduce_op
//  [python]
def update(
      self,
      total_loss,
      positive_loss,
      negative_loss,
      positive_logits,
      negative_logits,
  ):
    """Update the huge-tpu metrics.

    Args:
      total_loss: A tf.Tensor containing the total model loss.
      positive_loss: A tf.Tensor containing the positive portion of the loss.
      negative_loss: A tf.Tensor containing the negative portion of the loss.
      positive_logits: Model output corresponding to positive examples.
      negative_logits: Model output corresponding to negative examples.
    """
    # Approximate in-batch confusion matrix.
    tp = tf.math.count_nonzero(positive_logits > 0.5, dtype=tf.float32)
    fn = tf.math.count_nonzero(positive_logits <= 0.5, dtype=tf.float32)
    tn = tf.math.count_nonzero(negative_logits <= 0.5, dtype=tf.float32)
    fp = tf.math.count_nonzero(negative_logits > 0.5, dtype=tf.float32)

    # There are many more negatives than positives: scale the negatives s.t.
    # accuracy and recall are more intelligible.
    if self.num_negs_per_pos:
      tn = tn / self.num_negs_per_pos
      fp = fp / self.num_negs_per_pos

    accuracy = tf.truediv(tf.add(tp, tn), tf.add(tp, tf.add(tn, tf.add(fp, fn))))
    precision = tf.truediv(tp, tf.add(tp, fp))
    recall = tf.truediv(tp, tf.add(tp, fn))
    f1 = tf.truediv(
        tf.multiply(tp, 2), tf.add(tf.multiply(tp, 2), tf.add(fp, fn))
    )

    self.metrics["total_loss"].update_state(total_loss)
    self.metrics["positive_loss"].update_state(positive_loss)
    self.metrics["negative_loss"].update_state(negative_loss)
    self.metrics["tp"].update_state(tp)
    self.metrics["fn"].update_state(fn)
    self.metrics["tn"].update_state(tn)
    self.metrics["fp"].update_state(fp)
    self.metrics["accuracy"].update_state(accuracy)
    self.metrics["precision"].update_state(precision)
    self.metrics["recall"].update_state(recall)
    self.metrics["f1"].update_state(f1)
//  [python]
def update(
      self,
      total_loss,
      positive_loss,
      negative_loss,
      positive_logits,
      negative_logits,
  ):
    """Update the huge-tpu metrics.

    Args:
      total_loss: A tf.Tensor containing the total model loss.
      positive_loss: A tf.Tensor containing the positive portion of the loss.
      negative_loss: A tf.Tensor containing the negative portion of the loss.
      positive_logits: Model output corresponding to positive examples.
      negative_logits: Model output corresponding to negative examples.
    """
    # Approximate in-batch confusion matrix.
    tp = tf.math.count_nonzero(positive_logits > 0.5, dtype=tf.float32)
    fn = tf.math.count_nonzero(positive_logits <= 0.5, dtype=tf.float32)
    tn = tf.math.count_nonzero(negative_logits <= 0.5, dtype=tf.float32)
    fp = tf.math.count_nonzero(negative_logits > 0.5, dtype=tf.float32)

    # There are many more negatives than positives: scale the negatives s.t.
    # accuracy and recall are more intelligible.
    if self.num_negs_per_pos:
      tn = tn / self.num_negs_per_pos
      fp = fp / self.num_negs_per_pos

    accuracy = tf.divide(tf.add(tp, tn), tf.add(tp, tf.add(tn, tf.add(fp, fn))))
    precision = tf.divide(tp, tf.add(tp, fp))
    recall = tf.divide(tp, tf.add(tp, fn))
    f1 = tf.divide(
        tf.multiply(tp, 2), tf.add(tf.multiply(tp, 2), tf.add(fp, fn))
    )

    self.metrics["total_loss"].update_state(total_loss)
    self.metrics["positive_loss"].update_state(positive_loss)
    self.metrics["negative_loss"].update_state(negative_loss)
    self.metrics["tp"].update_state(tp)
    self.metrics["fn"].update_state(fn)
    self.metrics["tn"].update_state(tn)
    self.metrics["fp"].update_state(fp)
    self.metrics["accuracy"].update_state(accuracy)
    self.metrics["precision"].update_state(precision)
    self.metrics["recall"].update_state(recall)
    self.metrics["f1"].update_state(f1)
//  [python]
def __call__(self, net, n_batch_placeholder, dtype="output", pruner=None):
        name = "DNDF_{}".format(dtype)
        with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
            flat_probabilities = self.build_tree_projection(dtype, net, pruner)
            routes = self.build_routes(flat_probabilities, n_batch_placeholder)
            features = tf.concat(routes, 1, name="Feature_Concat")
            if dtype == "feature":
                return features
            leafs = self.build_leafs()
            leafs_matrix = tf.concat(leafs, 0, name="Prob_Concat")
            return tf.truediv(
                tf.matmul(features, leafs_matrix),
                float(self.n_tree), name=name
            )
//  [python]
def __call__(self, net, n_batch_placeholder, dtype="output", pruner=None):
        name = "DNDF_{}".format(dtype)
        with tf.variable_scope(name, reuse=tf.AUTO_REUSE):
            flat_probabilities = self.build_tree_projection(dtype, net, pruner)
            routes = self.build_routes(flat_probabilities, n_batch_placeholder)
            features = tf.concat(routes, 1, name="Feature_Concat")
            if dtype == "feature":
                return features
            leafs = self.build_leafs()
            leafs_matrix = tf.concat(leafs, 0, name="Prob_Concat")
            return tf.divide(
                tf.matmul(features, leafs_matrix),
                float(self.n_tree), name=name
            )
//  [python]
def cosine(self, v1, v2):
        m = tf.matmul(v1, tf.transpose(v2, [0, 2, 1]))
        n = tf.norm(v1, axis=2, keep_dims=True) * tf.norm(v2, axis=2, keep_dims=True)
        # cosine = m / n
        cosine = tf.truediv(m, n)
        return cosine
//  [python]
def cosine(self, v1, v2):
        m = tf.matmul(v1, tf.transpose(v2, [0, 2, 1]))
        n = tf.norm(v1, axis=2, keep_dims=True) * tf.norm(v2, axis=2, keep_dims=True)
        # cosine = m / n
        cosine = tf.divide(m, n)
        return cosine
//  [python]
def tv_loss(input_, output, weight):
    I = tf.image.rgb_to_grayscale(input_)
    L = tf.log(I+0.0001)
    dx = L[:, :-1, :-1, :] - L[:, :-1, 1:, :]
    dy = L[:, :-1, :-1, :] - L[:, 1:, :-1, :]

    alpha = tf.constant(1.2)
    lamda = tf.constant(1.5)  
    dx = tf.truediv(lamda, tf.pow(tf.abs(dx),alpha)+ tf.constant(0.0001))
    dy = tf.truediv(lamda, tf.pow(tf.abs(dy),alpha)+ tf.constant(0.0001))
    shape = output.get_shape()
    x_loss = dx *((output[:, :-1, :-1, :] - output[:, :-1, 1:, :])**2)
    y_loss = dy *((output[:, :-1, :-1, :] - output[:, 1:, :-1, :])**2)
    tvloss = tf.reduce_mean(x_loss + y_loss)/2.0
    return tvloss*weight
//  [python]
def tv_loss(input_, output, weight):
    I = tf.image.rgb_to_grayscale(input_)
    L = tf.log(I+0.0001)
    dx = L[:, :-1, :-1, :] - L[:, :-1, 1:, :]
    dy = L[:, :-1, :-1, :] - L[:, 1:, :-1, :]

    alpha = tf.constant(1.2)
    lamda = tf.constant(1.5)  
    dx = tf.divide(lamda, tf.pow(tf.abs(dx),alpha)+ tf.constant(0.0001))
    dy = tf.divide(lamda, tf.pow(tf.abs(dy),alpha)+ tf.constant(0.0001))
    shape = output.get_shape()
    x_loss = dx *((output[:, :-1, :-1, :] - output[:, :-1, 1:, :])**2)
    y_loss = dy *((output[:, :-1, :-1, :] - output[:, 1:, :-1, :])**2)
    tvloss = tf.reduce_mean(x_loss + y_loss)/2.0
    return tvloss*weight
//  [python]
def full_matching(self, metric, vec, w):
        w = tf.expand_dims(tf.expand_dims(w, 0), 2)
        metric = w * tf.stack([metric] * args.num_perspective, axis=1)
        vec = w * tf.stack([vec] * args.num_perspective, axis=1)

        m = tf.matmul(metric, tf.transpose(vec, [0, 1, 3, 2]))
        n = tf.norm(metric, axis=3, keep_dims=True) * tf.norm(vec, axis=3, keep_dims=True)
        cosine = tf.transpose(tf.truediv(m, n), [0, 2, 3, 1])

        return cosine
//  [python]
def full_matching(self, metric, vec, w):
        w = tf.expand_dims(tf.expand_dims(w, 0), 2)
        metric = w * tf.stack([metric] * args.num_perspective, axis=1)
        vec = w * tf.stack([vec] * args.num_perspective, axis=1)

        m = tf.matmul(metric, tf.transpose(vec, [0, 1, 3, 2]))
        n = tf.norm(metric, axis=3, keep_dims=True) * tf.norm(vec, axis=3, keep_dims=True)
        cosine = tf.transpose(tf.divide(m, n), [0, 2, 3, 1])

        return cosine
//  [python]
def create_discrete_action_masking_layer(all_logits, action_masks, action_size):
        """
        Creates a masking layer for the discrete actions
        :param all_logits: The concatenated unnormalized action probabilities for all branches
        :param action_masks: The mask for the logits. Must be of dimension [None x total_number_of_action]
        :param action_size: A list containing the number of possible actions for each branch
        :return: The action output dimension [batch_size, num_branches] and the concatenated normalized logits
        """
        action_idx = [0] + list(np.cumsum(action_size))
        branches_logits = [all_logits[:, action_idx[i]:action_idx[i + 1]] for i in range(len(action_size))]
        branch_masks = [action_masks[:, action_idx[i]:action_idx[i + 1]] for i in range(len(action_size))]
        raw_probs = [tf.multiply(tf.nn.softmax(branches_logits[k]), branch_masks[k]) + 1.0e-10
                     for k in range(len(action_size))]
        normalized_probs = [
            tf.truediv(raw_probs[k], tf.reduce_sum(raw_probs[k] + 1.0e-10, axis=1, keepdims=True))
                            for k in range(len(action_size))]
        output = tf.concat([tf.multinomial(tf.log(normalized_probs[k]), 1) for k in range(len(action_size))], axis=1)
        return output, tf.concat([tf.log(normalized_probs[k]) for k in range(len(action_size))], axis=1)
//  [python]
def create_discrete_action_masking_layer(all_logits, action_masks, action_size):
        """
        Creates a masking layer for the discrete actions
        :param all_logits: The concatenated unnormalized action probabilities for all branches
        :param action_masks: The mask for the logits. Must be of dimension [None x total_number_of_action]
        :param action_size: A list containing the number of possible actions for each branch
        :return: The action output dimension [batch_size, num_branches] and the concatenated normalized logits
        """
        action_idx = [0] + list(np.cumsum(action_size))
        branches_logits = [all_logits[:, action_idx[i]:action_idx[i + 1]] for i in range(len(action_size))]
        branch_masks = [action_masks[:, action_idx[i]:action_idx[i + 1]] for i in range(len(action_size))]
        raw_probs = [tf.multiply(tf.nn.softmax(branches_logits[k]), branch_masks[k]) + 1.0e-10
                     for k in range(len(action_size))]
        normalized_probs = [
            tf.divide(raw_probs[k], tf.reduce_sum(raw_probs[k] + 1.0e-10, axis=1, keepdims=True))
                            for k in range(len(action_size))]
        output = tf.concat([tf.multinomial(tf.log(normalized_probs[k]), 1) for k in range(len(action_size))], axis=1)
        return output, tf.concat([tf.log(normalized_probs[k]) for k in range(len(action_size))], axis=1)
//  [python]
def normalize_vgg(self, input, name):
        # normalize input -0.5 ~ 0.5
        input = tf.truediv(input, 256.0, name=name + '_divide')
        input = tf.subtract(input, 0.5, name=name + '_subtract')
        return input
//  [python]
def normalize_vgg(self, input, name):
        # normalize input -0.5 ~ 0.5
        input = tf.divide(input, 256.0, name=name + '_divide')
        input = tf.subtract(input, 0.5, name=name + '_subtract')
        return input
//  [python]
def gen_prior(self, gen_params):
        with tf.variable_scope("generator") as scope:
            prior_loss = 0.0
            for var in gen_params.values():
                nn = tf.div(var, self.prior_std)
                prior_loss += tf.reduce_mean(tf.multiply(nn, nn))
                
        prior_loss /= self.dataset_size

        return prior_loss
//  [python]
def gen_prior(self, gen_params):
        with tf.variable_scope("generator") as scope:
            prior_loss = 0.0
            for var in gen_params.values():
                nn = tf.divide(var, self.prior_std)
                prior_loss += tf.reduce_mean(tf.multiply(nn, nn))
                
        prior_loss /= self.dataset_size

        return prior_loss
//  [python]
def disc_prior(self):
        with tf.variable_scope("discriminator") as scope:
            prior_loss = 0.0
            for var in self.d_vars:
                nn = tf.div(var, self.prior_std)
                prior_loss += tf.reduce_mean(tf.multiply(nn, nn))
                
        prior_loss /= self.dataset_size

        return prior_loss
//  [python]
def disc_prior(self):
        with tf.variable_scope("discriminator") as scope:
            prior_loss = 0.0
            for var in self.d_vars:
                nn = tf.divide(var, self.prior_std)
                prior_loss += tf.reduce_mean(tf.multiply(nn, nn))
                
        prior_loss /= self.dataset_size

        return prior_loss