jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def create_div_model(self):
          w1 = tf.placeholder(tf.float32, name='w1')
          w2 = tf.placeholder(tf.float32, name='w2')
          feed_dict ={'w1': 8.0, 'w2': 2.0}
          r =tf.divide(w1, w2, name='op_div')
          sess = tf.Session()
          _ = tf.Variable(initial_value = 'fake_variable')
          sess.run(tf.global_variables_initializer())
          saver = tf.train.Saver()
          print(f"feed_dict['w1'] : {feed_dict['w1']}")
          print(f"feed_dict['w2'] : {feed_dict['w2']}")
          result = sess.run(r, {w1: feed_dict['w1'], w2: feed_dict['w2']})
          print(f'TF 나눗셈결과: {result}')
          saver.save(sess, os.path.join(self.model,'calculator_div', 'model'),global_step=1000)
//  [python]
def _compute_gram(self, feature):
        # https://github.com/fullfanta/real_time_style_transfer/blob/master/train.py
        shape = tf.shape(feature)
        psi = tf.reshape(feature, [shape[0], shape[1] * shape[2], shape[3]])
        # psi_t = tf.transpose(psi, perm=[0, 2, 1])
        gram = tf.matmul(psi, psi, transpose_a=True)
        gram = tf.div(gram, tf.cast(shape[1] * shape[2] * shape[3], tf.float32))
        return gram
//  [python]
def _compute_gram(self, feature):
        shape = tf.shape(feature)
        psi = tf.reshape(feature, [shape[0], shape[1] * shape[2], shape[3]])
        # psi_t = tf.transpose(psi, perm=[0, 2, 1])
        gram = tf.matmul(psi, psi, transpose_a=True)
        gram = tf.divide(gram, tf.cast(shape[1] * shape[2] * shape[3], tf.float32))
        return gram
//  [python]
def _gen_norm_warmup(clip_norm: float, global_step_var: tf.Tensor,
                     warmup_step: int):
  if not warmup_step:
    return clip_norm
  return tf.cond(
      tf.less(global_step_var, warmup_step), lambda: tf.compat.v1.div(
          tf.cast(global_step_var, dtype=tf.float32), float(warmup_step)),
      lambda: 1.0) * clip_norm
//  [python]
def _gen_norm_warmup(clip_norm: float, global_step_var: tf.Tensor,
                     warmup_step: int):
  if not warmup_step:
    return clip_norm
  return tf.cond(
      tf.less(global_step_var, warmup_step), lambda: tf.divide(
          tf.cast(global_step_var, dtype=tf.float32), float(warmup_step)),
      lambda: 1.0) * clip_norm
//  [python]
def zmean_loss2(valid_z, pre_logits, one_hot_labels, num_classes):
        valid_z = tf.reshape(valid_z, [-1,1])#[BN, 1]
        valid_zrgb_T = tf.transpose(valid_z, perm=[1, 0])#[num_channel, B*N]

        pre_label = tf.argmax (pre_logits, axis = 1)
        pre_one_hot = tf.one_hot(pre_label, depth=num_classes)
        n1 = tf.reduce_sum( pre_one_hot, axis=0, keepdims=True)  #[1, 13]
        n2 = tf.reduce_sum(one_hot_labels, axis=0, keepdims=True)

        t1 = tf.matmul(valid_zrgb_T, pre_one_hot)  # [num_channel, B*N] *  [B*N, 13] = [num_channel, 13]
        t2 = tf.matmul(valid_zrgb_T, one_hot_labels)  # [num_channel, B*N] *  [B*N, 13]= [num_channel, 13]

        t1 = tf.div( t1, n1+(1e-2))[0: num_classes-1]# [num_channel, 13] /  [1, 13] class-wise
        t2 = tf.div( t2, n2+(1e-2))[0: num_classes-1]

        loss = tf.reduce_mean(tf.square(t1-t2))#/num_classes
        # loss_num = tf.reduce_mean(tf.square(n1[0: num_classes-1] - n2[0: num_classes-1]))  # /num_classes
        t12 = tf.concat((t1,t2), axis=0)
        return loss
//  [python]
def zmean_loss2(valid_z, pre_logits, one_hot_labels, num_classes):
        valid_z = tf.reshape(valid_z, [-1,1])#[BN, 1]
        valid_zrgb_T = tf.transpose(valid_z, perm=[1, 0])#[num_channel, B*N]

        pre_label = tf.argmax (pre_logits, axis = 1)
        pre_one_hot = tf.one_hot(pre_label, depth=num_classes)
        n1 = tf.reduce_sum( pre_one_hot, axis=0, keepdims=True)  #[1, 13]
        n2 = tf.reduce_sum(one_hot_labels, axis=0, keepdims=True)

        t1 = tf.matmul(valid_zrgb_T, pre_one_hot)  # [num_channel, B*N] *  [B*N, 13] = [num_channel, 13]
        t2 = tf.matmul(valid_zrgb_T, one_hot_labels)  # [num_channel, B*N] *  [B*N, 13]= [num_channel, 13]

        t1 = tf.divide( t1, n1+(1e-2))[0: num_classes-1]# [num_channel, 13] /  [1, 13] class-wise
        t2 = tf.divide( t2, n2+(1e-2))[0: num_classes-1]

        loss = tf.reduce_mean(tf.square(t1-t2))#/num_classes
        # loss_num = tf.reduce_mean(tf.square(n1[0: num_classes-1] - n2[0: num_classes-1]))  # /num_classes
        t12 = tf.concat((t1,t2), axis=0)
        return loss
//  [python]
def get_loss(self):
    factor = tf.div(1.0, tf.multiply(2.0, self._sigmas_sq[0]))
    loss = tf.add(tf.multiply(factor, self._loss_list[0]), tf.log(self._sigmas_sq[0]))
    for i in range(1, len(self._sigmas_sq)):
      factor = tf.div(1.0, tf.multiply(2.0, self._sigmas_sq[i]))
      loss = tf.add(loss, tf.add(tf.multiply(factor, self._loss_list[i]), tf.log(self._sigmas_sq[i])))
    return loss
//  [python]
def get_loss(self):
    factor = tf.divide(1.0, tf.multiply(2.0, self._sigmas_sq[0]))
    loss = tf.add(tf.multiply(factor, self._loss_list[0]), tf.log(self._sigmas_sq[0]))
    for i in range(1, len(self._sigmas_sq)):
      factor = tf.divide(1.0, tf.multiply(2.0, self._sigmas_sq[i]))
      loss = tf.add(loss, tf.add(tf.multiply(factor, self._loss_list[i]), tf.log(self._sigmas_sq[i])))
    return loss
//  [python]
def _address_content(self, x):
    """Address the memory based on content similarity.

    Args:
      x: a tensor in the shape of [batch_size, length, depth].
    Returns:
      the logits for each memory entry [batch_size, length, memory_size].
    """
    mem_keys = tf.layers.dense(self.mem_vals, self.key_depth,
                               bias_initializer=tf.constant_initializer(1.0),
                               name="mem_key")
    mem_query = tf.layers.dense(x, self.key_depth,
                                bias_initializer=tf.constant_initializer(1.0),
                                name="mem_query")
    norm = tf.matmul(self._norm(mem_query), self._norm(mem_keys),
                     transpose_b=True)
    dot_product = tf.matmul(mem_query, mem_keys, transpose_b=True)
    cos_dist = tf.div(dot_product, norm + 1e-7, name="cos_dist")
    access_logits = self.sharpen_factor * cos_dist
    return access_logits
//  [python]
def _address_content(self, x):
    """Address the memory based on content similarity.

    Args:
      x: a tensor in the shape of [batch_size, length, depth].
    Returns:
      the logits for each memory entry [batch_size, length, memory_size].
    """
    mem_keys = tf.layers.dense(self.mem_vals, self.key_depth,
                               bias_initializer=tf.constant_initializer(1.0),
                               name="mem_key")
    mem_query = tf.layers.dense(x, self.key_depth,
                                bias_initializer=tf.constant_initializer(1.0),
                                name="mem_query")
    norm = tf.matmul(self._norm(mem_query), self._norm(mem_keys),
                     transpose_b=True)
    dot_product = tf.matmul(mem_query, mem_keys, transpose_b=True)
    cos_dist = tf.divide(dot_product, norm + 1e-7, name="cos_dist")
    access_logits = self.sharpen_factor * cos_dist
    return access_logits
//  [python]
def _accuracy(self):
        # shape: [batch_size]
        aff = self.link_pred_layer.affinity(self.outputs1, self.outputs2)
        # shape : [batch_size x num_neg_samples]
        self.neg_aff = self.link_pred_layer.neg_cost(self.outputs1, self.neg_outputs)
        self.neg_aff = tf.reshape(self.neg_aff, [self.batch_size, FLAGS.neg_sample_size])
        _aff = tf.expand_dims(aff, axis=1)
        self.aff_all = tf.concat(axis=1, values=[self.neg_aff, _aff])
        size = tf.shape(self.aff_all)[1]
        _, indices_of_ranks = tf.nn.top_k(self.aff_all, k=size)
        _, self.ranks = tf.nn.top_k(-indices_of_ranks, k=size)
        self.mrr = tf.reduce_mean(tf.div(1.0, tf.cast(self.ranks[:, -1] + 1, tf.float32)))
        tf.summary.scalar('mrr', self.mrr)
//  [python]
def _accuracy(self):
        # shape: [batch_size]
        aff = self.link_pred_layer.affinity(self.outputs1, self.outputs2)
        # shape : [batch_size x num_neg_samples]
        self.neg_aff = self.link_pred_layer.neg_cost(self.outputs1, self.neg_outputs)
        self.neg_aff = tf.reshape(self.neg_aff, [self.batch_size, FLAGS.neg_sample_size])
        _aff = tf.expand_dims(aff, axis=1)
        self.aff_all = tf.concat(axis=1, values=[self.neg_aff, _aff])
        size = tf.shape(self.aff_all)[1]
        _, indices_of_ranks = tf.nn.top_k(self.aff_all, k=size)
        _, self.ranks = tf.nn.top_k(-indices_of_ranks, k=size)
        self.mrr = tf.reduce_mean(tf.divide(1.0, tf.cast(self.ranks[:, -1] + 1, tf.float32)))
        tf.summary.scalar('mrr', self.mrr)
//  [python]
def _build_optim(self):
    losses = tf.nn.sparse_softmax_cross_entropy_with_logits(
        labels=self.dec_targets, logits=self.dec_pred_logits)
    inference_losses = tf.nn.sparse_softmax_cross_entropy_with_logits(
        labels=self.dec_targets, logits=self.dec_inference_logits)

    def apply_mask(op):
      length = tf.cast(op[:1], tf.int32)
      loss = op[1:]
      return tf.multiply(loss, tf.ones(length, dtype=tf.float32))

    batch_loss = tf.div(
        tf.reduce_sum(tf.multiply(losses, self.mask)),
        tf.reduce_sum(self.mask), name="batch_loss")

    batch_inference_loss = tf.div(
        tf.reduce_sum(tf.multiply(losses, self.mask)),
        tf.reduce_sum(self.mask), name="batch_inference_loss")

    tf.losses.add_loss(batch_loss)
    total_loss = tf.losses.get_total_loss()

    self.total_loss = total_loss
    self.target_cross_entropy_losses = losses
    self.total_inference_loss = batch_inference_loss

    self.lr = tf.train.exponential_decay(
        self.lr_start, self.global_step, self.lr_decay_step,
        self.lr_decay_rate, staircase=True, name="learning_rate")

    optimizer = tf.train.AdamOptimizer(self.lr)

    if self.max_grad_norm != None:
      grads_and_vars = optimizer.compute_gradients(self.total_loss)
      for idx, (grad, var) in enumerate(grads_and_vars):
        if grad is not None:
          grads_and_vars[idx] = (tf.clip_by_norm(grad, self.max_grad_norm), var)
      self.optim = optimizer.apply_gradients(grads_and_vars, global_step=self.global_step)
    else:
      self.optim = optimizer.minimize(self.total_loss, global_step=self.global_step)
//  [python]
def _build_optim(self):
    losses = tf.nn.sparse_softmax_cross_entropy_with_logits(
        labels=self.dec_targets, logits=self.dec_pred_logits)
    inference_losses = tf.nn.sparse_softmax_cross_entropy_with_logits(
        labels=self.dec_targets, logits=self.dec_inference_logits)

    def apply_mask(op):
      length = tf.cast(op[:1], tf.int32)
      loss = op[1:]
      return tf.multiply(loss, tf.ones(length, dtype=tf.float32))

    batch_loss = tf.divide(
        tf.reduce_sum(tf.multiply(losses, self.mask)),
        tf.reduce_sum(self.mask), name="batch_loss")

    batch_inference_loss = tf.divide(
        tf.reduce_sum(tf.multiply(losses, self.mask)),
        tf.reduce_sum(self.mask), name="batch_inference_loss")

    tf.losses.add_loss(batch_loss)
    total_loss = tf.losses.get_total_loss()

    self.total_loss = total_loss
    self.target_cross_entropy_losses = losses
    self.total_inference_loss = batch_inference_loss

    self.lr = tf.train.exponential_decay(
        self.lr_start, self.global_step, self.lr_decay_step,
        self.lr_decay_rate, staircase=True, name="learning_rate")

    optimizer = tf.train.AdamOptimizer(self.lr)

    if self.max_grad_norm != None:
      grads_and_vars = optimizer.compute_gradients(self.total_loss)
      for idx, (grad, var) in enumerate(grads_and_vars):
        if grad is not None:
          grads_and_vars[idx] = (tf.clip_by_norm(grad, self.max_grad_norm), var)
      self.optim = optimizer.apply_gradients(grads_and_vars, global_step=self.global_step)
    else:
      self.optim = optimizer.minimize(self.total_loss, global_step=self.global_step)
//  [python]
def atari_network(num_actions, num_atoms, support, network_type, state,
                  representation_layer=10):
  """The convolutional network used to compute agent's Q-value distributions.

  Args:
    num_actions: int, number of actions.
    num_atoms: int, the number of buckets of the value function distribution.
    support: tf.linspace, the support of the Q-value distribution.
    network_type: namedtuple, collection of expected values to return.
    state: `tf.Tensor`, contains the agent's current state.
    representation_layer: int, the layer which will be used as the
      representation for computing the bisimulation distances. Defaults to
      a high value, which defaults to the penultimate layer.

  Returns:
    net: _network_type object containing the tensors output by the network.
  """
  weights_initializer = contrib_slim.variance_scaling_initializer(
      factor=1.0 / np.sqrt(3.0), mode='FAN_IN', uniform=True)

  curr_layer = 1
  net = tf.cast(state, tf.float32)
  net = tf.div(net, 255.)
  representation = None
  if representation_layer <= curr_layer:
    representation = contrib_slim.flatten(net)
  net = contrib_slim.conv2d(
      net,
      32, [8, 8],
      stride=4,
      weights_initializer=weights_initializer,
      trainable=False)
  curr_layer += 1
  if representation is None and representation_layer <= curr_layer:
    representation = contrib_slim.flatten(net)
  net = contrib_slim.conv2d(
      net,
      64, [4, 4],
      stride=2,
      weights_initializer=weights_initializer,
      trainable=False)
  curr_layer += 1
  if representation is None and representation_layer <= curr_layer:
    representation = contrib_slim.flatten(net)
  net = contrib_slim.conv2d(
      net,
      64, [3, 3],
      stride=1,
      weights_initializer=weights_initializer,
      trainable=False)
  net = contrib_slim.flatten(net)
  curr_layer += 1
  if representation is None and representation_layer <= curr_layer:
    representation = net
  net = contrib_slim.fully_connected(
      net, 512, weights_initializer=weights_initializer, trainable=False)
  curr_layer += 1
  if representation is None:
    representation = net
  net = contrib_slim.fully_connected(
      net,
      num_actions * num_atoms,
      activation_fn=None,
      weights_initializer=weights_initializer,
      trainable=False)

  logits = tf.reshape(net, [-1, num_actions, num_atoms])
  probabilities = contrib_layers.softmax(logits)
  q_values = tf.reduce_sum(support * probabilities, axis=2)
  return network_type(q_values, logits, probabilities, representation)
//  [python]
def atari_network(num_actions, num_atoms, support, network_type, state,
                  representation_layer=10):
  """The convolutional network used to compute agent's Q-value distributions.

  Args:
    num_actions: int, number of actions.
    num_atoms: int, the number of buckets of the value function distribution.
    support: tf.linspace, the support of the Q-value distribution.
    network_type: namedtuple, collection of expected values to return.
    state: `tf.Tensor`, contains the agent's current state.
    representation_layer: int, the layer which will be used as the
      representation for computing the bisimulation distances. Defaults to
      a high value, which defaults to the penultimate layer.

  Returns:
    net: _network_type object containing the tensors output by the network.
  """
  weights_initializer = contrib_slim.variance_scaling_initializer(
      factor=1.0 / np.sqrt(3.0), mode='FAN_IN', uniform=True)

  curr_layer = 1
  net = tf.cast(state, tf.float32)
  net = tf.divide(net, 255.)
  representation = None
  if representation_layer <= curr_layer:
    representation = contrib_slim.flatten(net)
  net = contrib_slim.conv2d(
      net,
      32, [8, 8],
      stride=4,
      weights_initializer=weights_initializer,
      trainable=False)
  curr_layer += 1
  if representation is None and representation_layer <= curr_layer:
    representation = contrib_slim.flatten(net)
  net = contrib_slim.conv2d(
      net,
      64, [4, 4],
      stride=2,
      weights_initializer=weights_initializer,
      trainable=False)
  curr_layer += 1
  if representation is None and representation_layer <= curr_layer:
    representation = contrib_slim.flatten(net)
  net = contrib_slim.conv2d(
      net,
      64, [3, 3],
      stride=1,
      weights_initializer=weights_initializer,
      trainable=False)
  net = contrib_slim.flatten(net)
  curr_layer += 1
  if representation is None and representation_layer <= curr_layer:
    representation = net
  net = contrib_slim.fully_connected(
      net, 512, weights_initializer=weights_initializer, trainable=False)
  curr_layer += 1
  if representation is None:
    representation = net
  net = contrib_slim.fully_connected(
      net,
      num_actions * num_atoms,
      activation_fn=None,
      weights_initializer=weights_initializer,
      trainable=False)

  logits = tf.reshape(net, [-1, num_actions, num_atoms])
  probabilities = contrib_layers.softmax(logits)
  q_values = tf.reduce_sum(support * probabilities, axis=2)
  return network_type(q_values, logits, probabilities, representation)
//  [python]
def _accuracy(self):
        # shape: [batch_size]
        aff = self.link_pred_layer.affinity(self.outputs1, self.outputs2)
       # shape : [batch_size x num_neg_samples]
        self.neg_aff = self.link_pred_layer.neg_cost(self.outputs1, self.neg_outputs)
        self.neg_aff = tf.reshape(self.neg_aff, [self.batch_size, FLAGS.neg_sample_size])
        _aff = tf.expand_dims(aff, axis=1)
        self.aff_all = tf.concat(axis=1, values=[self.neg_aff, _aff])
        size = tf.shape(self.aff_all)[1]
        _, indices_of_ranks = tf.nn.top_k(self.aff_all, k=size)
        _, self.ranks = tf.nn.top_k(-indices_of_ranks, k=size)
        self.mrr = tf.reduce_mean(tf.div(1.0, tf.cast(self.ranks[:, -1] + 1, tf.float32)))
        tf.summary.scalar('mrr', self.mrr)
//  [python]
def _accuracy(self):
        # shape: [batch_size]
        aff = self.link_pred_layer.affinity(self.outputs1, self.outputs2)
       # shape : [batch_size x num_neg_samples]
        self.neg_aff = self.link_pred_layer.neg_cost(self.outputs1, self.neg_outputs)
        self.neg_aff = tf.reshape(self.neg_aff, [self.batch_size, FLAGS.neg_sample_size])
        _aff = tf.expand_dims(aff, axis=1)
        self.aff_all = tf.concat(axis=1, values=[self.neg_aff, _aff])
        size = tf.shape(self.aff_all)[1]
        _, indices_of_ranks = tf.nn.top_k(self.aff_all, k=size)
        _, self.ranks = tf.nn.top_k(-indices_of_ranks, k=size)
        self.mrr = tf.reduce_mean(tf.divide(1.0, tf.cast(self.ranks[:, -1] + 1, tf.float32)))
        tf.summary.scalar('mrr', self.mrr)
//  [python]
def create_div_model(pb_file_path, x1_size, x2_size):
    ''' Create tensorflow model for div op.
    '''
    with tf.Session(graph=tf.Graph()) as sess:
        x1 = tf.placeholder(tf.float32, shape=x1_size, name='X1')
        x2 = tf.placeholder(tf.float32, shape=x2_size, name='X2')
        op1 = tf.raw_ops.Div(x=x1, y=x2, name='div')
        y = tf.div(op1, 10.0, name='Y')

        sess.run(tf.global_variables_initializer())
        constant_graph = tf.graph_util.convert_variables_to_constants(
            sess, sess.graph_def, ['Y'])

        # save to pb file
        with tf.gfile.GFile(pb_file_path, mode='wb') as f:
            f.write(constant_graph.SerializeToString())
//  [python]
def create_div_model(pb_file_path, x1_size, x2_size):
    ''' Create tensorflow model for div op.
    '''
    with tf.Session(graph=tf.Graph()) as sess:
        x1 = tf.placeholder(tf.float32, shape=x1_size, name='X1')
        x2 = tf.placeholder(tf.float32, shape=x2_size, name='X2')
        op1 = tf.raw_ops.Div(x=x1, y=x2, name='div')
        y = tf.divide(op1, 10.0, name='Y')

        sess.run(tf.global_variables_initializer())
        constant_graph = tf.graph_util.convert_variables_to_constants(
            sess, sess.graph_def, ['Y'])

        # save to pb file
        with tf.gfile.GFile(pb_file_path, mode='wb') as f:
            f.write(constant_graph.SerializeToString())
//  [python]
def _gather_clone_loss(clone, num_clones, regularization_losses):
    """Gather the loss for a single clone.

    Args:
      clone: A Clone namedtuple.
      num_clones: The number of clones being deployed.
      regularization_losses: Possibly empty list of regularization_losses
        to add to the clone losses.

    Returns:
      A tensor for the total loss for the clone.  Can be None.
    """
    # The return value.
    sum_loss = None
    # Individual components of the loss that will need summaries.
    clone_loss = None
    regularization_loss = None
    # Compute and aggregate losses on the clone device.
    with tf.device(clone.device):
        all_losses = []
        clone_losses = tf.get_collection(tf.GraphKeys.LOSSES, clone.scope)
        if clone_losses:
            clone_loss = tf.add_n(clone_losses, name='clone_loss')
            if num_clones > 1:
                clone_loss = tf.div(clone_loss, 1.0 * num_clones,
                                    name='scaled_clone_loss')
            all_losses.append(clone_loss)
        if regularization_losses:
            regularization_loss = tf.add_n(regularization_losses,
                                           name='regularization_loss')
            all_losses.append(regularization_loss)
        if all_losses:
            sum_loss = tf.add_n(all_losses)
    # Add the summaries out of the clone device block.
    if clone_loss is not None:
        tf.summary.scalar('/'.join(filter(None,
                                          ['Losses', clone.scope, 'clone_loss'])),
                          clone_loss)
    if regularization_loss is not None:
        tf.summary.scalar('Losses/regularization_loss', regularization_loss)
    return sum_loss
//  [python]
def _gather_clone_loss(clone, num_clones, regularization_losses):
    """Gather the loss for a single clone.

    Args:
      clone: A Clone namedtuple.
      num_clones: The number of clones being deployed.
      regularization_losses: Possibly empty list of regularization_losses
        to add to the clone losses.

    Returns:
      A tensor for the total loss for the clone.  Can be None.
    """
    # The return value.
    sum_loss = None
    # Individual components of the loss that will need summaries.
    clone_loss = None
    regularization_loss = None
    # Compute and aggregate losses on the clone device.
    with tf.device(clone.device):
        all_losses = []
        clone_losses = tf.get_collection(tf.GraphKeys.LOSSES, clone.scope)
        if clone_losses:
            clone_loss = tf.add_n(clone_losses, name='clone_loss')
            if num_clones > 1:
                clone_loss = tf.divide(clone_loss, 1.0 * num_clones,
                                    name='scaled_clone_loss')
            all_losses.append(clone_loss)
        if regularization_losses:
            regularization_loss = tf.add_n(regularization_losses,
                                           name='regularization_loss')
            all_losses.append(regularization_loss)
        if all_losses:
            sum_loss = tf.add_n(all_losses)
    # Add the summaries out of the clone device block.
    if clone_loss is not None:
        tf.summary.scalar('/'.join(filter(None,
                                          ['Losses', clone.scope, 'clone_loss'])),
                          clone_loss)
    if regularization_loss is not None:
        tf.summary.scalar('Losses/regularization_loss', regularization_loss)
    return sum_loss
//  [python]
def GaussianLogDensity(x, mu, log_var, name='GaussianLogDensity'):
    with tf.name_scope(name):
        c = tf.log(2. * PI)
        var = tf.exp(log_var)
        x_mu2 = tf.square(x - mu)   # [Issue] not sure the dim works or not?
        x_mu2_over_var = tf.div(x_mu2, var + EPSILON)
        log_prob = -0.5 * (c + log_var + x_mu2_over_var)
        log_prob = tf.reduce_sum(log_prob, -1)   # keep_dims=True,
        return log_prob
//  [python]
def GaussianLogDensity(x, mu, log_var, name='GaussianLogDensity'):
    with tf.name_scope(name):
        c = tf.log(2. * PI)
        var = tf.exp(log_var)
        x_mu2 = tf.square(x - mu)   # [Issue] not sure the dim works or not?
        x_mu2_over_var = tf.divide(x_mu2, var + EPSILON)
        log_prob = -0.5 * (c + log_var + x_mu2_over_var)
        log_prob = tf.reduce_sum(log_prob, -1)   # keep_dims=True,
        return log_prob
//  [python]
def _network_template(self, state):
    """Builds a convolutional network that outputs Q-value distributions.

    Args:
      state: `tf.Tensor`, contains the agent's current state.

    Returns:
      net: _network_type object containing the tensors output by the network.
    """
    weights_initializer = slim.variance_scaling_initializer(
        factor=1.0 / np.sqrt(3.0), mode='FAN_IN', uniform=True)

    with tf.variable_scope('body'):
      net = tf.cast(state, tf.float32)
      net = tf.div(net, 255.)
      net = slim.conv2d(
          net,
          int(32 * self.network_size_expansion), [8, 8],
          stride=4,
          weights_initializer=weights_initializer)
      net = slim.conv2d(
          net,
          int(64 * self.network_size_expansion), [4, 4],
          stride=2,
          weights_initializer=weights_initializer)
      net = slim.conv2d(
          net,
          int(64 * self.network_size_expansion), [3, 3],
          stride=1,
          weights_initializer=weights_initializer)
      net = slim.flatten(net)
      body_net = slim.fully_connected(
          net,
          int(512 * self.network_size_expansion),
          weights_initializer=weights_initializer)

    logits = []
    probabilities = []
    q_values = []
    with tf.variable_scope('head'):
      for _ in range(self.number_of_gammas):
        net = slim.fully_connected(
            body_net,
            self.num_actions * self._num_atoms,
            activation_fn=None,
            weights_initializer=weights_initializer)

        gamma_logits = tf.reshape(net, [-1, self.num_actions, self._num_atoms])
        gamma_probabilities = contrib_layers.softmax(gamma_logits)
        gamma_q_values = tf.reduce_sum(
            self._support * gamma_probabilities, axis=2)

        # Add one for each gamma being used.
        logits.append(gamma_logits)
        probabilities.append(gamma_probabilities)
        q_values.append(gamma_q_values)

    # Estimate the hyperbolic discounted q-values.
    hyp_q_values = agent_utils.integrate_q_values(q_values,
                                                  self.integral_estimate,
                                                  self.eval_gammas,
                                                  self.number_of_gammas,
                                                  self.gammas)

    return self._get_network_type()(hyp_q_values, q_values, logits,
                                    probabilities)
//  [python]
def _network_template(self, state):
    """Builds a convolutional network that outputs Q-value distributions.

    Args:
      state: `tf.Tensor`, contains the agent's current state.

    Returns:
      net: _network_type object containing the tensors output by the network.
    """
    weights_initializer = slim.variance_scaling_initializer(
        factor=1.0 / np.sqrt(3.0), mode='FAN_IN', uniform=True)

    with tf.variable_scope('body'):
      net = tf.cast(state, tf.float32)
      net = tf.divide(net, 255.)
      net = slim.conv2d(
          net,
          int(32 * self.network_size_expansion), [8, 8],
          stride=4,
          weights_initializer=weights_initializer)
      net = slim.conv2d(
          net,
          int(64 * self.network_size_expansion), [4, 4],
          stride=2,
          weights_initializer=weights_initializer)
      net = slim.conv2d(
          net,
          int(64 * self.network_size_expansion), [3, 3],
          stride=1,
          weights_initializer=weights_initializer)
      net = slim.flatten(net)
      body_net = slim.fully_connected(
          net,
          int(512 * self.network_size_expansion),
          weights_initializer=weights_initializer)

    logits = []
    probabilities = []
    q_values = []
    with tf.variable_scope('head'):
      for _ in range(self.number_of_gammas):
        net = slim.fully_connected(
            body_net,
            self.num_actions * self._num_atoms,
            activation_fn=None,
            weights_initializer=weights_initializer)

        gamma_logits = tf.reshape(net, [-1, self.num_actions, self._num_atoms])
        gamma_probabilities = contrib_layers.softmax(gamma_logits)
        gamma_q_values = tf.reduce_sum(
            self._support * gamma_probabilities, axis=2)

        # Add one for each gamma being used.
        logits.append(gamma_logits)
        probabilities.append(gamma_probabilities)
        q_values.append(gamma_q_values)

    # Estimate the hyperbolic discounted q-values.
    hyp_q_values = agent_utils.integrate_q_values(q_values,
                                                  self.integral_estimate,
                                                  self.eval_gammas,
                                                  self.number_of_gammas,
                                                  self.gammas)

    return self._get_network_type()(hyp_q_values, q_values, logits,
                                    probabilities)
//  [python]
def input_setup(self):
        """
        dataset_A:non-makeup
        dataset_B:makeup
        :return:
        """
        filename_A = tf.train.match_filenames_once("./all/images/non-makeup/*.png")
        self.queue_length_A = tf.size(filename_A)
        filename_B = tf.train.match_filenames_once("./all/images/makeup/*.png")
        self.queue_length_B = tf.size(filename_B)

        filename_A_queue = tf.train.string_input_producer(filename_A,shuffle=False)
        filename_B_queue = tf.train.string_input_producer(filename_B,shuffle=False)

        image_reader = tf.WholeFileReader()
        _, image_file_A = image_reader.read(filename_A_queue)
        _, image_file_B = image_reader.read(filename_B_queue)
        self.image_A = tf.subtract(tf.div(tf.image.resize_images(tf.image.decode_jpeg(image_file_A),[256,256]),127.5),1)
        self.image_B = tf.subtract(tf.div(tf.image.resize_images(tf.image.decode_jpeg(image_file_B),[256,256]),127.5),1)
//  [python]
def input_setup(self):
        """
        dataset_A:non-makeup
        dataset_B:makeup
        :return:
        """
        filename_A = tf.train.match_filenames_once("./all/images/non-makeup/*.png")
        self.queue_length_A = tf.size(filename_A)
        filename_B = tf.train.match_filenames_once("./all/images/makeup/*.png")
        self.queue_length_B = tf.size(filename_B)

        filename_A_queue = tf.train.string_input_producer(filename_A,shuffle=False)
        filename_B_queue = tf.train.string_input_producer(filename_B,shuffle=False)

        image_reader = tf.WholeFileReader()
        _, image_file_A = image_reader.read(filename_A_queue)
        _, image_file_B = image_reader.read(filename_B_queue)
        self.image_A = tf.subtract(tf.divide(tf.image.resize_images(tf.image.decode_jpeg(image_file_A),[256,256]),127.5),1)
        self.image_B = tf.subtract(tf.divide(tf.image.resize_images(tf.image.decode_jpeg(image_file_B),[256,256]),127.5),1)
//  [python]
def mean_over_time(inputs, lengths):
    ''' Implements a MoT layer.

    Takes average vector across temporal dimension.

    Args:
        inputs: `tensor` [bsz x timestep x dim]
        lengths: `tensor` [bsz x 1] of sequence lengths

    Returns:
        mean_vec:`tensor` [bsz x dim]
    '''
    mean_vec = tf.reduce_sum(inputs, 1) 
    mean_vec = tf.div(mean_vec, tf.cast(lengths, tf.float32))
    return mean_vec
//  [python]
def mean_over_time(inputs, lengths):
    ''' Implements a MoT layer.

    Takes average vector across temporal dimension.

    Args:
        inputs: `tensor` [bsz x timestep x dim]
        lengths: `tensor` [bsz x 1] of sequence lengths

    Returns:
        mean_vec:`tensor` [bsz x dim]
    '''
    mean_vec = tf.reduce_sum(inputs, 1) 
    mean_vec = tf.divide(mean_vec, tf.cast(lengths, tf.float32))
    return mean_vec
//  [python]
def cr_loss(e1, e2, labels, depth):
        label_pool_one_hot = tf.one_hot(labels, depth=depth) 
        Afinite_hot = tf.matmul(label_pool_one_hot, tf.transpose(label_pool_one_hot, [1, 0]))

        rs_map_soft = tf.matmul(e1, tf.transpose(e2, [1, 0])) 
        rs_map_soft = tf.nn.relu(rs_map_soft)
        rs_map_soft = tf.clip_by_value(rs_map_soft, 1e-4, 1-(1e-4))
        Afinite = tf.reshape(Afinite_hot, [-1, 1])
        rs_map = tf.reshape(rs_map_soft, [-1, 1])
        loss_cr = -1.0 * tf.reduce_mean(Afinite * tf.log(rs_map) + (1 - Afinite) * tf.log(1 - rs_map))
        A_R = tf.reduce_sum (Afinite_hot * rs_map_soft, axis=1)
        loss_tjp = -1.0 * tf.reduce_mean(tf.log(tf.div(A_R, tf.reduce_sum (rs_map_soft, axis=1))))
        loss_tjr = -1.0 * tf.reduce_mean(tf.log(tf.div(A_R, tf.reduce_sum (Afinite_hot, axis=1))))
        A_R_1 = tf.reduce_sum((1-Afinite_hot) * (1-rs_map_soft), axis=1)
        # loss_tjs = -1.0 * tf.reduce_mean( tf.log( tf.div( A_R_1, tf.reduce_sum( (1-Afinite_hot), axis=1) ) ) )
        return loss_cr + loss_tjp + loss_tjr
//  [python]
def cr_loss(e1, e2, labels, depth):
        label_pool_one_hot = tf.one_hot(labels, depth=depth) 
        Afinite_hot = tf.matmul(label_pool_one_hot, tf.transpose(label_pool_one_hot, [1, 0]))

        rs_map_soft = tf.matmul(e1, tf.transpose(e2, [1, 0])) 
        rs_map_soft = tf.nn.relu(rs_map_soft)
        rs_map_soft = tf.clip_by_value(rs_map_soft, 1e-4, 1-(1e-4))
        Afinite = tf.reshape(Afinite_hot, [-1, 1])
        rs_map = tf.reshape(rs_map_soft, [-1, 1])
        loss_cr = -1.0 * tf.reduce_mean(Afinite * tf.log(rs_map) + (1 - Afinite) * tf.log(1 - rs_map))
        A_R = tf.reduce_sum (Afinite_hot * rs_map_soft, axis=1)
        loss_tjp = -1.0 * tf.reduce_mean(tf.log(tf.divide(A_R, tf.reduce_sum (rs_map_soft, axis=1))))
        loss_tjr = -1.0 * tf.reduce_mean(tf.log(tf.divide(A_R, tf.reduce_sum (Afinite_hot, axis=1))))
        A_R_1 = tf.reduce_sum((1-Afinite_hot) * (1-rs_map_soft), axis=1)
        # loss_tjs = -1.0 * tf.reduce_mean( tf.log( tf.divide( A_R_1, tf.reduce_sum( (1-Afinite_hot), axis=1) ) ) )
        return loss_cr + loss_tjp + loss_tjr
//  [python]
def GaussianKLD(mu1, lv1, mu2, lv2):
    ''' Kullback-Leibler divergence of two Gaussians
        *Assuming that each dimension is independent
        mu: mean
        lv: log variance
        Equation: http://stats.stackexchange.com/questions/7440/kl-divergence-between-two-univariate-gaussians
    '''
    with tf.name_scope('GaussianKLD'):
        v1 = tf.exp(lv1)
        v2 = tf.exp(lv2)
        mu_diff_sq = tf.square(mu1 - mu2)
        dimwise_kld = .5 * (
            (lv2 - lv1) + tf.div(v1 + mu_diff_sq, v2 + EPSILON) - 1.)
        return tf.reduce_sum(dimwise_kld, -1)
//  [python]
def GaussianKLD(mu1, lv1, mu2, lv2):
    ''' Kullback-Leibler divergence of two Gaussians
        *Assuming that each dimension is independent
        mu: mean
        lv: log variance
        Equation: http://stats.stackexchange.com/questions/7440/kl-divergence-between-two-univariate-gaussians
    '''
    with tf.name_scope('GaussianKLD'):
        v1 = tf.exp(lv1)
        v2 = tf.exp(lv2)
        mu_diff_sq = tf.square(mu1 - mu2)
        dimwise_kld = .5 * (
            (lv2 - lv1) + tf.divide(v1 + mu_diff_sq, v2 + EPSILON) - 1.)
        return tf.reduce_sum(dimwise_kld, -1)
//  [python]
def rsquared_metric(labels, predictions, axis=None):
    SST, update_op1 = tf.metrics.mean_squared_error(labels, tf.reduce_mean(labels, axis=axis, keepdims=True))
    SSE, update_op2 = tf.metrics.mean_squared_error(labels, predictions)
    return tf.subtract(1.0, tf.div(SSE, SST)), tf.group(update_op1, update_op2)
//  [python]
def rsquared_metric(labels, predictions, axis=None):
    SST, update_op1 = tf.metrics.mean_squared_error(labels, tf.reduce_mean(labels, axis=axis, keepdims=True))
    SSE, update_op2 = tf.metrics.mean_squared_error(labels, predictions)
    return tf.subtract(1.0, tf.divide(SSE, SST)), tf.group(update_op1, update_op2)
//  [python]
def rsquared(labels, predictions, axis=None):
    residual = tf.reduce_sum(tf.square(tf.subtract(labels, predictions)))
    total = tf.reduce_sum(tf.square(tf.subtract(labels, tf.reduce_mean(labels, axis=axis, keepdims=True))))
    r2 = tf.subtract(1.0, tf.div(residual, total))
    return r2
//  [python]
def rsquared(labels, predictions, axis=None):
    residual = tf.reduce_sum(tf.square(tf.subtract(labels, predictions)))
    total = tf.reduce_sum(tf.square(tf.subtract(labels, tf.reduce_mean(labels, axis=axis, keepdims=True))))
    r2 = tf.subtract(1.0, tf.divide(residual, total))
    return r2
//  [python]
def get_value(self, grad, n_steps, average):
        with tf.device(self.device):
            if average:
                return tf.div(self.accumulator_var + grad, n_steps)
            else:
                return self.accumulator_var + grad
//  [python]
def get_value(self, grad, n_steps, average):
        with tf.device(self.device):
            if average:
                return tf.divide(self.accumulator_var + grad, n_steps)
            else:
                return self.accumulator_var + grad
//  [python]
def groupnorm(inputs,
              G=32,
              data_format='channels_last',
              weight_decay=0.0,
              epsilon=1e-5,
              training=True,
              gamma_init=1,
              beta_init=0):
    '''
    Like LayerNorm, z-scores features along the channel dimension only.
    However, it only normalizes within G groups of C/G channels each.
    Optionally applies learnable scale/shift parameters.
    '''
    assert len(inputs.shape.as_list()) == 4, "Applies only to conv2D layers"
    if data_format == 'channels_first':
        inputs = tf.transpose(inputs, [0,2,3,1])
    elif data_format == 'channels_last':
        pass
    else:
        raise ValueError("data_format must be 'channels_first' or 'channels_last'")

    B,H,W,C = inputs.shape.as_list()
    assert C % G == 0, "num groups G must divide C"
    CpG = C // G

    inputs = tf.reshape(inputs, [B,H,W,CpG,G])
    mean, var = tf.nn.moments(inputs, axes=[1,2,3], keep_dims=True)
    inputs = tf.div(inputs - mean, tf.sqrt(var + epsilon))
    inputs = tf.reshape(inputs, [B,H,W,C])

    if training:
        gamma = tf.get_variable("groupnorm_scale", shape=[1,1,1,C], dtype=tf.float32,
                                initializer=initializer("constant", float(gamma_init)))
                                # regularizer=tf.contrib.layers.l2_regularizer(weight_decay))
        beta = tf.get_variable("groupnorm_shift", shape=[1,1,1,C], dtype=tf.float32,
                               initializer=initializer("constant", float(beta_init)))
                               # regularizer=tf.contrib.layers.l2_regularizer(weight_decay))
    else:
        gamma = tf.constant(gamma_init, dtype=tf.float32)
        beta = tf.constant(beta_init, dtype=tf.float32)

    inputs = gamma*inputs + beta
    if data_format == 'channels_first':
        inputs = tf.transpose(inputs, [0,3,1,2])

    return inputs
//  [python]
def groupnorm(inputs,
              G=32,
              data_format='channels_last',
              weight_decay=0.0,
              epsilon=1e-5,
              training=True,
              gamma_init=1,
              beta_init=0):
    '''
    Like LayerNorm, z-scores features along the channel dimension only.
    However, it only normalizes within G groups of C/G channels each.
    Optionally applies learnable scale/shift parameters.
    '''
    assert len(inputs.shape.as_list()) == 4, "Applies only to conv2D layers"
    if data_format == 'channels_first':
        inputs = tf.transpose(inputs, [0,2,3,1])
    elif data_format == 'channels_last':
        pass
    else:
        raise ValueError("data_format must be 'channels_first' or 'channels_last'")

    B,H,W,C = inputs.shape.as_list()
    assert C % G == 0, "num groups G must divide C"
    CpG = C // G

    inputs = tf.reshape(inputs, [B,H,W,CpG,G])
    mean, var = tf.nn.moments(inputs, axes=[1,2,3], keep_dims=True)
    inputs = tf.divide(inputs - mean, tf.sqrt(var + epsilon))
    inputs = tf.reshape(inputs, [B,H,W,C])

    if training:
        gamma = tf.get_variable("groupnorm_scale", shape=[1,1,1,C], dtype=tf.float32,
                                initializer=initializer("constant", float(gamma_init)))
                                # regularizer=tf.contrib.layers.l2_regularizer(weight_decay))
        beta = tf.get_variable("groupnorm_shift", shape=[1,1,1,C], dtype=tf.float32,
                               initializer=initializer("constant", float(beta_init)))
                               # regularizer=tf.contrib.layers.l2_regularizer(weight_decay))
    else:
        gamma = tf.constant(gamma_init, dtype=tf.float32)
        beta = tf.constant(beta_init, dtype=tf.float32)

    inputs = gamma*inputs + beta
    if data_format == 'channels_first':
        inputs = tf.transpose(inputs, [0,3,1,2])

    return inputs
//  [python]
def compute_mse(predictions, true_labels, r, c):
	return tf.div(tf.reduce_sum(tf.square(tf.subtract(predictions,true_labels))),r*c)
//  [python]
def compute_mse(predictions, true_labels, r, c):
	return tf.divide(tf.reduce_sum(tf.square(tf.subtract(predictions,true_labels))),r*c)
//  [python]
def __init__(
    self,
    layer = None,
    act = tf.identity,
    epsilon = 1e-5,
    scale_init = tf.truncated_normal_initializer(mean=1.0, stddev=0.02),
    offset_init = tf.constant_initializer(0.0),
    name ='instan_norm',
    ):
        Layer.__init__(self, name=name)
        self.inputs = layer.outputs
        print("  [TL] InstanceNormLayer %s: epsilon:%f act:%s" %
                            (self.name, epsilon, act.__name__))

        with tf.variable_scope(name) as vs:
            mean, var = tf.nn.moments(self.inputs, [1, 2], keep_dims=True)
            scale = tf.get_variable('scale',[self.inputs.get_shape()[-1]],
                initializer=tf.truncated_normal_initializer(mean=1.0, stddev=0.02))
            offset = tf.get_variable('offset',[self.inputs.get_shape()[-1]],initializer=tf.constant_initializer(0.0))
            self.outputs = scale * tf.div(self.inputs-mean, tf.sqrt(var+epsilon)) + offset
            self.outputs = act(self.outputs)
            variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name)

        self.all_layers = list(layer.all_layers)
        self.all_params = list(layer.all_params)
        self.all_drop = dict(layer.all_drop)
        self.all_layers.extend( [self.outputs] )
        self.all_params.extend( variables )
//  [python]
def __init__(
    self,
    layer = None,
    act = tf.identity,
    epsilon = 1e-5,
    scale_init = tf.truncated_normal_initializer(mean=1.0, stddev=0.02),
    offset_init = tf.constant_initializer(0.0),
    name ='instan_norm',
    ):
        Layer.__init__(self, name=name)
        self.inputs = layer.outputs
        print("  [TL] InstanceNormLayer %s: epsilon:%f act:%s" %
                            (self.name, epsilon, act.__name__))

        with tf.variable_scope(name) as vs:
            mean, var = tf.nn.moments(self.inputs, [1, 2], keep_dims=True)
            scale = tf.get_variable('scale',[self.inputs.get_shape()[-1]],
                initializer=tf.truncated_normal_initializer(mean=1.0, stddev=0.02))
            offset = tf.get_variable('offset',[self.inputs.get_shape()[-1]],initializer=tf.constant_initializer(0.0))
            self.outputs = scale * tf.divide(self.inputs-mean, tf.sqrt(var+epsilon)) + offset
            self.outputs = act(self.outputs)
            variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name)

        self.all_layers = list(layer.all_layers)
        self.all_params = list(layer.all_params)
        self.all_drop = dict(layer.all_drop)
        self.all_layers.extend( [self.outputs] )
        self.all_params.extend( variables )
//  [python]
def iou(bbox_1, bbox_2):
  """Compute iou of a box with another box. Box format '[y_min, x_min, y_max, x_max]'.
  Args:
    bbox_1: 1-D with shape `[4]`.
    bbox_2: 1-D with shape `[4]`.

  Returns:
    IOU
  """
  lr = tf.minimum(bbox_1[3], bbox_2[3]) - tf.maximum(bbox_1[1], bbox_2[1])
  tb = tf.minimum(bbox_1[2], bbox_2[2]) - tf.maximum(bbox_1[0], bbox_2[0])
  lr = tf.maximum(lr, lr * 0)
  tb = tf.maximum(tb, tb * 0)
  intersection = tf.multiply(tb, lr)
  union = tf.subtract(
    tf.multiply((bbox_1[3] - bbox_1[1]), (bbox_1[2] - bbox_1[0])) +
    tf.multiply((bbox_2[3] - bbox_2[1]), (bbox_2[2] - bbox_2[0])),
    intersection
  )
  iou = tf.div(intersection, union)
  return iou
//  [python]
def iou(bbox_1, bbox_2):
  """Compute iou of a box with another box. Box format '[y_min, x_min, y_max, x_max]'.
  Args:
    bbox_1: 1-D with shape `[4]`.
    bbox_2: 1-D with shape `[4]`.

  Returns:
    IOU
  """
  lr = tf.minimum(bbox_1[3], bbox_2[3]) - tf.maximum(bbox_1[1], bbox_2[1])
  tb = tf.minimum(bbox_1[2], bbox_2[2]) - tf.maximum(bbox_1[0], bbox_2[0])
  lr = tf.maximum(lr, lr * 0)
  tb = tf.maximum(tb, tb * 0)
  intersection = tf.multiply(tb, lr)
  union = tf.subtract(
    tf.multiply((bbox_1[3] - bbox_1[1]), (bbox_1[2] - bbox_1[0])) +
    tf.multiply((bbox_2[3] - bbox_2[1]), (bbox_2[2] - bbox_2[0])),
    intersection
  )
  iou = tf.divide(intersection, union)
  return iou
//  [python]
def __call__(self, inputs, *args, **kwargs):
        """Call MeanShift."""
        std = tf.convert_to_tensor(self.rgb_std, dtype=tf.float32)
        self.weight = tf.convert_to_tensor(np.eye(3).astype(np.float32))  # tf.eye(3)
        self.weight = tf.div(self.weight, std)
        self.bias = self.sign * self.rgb_range * tf.convert_to_tensor(self.rgb_mean, dtype=tf.float32)
        self.bias = tf.div(self.bias, std)
        res = tf.einsum('ij, njhw->nihw', self.weight, inputs)
        res = tf.transpose(res, [0, 2, 3, 1])
        res = tf.nn.bias_add(res, self.bias)
        res = tf.transpose(res, [0, 3, 1, 2])
        return res
//  [python]
def __call__(self, inputs, *args, **kwargs):
        """Call MeanShift."""
        std = tf.convert_to_tensor(self.rgb_std, dtype=tf.float32)
        self.weight = tf.convert_to_tensor(np.eye(3).astype(np.float32))  # tf.eye(3)
        self.weight = tf.divide(self.weight, std)
        self.bias = self.sign * self.rgb_range * tf.convert_to_tensor(self.rgb_mean, dtype=tf.float32)
        self.bias = tf.divide(self.bias, std)
        res = tf.einsum('ij, njhw->nihw', self.weight, inputs)
        res = tf.transpose(res, [0, 2, 3, 1])
        res = tf.nn.bias_add(res, self.bias)
        res = tf.transpose(res, [0, 3, 1, 2])
        return res
//  [python]
def batch_iou(bboxes, bbox):
  """Compute iou of a batch of boxes with another box. Box format '[y_min, x_min, y_max, x_max]'.
  Args:
    bboxes: A batch of boxes. 2-D with shape `[B, 4]`.
    bbox: A single box. 1-D with shape `[4]`.

  Returns:
    Batch of IOUs
  """
  lr = tf.maximum(
    tf.minimum(bboxes[:, 3], bbox[3]) -
    tf.maximum(bboxes[:, 1], bbox[1]),
    0
  )
  tb = tf.maximum(
    tf.minimum(bboxes[:, 2], bbox[2]) -
    tf.maximum(bboxes[:, 0], bbox[0]),
    0
  )
  intersection = tf.multiply(tb, lr)
  union = tf.subtract(
    tf.multiply((bboxes[:, 3] - bboxes[:, 1]), (bboxes[:, 2] - bboxes[:, 0])) +
    tf.multiply((bbox[3] - bbox[1]), (bbox[2] - bbox[0])),
    intersection
  )
  iou = tf.div(intersection, union)
  return iou
//  [python]
def batch_iou(bboxes, bbox):
  """Compute iou of a batch of boxes with another box. Box format '[y_min, x_min, y_max, x_max]'.
  Args:
    bboxes: A batch of boxes. 2-D with shape `[B, 4]`.
    bbox: A single box. 1-D with shape `[4]`.

  Returns:
    Batch of IOUs
  """
  lr = tf.maximum(
    tf.minimum(bboxes[:, 3], bbox[3]) -
    tf.maximum(bboxes[:, 1], bbox[1]),
    0
  )
  tb = tf.maximum(
    tf.minimum(bboxes[:, 2], bbox[2]) -
    tf.maximum(bboxes[:, 0], bbox[0]),
    0
  )
  intersection = tf.multiply(tb, lr)
  union = tf.subtract(
    tf.multiply((bboxes[:, 3] - bboxes[:, 1]), (bboxes[:, 2] - bboxes[:, 0])) +
    tf.multiply((bbox[3] - bbox[1]), (bbox[2] - bbox[0])),
    intersection
  )
  iou = tf.divide(intersection, union)
  return iou