jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def evaluate(sentence):
    sentence = list(sentence[0])
    sentence.insert(0, '<SOS>')
    sentence.append('<EOS>')
    sentence = token_Japanese.texts_to_sequences(sentence)
    # For some reason tokenizing JP returns [[1], [2], [3]] when we want [1, 2, 3]
    sentence = [sentence[i][0] for i in range(len(sentence))]
    sentence = pad([sentence], length = len(Japanese[0]))
    sentence = tf.convert_to_tensor(np.array(sentence),dtype=tf.int64)

    decoder_input = tf.convert_to_tensor(np.array(token_English.texts_to_sequences(['SOS'])),dtype=tf.int64)

    outputs = None
    for i in range(len(Japanese[0])):
        enc_padding_mask, combined_mask, dec_padding_mask = create_masks(
            sentence, decoder_input)
        #tf.print(enc_padding_mask.shape, combined_mask.shape, dec_padding_mask.shape)
        # predictions.shape == (batch_size, seq_len, vocab_size)
        predictions, attention_weights, out = transformer([sentence,
                                                     decoder_input,
                                                     enc_padding_mask,
                                                     combined_mask,
                                                     dec_padding_mask],
                                                     False)

        # select the last word from the seq_len dimension
        predictions = predictions[: ,-1:, :]  # (batch_size, 1, vocab_size)

        predicted_id = tf.cast(tf.argmax(predictions, axis=-1), tf.int64)
        
        #tf.print(predicted_id,decoder_input)
        # return the result if the predicted_id is equal to the end token
        if predicted_id == token_English.texts_to_sequences(['EOS']):
            out['input_sentence'] = sentence.numpy()
            return tf.squeeze(decoder_input, axis=0), attention_weights , sentence, out

        # concatentate the predicted_id to the output which is given to the decoder
        # as its input.
        decoder_input = tf.concat([decoder_input, predicted_id], axis=1)
        outputs = out

    outputs['input_sentence'] = sentence.numpy()
    return tf.squeeze(decoder_input, axis=0), attention_weights, sentence, outputs
//  [python]
def cast(x, dtype):
    dtype = standardize_dtype(dtype)
    if isinstance(x, tf.SparseTensor):
        x_shape = x.shape
        x = t.cast(x, dtype)
        x.set_shape(x_shape)
        return x
    else:
        return t.cast(x, dtype=dtype)
//  [python]
def cast(x, dtype):
    dtype = standardize_dtype(dtype)
    if isinstance(x, tf.SparseTensor):
        x_shape = x.shape
        x = tf.cast(x, dtype)
        x.set_shape(x_shape)
        return x
    else:
        return tf.cast(x, dtype=dtype)
//  [python]
def convert_to_tensor(x, dtype=None, sparse=None):
    if isinstance(x, tf.SparseTensor) and sparse is not None and not sparse:
        x = sparse_to_dense(x)
    if dtype is not None:
        dtype = standardize_dtype(dtype)
    if not tf.is_tensor(x):
        if dtype == "bool":
            # TensorFlow boolean conversion is stricter than other backends.
            # It does not allow ints. We convert without dtype and cast instead.
            x = tf.convert_to_tensor(x)
            return t.cast(x, dtype)
        return tf.convert_to_tensor(x, dtype=dtype)
    elif dtype is not None and not x.dtype == dtype:
        if isinstance(x, tf.SparseTensor):
            x_shape = x.shape
            x = t.cast(x, dtype)
            x.set_shape(x_shape)
            return x
        return t.cast(x, dtype=dtype)
    return x
//  [python]
def convert_to_tensor(x, dtype=None, sparse=None):
    if isinstance(x, tf.SparseTensor) and sparse is not None and not sparse:
        x = sparse_to_dense(x)
    if dtype is not None:
        dtype = standardize_dtype(dtype)
    if not tf.is_tensor(x):
        if dtype == "bool":
            # TensorFlow boolean conversion is stricter than other backends.
            # It does not allow ints. We convert without dtype and cast instead.
            x = tf.convert_to_tensor(x)
            return tf.cast(x, dtype)
        return tf.convert_to_tensor(x, dtype=dtype)
    elif dtype is not None and not x.dtype == dtype:
        if isinstance(x, tf.SparseTensor):
            x_shape = x.shape
            x = tf.cast(x, dtype)
            x.set_shape(x_shape)
            return x
        return tf.cast(x, dtype=dtype)
    return x
//  [python]
def sequence_loss(logits, targets, weights,
                  average_across_timesteps=True, average_across_batch=True,
                  softmax_loss_function=None, name=None):
    """Weighted cross-entropy loss for a sequence of logits, batch-collapsed.

    Args:
        logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols].
        targets: List of 1D batch-sized int32 Tensors of the same length as logits.
        weights: List of 1D batch-sized float-Tensors of the same length as logits.
        average_across_timesteps: If set, divide the returned cost by the total
            label weight.
        average_across_batch: If set, divide the returned cost by the batch size.
        softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch
            to be used instead of the standard softmax (the default if this is None).
        name: Optional name for this operation, defaults to "sequence_loss".

    Returns:
        A scalar float Tensor: The average log-perplexity per symbol (weighted).

    Raises:
        ValueError: If len(logits) is different from len(targets) or len(weights).
    """
    with tf.compat.v1.name_scope(name, "sequence_loss", logits + targets + weights):
        cost = tf.reduce_sum(sequence_loss_by_example(
            logits, targets, weights,
            average_across_timesteps=average_across_timesteps,
            softmax_loss_function=softmax_loss_function))
        if average_across_batch:
            batch_size = tf.shape(targets[0])[0]
            return cost / tf.cast(batch_size, tf.float32)

        return cost
//  [python]
def moments(data,threshold,axes=-1):
        mask = tf.greater_equal(data,threshold)
        mask_f = tf.cast(mask,data.dtype)
        data_f = tf.where(mask,data,tf.zeros_like(data))
        data_sum = tf.reduce_sum(data_f,axis=axes,keepdims=True)
        data_nr = tf.maximum(tf.reduce_sum(mask_f,axis=axes,keepdims=True),1)
        data_mean = data_sum/data_nr
        s_diff = tf.squared_difference(data, tf.stop_gradient(data_mean))
        s_diff = tf.where(mask,s_diff,tf.zeros_like(s_diff))
        variance = tf.reduce_sum(
            s_diff,
            axis=axes,
            keepdims=True,
            name="variance")/data_nr
        return data_mean,variance
//  [python]
def moments(data,weights,threshold,axes=-1):
        mask = tf.greater_equal(data,threshold)
        mask_f = tf.cast(mask,data.dtype)
        if weights.dtype != data.dtype:
            weights = tf.cast(weights,data.dtype)
        mask_wf = mask_f*weights
        data_f = tf.where(mask,data,tf.zeros_like(data))
        data_wf = data_f*weights
        data_sum = tf.reduce_sum(data_wf,axis=axes,keepdims=True)
        data_nr = tf.maximum(tf.reduce_sum(mask_wf,axis=axes,keepdims=True),1)
        data_mean = data_sum/data_nr
        s_diff = tf.squared_difference(data, tf.stop_gradient(data_mean))
        s_diff = tf.where(mask,s_diff,tf.zeros_like(s_diff))*weights
        variance = tf.reduce_sum(
            s_diff,
            axis=axes,
            keepdims=True,
            name="variance")/data_nr
        return data_mean,variance
//  [python]
def process_fn(self, examples: tf.Tensor) -> Mapping[str, tf.Tensor]:
    """Converts serialized tf.Examples to batched model features.

    Args:
      examples: batch examples. If `self.batch_size` is not None,
        `examples.shape[0]` must be the same as `self.batch_size`.

    Returns:
      A Mapping from feature names to batch features.
    """
    ds = tf.data.Dataset.from_tensor_slices(examples)
    # Dataset of parsed tf Examples.
    ds = ds.map(self.parse_example)
    if self.run_precache:
      ds = self.task.preprocess_precache(ds)
    ds = self.task.preprocess_postcache(ds, self.task_feature_lengths)
    # Dataset of batched model features.
    ds = self.feature_converter(
        ds, task_feature_lengths=self.task_feature_lengths)
    # Assume all batch size are the same.
    single_feature = jax.tree_util.tree_leaves(examples)[0]
    if self.batch_size is not None:
      single_feature.shape[:1].assert_is_compatible_with([self.batch_size])
      ds = ds.batch(self.batch_size, drop_remainder=True)
    else:
      batch_size = t.cast(t.shape(single_feature)[0], dtype=t.int64)
      ds = ds.batch(batch_size, drop_remainder=True)
    # As we process one batch at a time, the dataset ds has a single batch.
    return ds.get_single_element()
//  [python]
def process_fn(self, examples: tf.Tensor) -> Mapping[str, tf.Tensor]:
    """Converts serialized tf.Examples to batched model features.

    Args:
      examples: batch examples. If `self.batch_size` is not None,
        `examples.shape[0]` must be the same as `self.batch_size`.

    Returns:
      A Mapping from feature names to batch features.
    """
    ds = tf.data.Dataset.from_tensor_slices(examples)
    # Dataset of parsed tf Examples.
    ds = ds.map(self.parse_example)
    if self.run_precache:
      ds = self.task.preprocess_precache(ds)
    ds = self.task.preprocess_postcache(ds, self.task_feature_lengths)
    # Dataset of batched model features.
    ds = self.feature_converter(
        ds, task_feature_lengths=self.task_feature_lengths)
    # Assume all batch size are the same.
    single_feature = jax.tree_util.tree_leaves(examples)[0]
    if self.batch_size is not None:
      single_feature.shape[:1].assert_is_compatible_with([self.batch_size])
      ds = ds.batch(self.batch_size, drop_remainder=True)
    else:
      batch_size = tf.cast(tf.shape(single_feature)[0], dtype=tf.int64)
      ds = ds.batch(batch_size, drop_remainder=True)
    # As we process one batch at a time, the dataset ds has a single batch.
    return ds.get_single_element()
//  [python]
def macro_f1(y, y_hat, thresh=0.5):
    """Compute the macro F1-score on a batch of observations (average F1 across labels)
    Args:
        y (int32 Tensor): labels array of shape (BATCH_SIZE, N_LABELS)
        y_hat (float32 Tensor): probability matrix from forward propagation of shape (BATCH_SIZE, N_LABELS)
        thresh: probability value above which we predict positive

    Returns:
        macro_f1 (scalar Tensor): value of macro F1 for the batch
    """
    y_pred = t.cast(t.greater(y_hat, thresh), t.float32)
    tp = t.cast(t.math.count_nonzero(y_pred * y, axis=0), t.float32)
    fp = t.cast(t.math.count_nonzero(y_pred * (1 - y), axis=0), t.float32)
    fn = t.cast(t.math.count_nonzero((1 - y_pred) * y, axis=0), t.float32)
    f1 = 2*tp / (2*tp + fn + fp + 1e-16)
    macro_f1 = t.reduce_mean(f1, axis=-1)
    return macro_f1
//  [python]
def macro_f1(y, y_hat, thresh=0.5):
    """Compute the macro F1-score on a batch of observations (average F1 across labels)
    Args:
        y (int32 Tensor): labels array of shape (BATCH_SIZE, N_LABELS)
        y_hat (float32 Tensor): probability matrix from forward propagation of shape (BATCH_SIZE, N_LABELS)
        thresh: probability value above which we predict positive

    Returns:
        macro_f1 (scalar Tensor): value of macro F1 for the batch
    """
    y_pred = tf.cast(tf.greater(y_hat, thresh), tf.float32)
    tp = tf.cast(tf.math.count_nonzero(y_pred * y, axis=0), tf.float32)
    fp = tf.cast(tf.math.count_nonzero(y_pred * (1 - y), axis=0), tf.float32)
    fn = tf.cast(tf.math.count_nonzero((1 - y_pred) * y, axis=0), tf.float32)
    f1 = 2*tp / (2*tp + fn + fp + 1e-16)
    macro_f1 = tf.reduce_mean(f1, axis=-1)
    return macro_f1
//  [python]
def ibm(xi):
	"""
	Computes the ideal binary mask (IBM) with a threshold of 0 dB.

	Argument/s:
		xi - a priori SNR.

	Returns:
		IBM.
	"""
	return t.cast(t.math.greater(xi, 1.0), t.float32)
//  [python]
def ibm(xi):
	"""
	Computes the ideal binary mask (IBM) with a threshold of 0 dB.

	Argument/s:
		xi - a priori SNR.

	Returns:
		IBM.
	"""
	return tf.cast(tf.math.greater(xi, 1.0), tf.float32)
//  [python]
def build_graph(self, A, B):
        with tf.name_scope('preprocess'):
            A = tf.transpose(A / 128.0 - 1.0, [0, 3, 1, 2])
            B = tf.transpose(B / 128.0 - 1.0, [0, 3, 1, 2])

        def viz3(name, a, b, c):
            with tf.name_scope(name):
                im = tf.concat([a, b, c], axis=3)
                im = tf.transpose(im, [0, 2, 3, 1])
                im = (im + 1.0) * 128
                im = tf.clip_by_value(im, 0, 255)
                im = tf.cast(im, tf.uint8, name='viz')
            tf.summary.image(name, im, max_outputs=50)

        # use the initializers from torch
        with argscope([Conv2D, Conv2DTranspose], use_bias=False,
                      kernel_initializer=tf.random_normal_initializer(stddev=0.02)), \
                argscope([Conv2D, Conv2DTranspose, InstanceNorm], data_format='channels_first'):
            with tf.variable_scope('gen'):
                with tf.variable_scope('B'):
                    AB = self.generator(A)
                with tf.variable_scope('A'):
                    BA = self.generator(B)
                    ABA = self.generator(AB)
                with tf.variable_scope('B'):
                    BAB = self.generator(BA)

            viz3('A_recon', A, AB, ABA)
            viz3('B_recon', B, BA, BAB)

            with tf.variable_scope('discrim'):
                with tf.variable_scope('A'):
                    A_dis_real = self.discriminator(A)
                    A_dis_fake = self.discriminator(BA)

                with tf.variable_scope('B'):
                    B_dis_real = self.discriminator(B)
                    B_dis_fake = self.discriminator(AB)

        def LSGAN_losses(real, fake):
            d_real = tf.reduce_mean(tf.squared_difference(real, 1), name='d_real')
            d_fake = tf.reduce_mean(tf.square(fake), name='d_fake')
            d_loss = tf.multiply(d_real + d_fake, 0.5, name='d_loss')

            g_loss = tf.reduce_mean(tf.squared_difference(fake, 1), name='g_loss')
            add_moving_summary(g_loss, d_loss)
            return g_loss, d_loss

        with tf.name_scope('losses'):
            with tf.name_scope('LossA'):
                # reconstruction loss
                recon_loss_A = tf.reduce_mean(tf.abs(A - ABA), name='recon_loss')
                # gan loss
                G_loss_A, D_loss_A = LSGAN_losses(A_dis_real, A_dis_fake)

            with tf.name_scope('LossB'):
                recon_loss_B = tf.reduce_mean(tf.abs(B - BAB), name='recon_loss')
                G_loss_B, D_loss_B = LSGAN_losses(B_dis_real, B_dis_fake)

            LAMBDA = 10.0
            self.g_loss = tf.add((G_loss_A + G_loss_B),
                                 (recon_loss_A + recon_loss_B) * LAMBDA, name='G_loss_total')
            self.d_loss = tf.add(D_loss_A, D_loss_B, name='D_loss_total')
        self.collect_variables('gen', 'discrim')

        add_moving_summary(recon_loss_A, recon_loss_B, self.g_loss, self.d_loss)
//  [python]
def mae_loss(pred, label):
    mask = tf.not_equal(label, 0)
    mask = tf.to_float(mask)
    mask /= tf.reduce_mean(mask)
    mask = tf.compat.v2.where(
        condition = tf.math.is_nan(mask), x = 0., y = mask)
    loss = tf.abs(tf.subtract(pred, label))
    loss *= mask
    loss = tf.compat.v2.where(
        condition = tf.math.is_nan(loss), x = 0., y = loss)
    loss = tf.reduce_mean(loss)
    return loss
//  [python]
def mae_loss(pred, label):
    mask = tf.not_equal(label, 0)
    mask = tf.cast(mask, tf.float32)
    mask /= tf.reduce_mean(mask)
    mask = tf.compat.v2.where(
        condition = tf.math.is_nan(mask), x = 0., y = mask)
    loss = tf.abs(tf.subtract(pred, label))
    loss *= mask
    loss = tf.compat.v2.where(
        condition = tf.math.is_nan(loss), x = 0., y = loss)
    loss = tf.reduce_mean(loss)
    return loss
//  [python]
def dice_hard_coe(output, target, threshold=0.5, axis=(1, 2, 3), smooth=1e-5):
    """Non-differentiable Sørensen–Dice coefficient for comparing the similarity
    of two batch of data, usually be used for binary image segmentation i.e. labels are binary.
    The coefficient between 0 to 1, 1 if totally match.

    Parameters
    -----------
    output : tensor
        A distribution with shape: [batch_size, ....], (any dimensions).
    target : tensor
        The target distribution, format the same with `output`.
    threshold : float
        The threshold value to be true.
    axis : tuple of integer
        All dimensions are reduced, default ``(1,2,3)``.
    smooth : float
        This small value will be added to the numerator and denominator, see ``dice_coe``.

    References
    -----------
    - `Wiki-Dice <https://en.wikipedia.org/wiki/Sørensen–Dice_coefficient>`__

    """
    output = t.cast(output > threshold, dtype=t.float32)
    target = t.cast(target > threshold, dtype=t.float32)
    inse = t.reduce_sum(t.multiply(output, target), axis=axis)
    l = t.reduce_sum(output, axis=axis)
    r = t.reduce_sum(target, axis=axis)
    # old axis=[0,1,2,3]
    # hard_dice = 2 * (inse) / (l + r)
    # epsilon = 1e-5
    # hard_dice = t.clip_by_value(hard_dice, 0, 1.0-epsilon)
    # new haodong
    hard_dice = (2. * inse + smooth) / (l + r + smooth)
    ##
    hard_dice = t.reduce_mean(hard_dice, name='hard_dice')
    return hard_dice
//  [python]
def dice_hard_coe(output, target, threshold=0.5, axis=(1, 2, 3), smooth=1e-5):
    """Non-differentiable Sørensen–Dice coefficient for comparing the similarity
    of two batch of data, usually be used for binary image segmentation i.e. labels are binary.
    The coefficient between 0 to 1, 1 if totally match.

    Parameters
    -----------
    output : tensor
        A distribution with shape: [batch_size, ....], (any dimensions).
    target : tensor
        The target distribution, format the same with `output`.
    threshold : float
        The threshold value to be true.
    axis : tuple of integer
        All dimensions are reduced, default ``(1,2,3)``.
    smooth : float
        This small value will be added to the numerator and denominator, see ``dice_coe``.

    References
    -----------
    - `Wiki-Dice <https://en.wikipedia.org/wiki/Sørensen–Dice_coefficient>`__

    """
    output = tf.cast(output > threshold, dtype=tf.float32)
    target = tf.cast(target > threshold, dtype=tf.float32)
    inse = tf.reduce_sum(tf.multiply(output, target), axis=axis)
    l = tf.reduce_sum(output, axis=axis)
    r = tf.reduce_sum(target, axis=axis)
    # old axis=[0,1,2,3]
    # hard_dice = 2 * (inse) / (l + r)
    # epsilon = 1e-5
    # hard_dice = tf.clip_by_value(hard_dice, 0, 1.0-epsilon)
    # new haodong
    hard_dice = (2. * inse + smooth) / (l + r + smooth)
    ##
    hard_dice = tf.reduce_mean(hard_dice, name='hard_dice')
    return hard_dice
//  [python]
def iou_coe(output, target, threshold=0.5, axis=(1, 2, 3), smooth=1e-5):
    """Non-differentiable Intersection over Union (IoU) for comparing the
    similarity of two batch of data, usually be used for evaluating binary image segmentation.
    The coefficient between 0 to 1, and 1 means totally match.

    Parameters
    -----------
    output : tensor
        A batch of distribution with shape: [batch_size, ....], (any dimensions).
    target : tensor
        The target distribution, format the same with `output`.
    threshold : float
        The threshold value to be true.
    axis : tuple of integer
        All dimensions are reduced, default ``(1,2,3)``.
    smooth : float
        This small value will be added to the numerator and denominator, see ``dice_coe``.

    Notes
    ------
    - IoU cannot be used as training loss, people usually use dice coefficient for training, IoU and hard-dice for evaluating.

    """
    pre = t.cast(output > threshold, dtype=t.float32)
    truth = t.cast(target > threshold, dtype=t.float32)
    inse = tf.reduce_sum(tf.multiply(pre, truth), axis=axis)  # AND
    union = tf.reduce_sum(tf.cast(tf.add(pre, truth) >= 1, dtype=tf.float32), axis=axis)  # OR
    # old axis=[0,1,2,3]
    # epsilon = 1e-5
    # batch_iou = inse / (union + epsilon)
    # new haodong
    batch_iou = (inse + smooth) / (union + smooth)
    iou = tf.reduce_mean(batch_iou, name='iou_coe')
    return iou
//  [python]
def iou_coe(output, target, threshold=0.5, axis=(1, 2, 3), smooth=1e-5):
    """Non-differentiable Intersection over Union (IoU) for comparing the
    similarity of two batch of data, usually be used for evaluating binary image segmentation.
    The coefficient between 0 to 1, and 1 means totally match.

    Parameters
    -----------
    output : tensor
        A batch of distribution with shape: [batch_size, ....], (any dimensions).
    target : tensor
        The target distribution, format the same with `output`.
    threshold : float
        The threshold value to be true.
    axis : tuple of integer
        All dimensions are reduced, default ``(1,2,3)``.
    smooth : float
        This small value will be added to the numerator and denominator, see ``dice_coe``.

    Notes
    ------
    - IoU cannot be used as training loss, people usually use dice coefficient for training, IoU and hard-dice for evaluating.

    """
    pre = tf.cast(output > threshold, dtype=tf.float32)
    truth = tf.cast(target > threshold, dtype=tf.float32)
    inse = tf.reduce_sum(tf.multiply(pre, truth), axis=axis)  # AND
    union = tf.reduce_sum(tf.cast(tf.add(pre, truth) >= 1, dtype=tf.float32), axis=axis)  # OR
    # old axis=[0,1,2,3]
    # epsilon = 1e-5
    # batch_iou = inse / (union + epsilon)
    # new haodong
    batch_iou = (inse + smooth) / (union + smooth)
    iou = tf.reduce_mean(batch_iou, name='iou_coe')
    return iou
//  [python]
def add_evaluation_step(result_tensor, ground_truth_tensor):
    """Inserts the operations we need to evaluate the accuracy of our results.

    Args:
      result_tensor: The new final node that produces results.
      ground_truth_tensor: The node we feed ground truth data
      into.

    Returns:
      Tuple of (evaluation step, prediction).
    """
    with tf.name_scope('accuracy'):
        with tf.name_scope('correct_prediction'):
            prediction = tf.argmax(result_tensor, 1)
            correct_prediction = tf.equal(prediction, ground_truth_tensor)
        with tf.name_scope('accuracy'):
            evaluation_step = tf.reduce_mean(tf.to_float(correct_prediction))
    tf.summary.scalar('accuracy', evaluation_step)
    return evaluation_step, prediction
//  [python]
def add_evaluation_step(result_tensor, ground_truth_tensor):
    """Inserts the operations we need to evaluate the accuracy of our results.

    Args:
      result_tensor: The new final node that produces results.
      ground_truth_tensor: The node we feed ground truth data
      into.

    Returns:
      Tuple of (evaluation step, prediction).
    """
    with tf.name_scope('accuracy'):
        with tf.name_scope('correct_prediction'):
            prediction = tf.argmax(result_tensor, 1)
            correct_prediction = tf.equal(prediction, ground_truth_tensor)
        with tf.name_scope('accuracy'):
            evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar('accuracy', evaluation_step)
    return evaluation_step, prediction
//  [python]
def __init__(self,
               batch,
               state_name=None,
               tokens=None,
               num_states=0,
               eos_id=None):
    """Construct VatxtInput.

    Args:
      batch: NextQueuedSequenceBatch.
      state_name: str, name of state to fetch and save.
      tokens: int Tensor, tokens. Defaults to batch's F_TOKEN_ID sequence.
      num_states: int The number of states to store.
      eos_id: int Id of end of Sequence.
    """
    self._batch = batch
    self._state_name = state_name
    self._tokens = (tokens if tokens is not None else
                    batch.sequences[data_utils.SequenceWrapper.F_TOKEN_ID])
    self._num_states = num_states

    w = batch.sequences[data_utils.SequenceWrapper.F_WEIGHT]
    self._weights = w

    l = batch.sequences[data_utils.SequenceWrapper.F_LABEL]
    self._labels = l

    # eos weights
    self._eos_weights = None
    if eos_id:
      ew = t.cast(t.equal(self._tokens, eos_id), t.float32)
      self._eos_weights = ew
//  [python]
def __init__(self,
               batch,
               state_name=None,
               tokens=None,
               num_states=0,
               eos_id=None):
    """Construct VatxtInput.

    Args:
      batch: NextQueuedSequenceBatch.
      state_name: str, name of state to fetch and save.
      tokens: int Tensor, tokens. Defaults to batch's F_TOKEN_ID sequence.
      num_states: int The number of states to store.
      eos_id: int Id of end of Sequence.
    """
    self._batch = batch
    self._state_name = state_name
    self._tokens = (tokens if tokens is not None else
                    batch.sequences[data_utils.SequenceWrapper.F_TOKEN_ID])
    self._num_states = num_states

    w = batch.sequences[data_utils.SequenceWrapper.F_WEIGHT]
    self._weights = w

    l = batch.sequences[data_utils.SequenceWrapper.F_LABEL]
    self._labels = l

    # eos weights
    self._eos_weights = None
    if eos_id:
      ew = tf.cast(tf.equal(self._tokens, eos_id), tf.float32)
      self._eos_weights = ew
//  [python]
def _loss_location(self, pred_offset, gt_offset, positiveness):

        positiveness = tf.expand_dims(positiveness, axis=-1)

        # get postive indices
        pos_indices = tf.where(positiveness == 1)
        pred_offset = tf.gather_nd(pred_offset, pos_indices[:, :-1])
        gt_offset = tf.gather_nd(gt_offset, pos_indices[:, :-1])

        # calculate the smoothL1(positive_pred, positive_gt) and return
        num_pos = tf.shape(gt_offset)[0]

        # calculate smoothL1 loss
        regression_diff = tf.abs(gt_offset - pred_offset)
        regression_loss = tf.where(
            tf.less(regression_diff, 1.0),
            0.5 * tf.pow(regression_diff, 2),
            regression_diff - 0.5
        )

        return tf.reduce_sum(regression_loss) / tf.to_float(num_pos)
//  [python]
def _loss_location(self, pred_offset, gt_offset, positiveness):

        positiveness = tf.expand_dims(positiveness, axis=-1)

        # get postive indices
        pos_indices = tf.where(positiveness == 1)
        pred_offset = tf.gather_nd(pred_offset, pos_indices[:, :-1])
        gt_offset = tf.gather_nd(gt_offset, pos_indices[:, :-1])

        # calculate the smoothL1(positive_pred, positive_gt) and return
        num_pos = tf.shape(gt_offset)[0]

        # calculate smoothL1 loss
        regression_diff = tf.abs(gt_offset - pred_offset)
        regression_loss = tf.where(
            tf.less(regression_diff, 1.0),
            0.5 * tf.pow(regression_diff, 2),
            regression_diff - 0.5
        )

        return tf.reduce_sum(regression_loss) / tf.cast(num_pos, regression_loss.dtype)
//  [python]
def add_jpeg_decoding(module_spec):
    """Adds operations that perform JPEG decoding and resizing to the graph..

    Args:
      module_spec: The hub.ModuleSpec for the image module being used.

    Returns:
      Tensors for the node to feed JPEG data into, and the output of the
        preprocessing steps.
    """
    input_height, input_width = hub.get_expected_image_size(module_spec)
    input_depth = hub.get_num_image_channels(module_spec)
    jpeg_data = tf.placeholder(tf.string, name='DecodeJPGInput')
    decoded_image = tf.image.decode_jpeg(jpeg_data, channels=input_depth)
    # Convert from full range of uint8 to range [0,1] of float32.
    decoded_image_as_float = tf.image.convert_image_dtype(decoded_image,
                                                          tf.float32)
    decoded_image_4d = tf.expand_dims(decoded_image_as_float, 0)
    resize_shape = tf.stack([input_height, input_width])
    resize_shape_as_int = tf.cast(resize_shape, dtype=tf.int32)
    resized_image = tf.image.resize_bilinear(decoded_image_4d,
                                             resize_shape_as_int)
    return jpeg_data, resized_image
//  [python]
def call(self, inputs, step_type, network_state=()):
    del step_type
    observation, actions = inputs
    actions = t.cast(tf.nest.flatten(actions)[0], tf.float32)

    states = t.cast(tf.nest.flatten(observation)[0], tf.float32)

    s_value = self._value_layer(states)
    if self._shared_layer:
      s_value = self._shared_layer(s_value)
    a_value = self._action_layer(actions)
    # Biggest state is best state.
    q_value = tf.reshape(s_value + a_value, [-1])
    return q_value, network_state
//  [python]
def call(self, inputs, step_type, network_state=()):
    del step_type
    observation, actions = inputs
    actions = tf.cast(tf.nest.flatten(actions)[0], tf.float32)

    states = tf.cast(tf.nest.flatten(observation)[0], tf.float32)

    s_value = self._value_layer(states)
    if self._shared_layer:
      s_value = self._shared_layer(s_value)
    a_value = self._action_layer(actions)
    # Biggest state is best state.
    q_value = tf.reshape(s_value + a_value, [-1])
    return q_value, network_state
//  [python]
def update_state(self, y_true, y_pred, sample_weight=None):
    y_true = t.cast(y_true, 'float32')
    y_pred = t.cast(y_pred, 'float32')

    if len(y_true.shape) == 2:
      reduce_axes = 0
    else:
      reduce_axes = [0,1]

    product = tf.reduce_sum(tf.multiply(y_true, y_pred), axis=reduce_axes)
    self._product.assign_add(product)

    true_sum = tf.reduce_sum(y_true, axis=reduce_axes)
    self._true_sum.assign_add(true_sum)

    true_sumsq = tf.reduce_sum(tf.math.square(y_true), axis=reduce_axes)
    self._true_sumsq.assign_add(true_sumsq)

    pred_sum = tf.reduce_sum(y_pred, axis=reduce_axes)
    self._pred_sum.assign_add(pred_sum)

    pred_sumsq = tf.reduce_sum(tf.math.square(y_pred), axis=reduce_axes)
    self._pred_sumsq.assign_add(pred_sumsq)

    count = tf.ones_like(y_true)
    count = tf.reduce_sum(count, axis=reduce_axes)
    self._count.assign_add(count)
//  [python]
def update_state(self, y_true, y_pred, sample_weight=None):
    y_true = tf.cast(y_true, 'float32')
    y_pred = tf.cast(y_pred, 'float32')

    if len(y_true.shape) == 2:
      reduce_axes = 0
    else:
      reduce_axes = [0,1]

    product = tf.reduce_sum(tf.multiply(y_true, y_pred), axis=reduce_axes)
    self._product.assign_add(product)

    true_sum = tf.reduce_sum(y_true, axis=reduce_axes)
    self._true_sum.assign_add(true_sum)

    true_sumsq = tf.reduce_sum(tf.math.square(y_true), axis=reduce_axes)
    self._true_sumsq.assign_add(true_sumsq)

    pred_sum = tf.reduce_sum(y_pred, axis=reduce_axes)
    self._pred_sum.assign_add(pred_sum)

    pred_sumsq = tf.reduce_sum(tf.math.square(y_pred), axis=reduce_axes)
    self._pred_sumsq.assign_add(pred_sumsq)

    count = tf.ones_like(y_true)
    count = tf.reduce_sum(count, axis=reduce_axes)
    self._count.assign_add(count)
//  [python]
def _loss_class(self, pred_cls, gt_cls, num_cls, positiveness):

        # reshape pred_cls from [batch, num_anchor, num_cls] => [batch * num_anchor, num_cls]
        pred_cls = tf.reshape(pred_cls, [-1, num_cls])
        pred_cls_max = tf.reduce_max(tf.reduce_max(pred_cls, axis=-1))
        logsumexp_pred_cls = tf.math.log(
            tf.reduce_sum(tf.math.exp(pred_cls - pred_cls_max), -1)) + pred_cls_max - pred_cls[:, 0]

        logsumexp_pred_cls = tf.reshape(logsumexp_pred_cls, [tf.shape(gt_cls)[0], -1])
        non_neg_mask = tf.to_float(tf.logical_not(gt_cls != 0))
        logsumexp_pred_cls = logsumexp_pred_cls * non_neg_mask

        idx = tf.argsort(logsumexp_pred_cls, axis=1, direction="DESCENDING")
        idx_rank = tf.argsort(idx, axis=1)

        num_pos = tf.expand_dims(
            tf.reduce_sum(tf.to_int32((positiveness == 1)), axis=-1), axis=-1)
        num_neg = tf.clip_by_value(num_pos * 3, clip_value_min=0,
                                   clip_value_max=tf.shape(positiveness)[-1] - 1)

        negative_bool = tf.broadcast_to((idx_rank < num_neg), tf.shape(idx_rank))
        negative_bool = tf.to_float(negative_bool) * non_neg_mask

        idx_pos = tf.where(positiveness == 1)
        idx_neg = tf.where(negative_bool == 1)
        idxes = tf.concat([idx_pos, idx_neg], axis=0)

        pred_cls = tf.reshape(pred_cls, [-1, tf.shape(positiveness)[-1], num_cls])
        pred_selected = tf.gather_nd(pred_cls, idxes)
        gt_selected = tf.gather_nd(gt_cls, idxes)
        gt_selected = tf.one_hot(tf.to_int32(gt_selected), depth=num_cls)

        loss_conf = tf.nn.softmax_cross_entropy_with_logits(gt_selected, pred_selected)
        loss_conf = tf.reduce_sum(loss_conf) / tf.to_float(tf.reduce_sum(num_pos))
        return loss_conf
//  [python]
def _loss_class(self, pred_cls, gt_cls, num_cls, positiveness):

        # reshape pred_cls from [batch, num_anchor, num_cls] => [batch * num_anchor, num_cls]
        pred_cls = tf.reshape(pred_cls, [-1, num_cls])
        pred_cls_max = tf.reduce_max(tf.reduce_max(pred_cls, axis=-1))
        logsumexp_pred_cls = tf.math.log(
            tf.reduce_sum(tf.math.exp(pred_cls - pred_cls_max), -1)) + pred_cls_max - pred_cls[:, 0]

        logsumexp_pred_cls = tf.reshape(logsumexp_pred_cls, [tf.shape(gt_cls)[0], -1])
        non_neg_mask = tf.cast(tf.logical_not(gt_cls != 0), tf.float32)
        logsumexp_pred_cls = logsumexp_pred_cls * non_neg_mask

        idx = tf.argsort(logsumexp_pred_cls, axis=1, direction="DESCENDING")
        idx_rank = tf.argsort(idx, axis=1)

        num_pos = tf.expand_dims(
            tf.reduce_sum(tf.cast((positiveness == 1), tf.int32), axis=-1), axis=-1)
        num_neg = tf.clip_by_value(num_pos * 3, clip_value_min=0,
                                   clip_value_max=tf.shape(positiveness)[-1] - 1)

        negative_bool = tf.broadcast_to((idx_rank < num_neg), tf.shape(idx_rank))
        negative_bool = tf.cast(negative_bool, logsumexp_pred_cls.dtype) * non_neg_mask

        idx_pos = tf.where(positiveness == 1)
        idx_neg = tf.where(negative_bool == 1)
        idxes = tf.concat([idx_pos, idx_neg], axis=0)

        pred_cls = tf.reshape(pred_cls, [-1, tf.shape(positiveness)[-1], num_cls])
        pred_selected = tf.gather_nd(pred_cls, idxes)
        gt_selected = tf.gather_nd(gt_cls, idxes)
        gt_selected = tf.one_hot(tf.cast(gt_selected, tf.int32), depth=num_cls)

        loss_conf = tf.nn.softmax_cross_entropy_with_logits(gt_selected, pred_selected)
        loss_conf = tf.reduce_sum(loss_conf) / tf.cast(tf.reduce_sum(num_pos), loss_conf.dtype)
        return loss_conf
//  [python]
def _compute_group_loss(self, sample_loss, group_identity):
    """Calculate subgroup losses.

    Args:
       sample_loss: Tensor of [num_queries] representing loss for each query.
       group_identity: Tensor of [num_queries] representing the group identity
         for each query.

    Returns:
       group_loss: Tensor of group loss values.
       group_mask: Tensor of [num_groups, num_queries].
    """
    # Shape of group_mask: [num_groups, num_queries].
    group_mask = t.cast(
        t.equal(group_identity, self._group_labels_matrix), dtype="float32")
    group_cnts = t.reduce_sum(group_mask, axis=1)
    # Avoid divide by zero.
    group_cnts += t.cast(group_cnts == 0, dtype="float32")
    # group loss shape: [num_groups]
    group_loss = t.divide(
        t.reduce_sum(group_mask * sample_loss, axis=1), group_cnts)
    return group_loss, group_mask
//  [python]
def _compute_group_loss(self, sample_loss, group_identity):
    """Calculate subgroup losses.

    Args:
       sample_loss: Tensor of [num_queries] representing loss for each query.
       group_identity: Tensor of [num_queries] representing the group identity
         for each query.

    Returns:
       group_loss: Tensor of group loss values.
       group_mask: Tensor of [num_groups, num_queries].
    """
    # Shape of group_mask: [num_groups, num_queries].
    group_mask = tf.cast(
        tf.equal(group_identity, self._group_labels_matrix), dtype="float32")
    group_cnts = tf.reduce_sum(group_mask, axis=1)
    # Avoid divide by zero.
    group_cnts += tf.cast(group_cnts == 0, dtype="float32")
    # group loss shape: [num_groups]
    group_loss = tf.divide(
        tf.reduce_sum(group_mask * sample_loss, axis=1), group_cnts)
    return group_loss, group_mask
//  [python]
def _loss_semantic_segmentation(self, pred_seg, mask_gt, classes, num_obj):

        shape_mask = tf.shape(mask_gt)
        num_batch = shape_mask[0]
        seg_shape = tf.shape(pred_seg)[1]
        loss_seg = 0.

        for idx in tf.range(num_batch):
            seg = pred_seg[idx]
            masks = mask_gt[idx]
            cls = classes[idx]
            objects = num_obj[idx]

            # seg shape (69, 69, num_cls)
            # resize masks from (100, 138, 138) to (100, 69, 69)
            masks = tf.expand_dims(masks, axis=-1)
            masks = tf.image.resize(masks, [seg_shape, seg_shape], method=tf.image.ResizeMethod.BILINEAR)
            masks = tf.to_float(tf.cast((masks > 0.5), tf.bool))
            masks = tf.squeeze(masks)

            # obj_mask shape (objects, 138, 138)
            obj_mask = masks[:objects]
            obj_cls = tf.expand_dims(cls[:objects], axis=-1)

            # create empty ground truth (138, 138, num_cls)
            seg_gt = tf.zeros_like(seg)
            seg_gt = tf.transpose(seg_gt, perm=(2, 0, 1))
            seg_gt = tf.tensor_scatter_nd_add(seg_gt, indices=obj_cls, updates=obj_mask)
            seg_gt = tf.transpose(seg_gt, perm=(1, 2, 0))
            loss_seg += tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(seg_gt, seg))
        loss_seg = loss_seg / tf.to_float(tf.cast(seg_shape, tf.int32)) ** 2 / tf.to_float(tf.cast(num_batch, tf.int32))

        return loss_seg
//  [python]
def _loss_semantic_segmentation(self, pred_seg, mask_gt, classes, num_obj):

        shape_mask = tf.shape(mask_gt)
        num_batch = shape_mask[0]
        seg_shape = tf.shape(pred_seg)[1]
        loss_seg = 0.

        for idx in tf.range(num_batch):
            seg = pred_seg[idx]
            masks = mask_gt[idx]
            cls = classes[idx]
            objects = num_obj[idx]

            # seg shape (69, 69, num_cls)
            # resize masks from (100, 138, 138) to (100, 69, 69)
            masks = tf.expand_dims(masks, axis=-1)
            masks = tf.image.resize(masks, [seg_shape, seg_shape], method=tf.image.ResizeMethod.BILINEAR)
            masks = tf.cast((masks > 0.5), seg.dtype)
            masks = tf.squeeze(masks)

            # obj_mask shape (objects, 138, 138)
            obj_mask = masks[:objects]
            obj_cls = tf.expand_dims(cls[:objects], axis=-1)

            # create empty ground truth (138, 138, num_cls)
            seg_gt = tf.zeros_like(seg)
            seg_gt = tf.transpose(seg_gt, perm=(2, 0, 1))
            seg_gt = tf.tensor_scatter_nd_add(seg_gt, indices=obj_cls, updates=obj_mask)
            seg_gt = tf.transpose(seg_gt, perm=(1, 2, 0))
            loss_seg += tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(seg_gt, seg))
        loss_seg = loss_seg / tf.cast(seg_shape, pred_seg.dtype) ** 2 / tf.cast(num_batch, pred_seg.dtype)

        return loss_seg
//  [python]
def model_fn(features, labels, mode):
    # Build the neural network
    logits = neural_net(features)

    # Predictions
    pred_classes = tf.argmax(logits, axis=1)
    pred_probas = tf.nn.softmax(logits)

    # If prediction mode, early return
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)

    # Define loss and optimizer
    loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits, labels=tf.to_int32(labels)))
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
    train_op = optimizer.minimize(loss_op,
                                  global_step=tf.train.get_global_step())

    # Evaluate the accuracy of the model
    acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)

    # TF Estimators requires to return a EstimatorSpec, that specify
    # the different ops for training, evaluating, ...
    estim_specs = tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=pred_classes,
        loss=loss_op,
        train_op=train_op,
        eval_metric_ops={'accuracy': acc_op})

    return estim_specs
//  [python]
def model_fn(features, labels, mode):
    # Build the neural network
    logits = neural_net(features)

    # Predictions
    pred_classes = tf.argmax(logits, axis=1)
    pred_probas = tf.nn.softmax(logits)

    # If prediction mode, early return
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)

    # Define loss and optimizer
    loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits, labels=tf.cast(labels, dtype=tf.int32)))
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
    train_op = optimizer.minimize(loss_op,
                                  global_step=tf.train.get_global_step())

    # Evaluate the accuracy of the model
    acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)

    # TF Estimators requires to return a EstimatorSpec, that specify
    # the different ops for training, evaluating, ...
    estim_specs = tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=pred_classes,
        loss=loss_op,
        train_op=train_op,
        eval_metric_ops={'accuracy': acc_op})

    return estim_specs
//  [python]
def finalize(self, outputs, final_state, sequence_lengths):
        """Finalize and return the predicted_ids.

        Args:
          outputs: An instance of BeamSearchDecoderOutput.
          final_state: An instance of BeamSearchDecoderState. Passed through to
            the output.
          sequence_lengths: An `int64` tensor shaped
            `[batch_size, beam_width]`. The sequence lengths determined for
            each beam during decode. **NOTE** These are ignored; the updated
            sequence lengths are stored in `final_state.lengths`.

        Returns:
          outputs: An instance of `FinalBeamSearchDecoderOutput` where the
            predicted_ids are the result of calling _gather_tree.
          final_state: The same input instance of `BeamSearchDecoderState`.
        """
        del sequence_lengths
        # Get max_sequence_length across all beams for each batch.
        max_sequence_lengths = t.cast(
            t.reduce_max(final_state.lengths, axis=1), t.int32
        )
        predicted_ids = gather_tree(
            outputs.predicted_ids,
            outputs.parent_ids,
            max_sequence_lengths=max_sequence_lengths,
            end_token=self._end_token,
        )
        if self._reorder_tensor_arrays:
            final_state = final_state._replace(
                cell_state=tf.nest.map_structure(
                    lambda t: self._maybe_sort_array_beams(
                        t, outputs.parent_ids, final_state.lengths
                    ),
                    final_state.cell_state,
                )
            )
        outputs = FinalBeamSearchDecoderOutput(
            beam_search_decoder_output=outputs, predicted_ids=predicted_ids
        )
        return outputs, final_state
//  [python]
def finalize(self, outputs, final_state, sequence_lengths):
        """Finalize and return the predicted_ids.

        Args:
          outputs: An instance of BeamSearchDecoderOutput.
          final_state: An instance of BeamSearchDecoderState. Passed through to
            the output.
          sequence_lengths: An `int64` tensor shaped
            `[batch_size, beam_width]`. The sequence lengths determined for
            each beam during decode. **NOTE** These are ignored; the updated
            sequence lengths are stored in `final_state.lengths`.

        Returns:
          outputs: An instance of `FinalBeamSearchDecoderOutput` where the
            predicted_ids are the result of calling _gather_tree.
          final_state: The same input instance of `BeamSearchDecoderState`.
        """
        del sequence_lengths
        # Get max_sequence_length across all beams for each batch.
        max_sequence_lengths = tf.cast(
            tf.reduce_max(final_state.lengths, axis=1), tf.int32
        )
        predicted_ids = gather_tree(
            outputs.predicted_ids,
            outputs.parent_ids,
            max_sequence_lengths=max_sequence_lengths,
            end_token=self._end_token,
        )
        if self._reorder_tensor_arrays:
            final_state = final_state._replace(
                cell_state=tf.nest.map_structure(
                    lambda t: self._maybe_sort_array_beams(
                        t, outputs.parent_ids, final_state.lengths
                    ),
                    final_state.cell_state,
                )
            )
        outputs = FinalBeamSearchDecoderOutput(
            beam_search_decoder_output=outputs, predicted_ids=predicted_ids
        )
        return outputs, final_state
//  [python]
def _length_penalty(sequence_lengths, penalty_factor):
    """Calculates the length penalty. See https://arxiv.org/abs/1609.08144.

    Returns the length penalty tensor:
    
    [(5+sequence_lengths)/6]**penalty_factor
    
    where all operations are performed element-wise.

    Args:
      sequence_lengths: `Tensor`, the sequence lengths of each hypotheses.
      penalty_factor: A scalar that weights the length penalty.

    Returns:
      If the penalty is `0`, returns the scalar `1.0`.  Otherwise returns
      the length penalty factor, a tensor with the same shape as
      `sequence_lengths`.
    """
    penalty_factor = tf.convert_to_tensor(penalty_factor, name="penalty_factor")
    penalty_factor.set_shape(())  # penalty should be a scalar.
    static_penalty = tf.get_static_value(penalty_factor)
    if static_penalty is not None and static_penalty == 0:
        return 1.0
    return tf.math.divide(
        (5.0 + tf.to_float(sequence_lengths)) ** penalty_factor,
        (5.0 + 1.0) ** penalty_factor,
    )
//  [python]
def _length_penalty(sequence_lengths, penalty_factor):
    """Calculates the length penalty. See https://arxiv.org/abs/1609.08144.

    Returns the length penalty tensor:
    ```
    [(5+sequence_lengths)/6]**penalty_factor
    ```
    where all operations are performed element-wise.

    Args:
      sequence_lengths: `Tensor`, the sequence lengths of each hypotheses.
      penalty_factor: A scalar that weights the length penalty.

    Returns:
      If the penalty is `0`, returns the scalar `1.0`.  Otherwise returns
      the length penalty factor, a tensor with the same shape as
      `sequence_lengths`.
    """
    penalty_factor = tf.convert_to_tensor(penalty_factor, name="penalty_factor")
    penalty_factor.set_shape(())  # penalty should be a scalar.
    static_penalty = tf.get_static_value(penalty_factor)
    if static_penalty is not None and static_penalty == 0:
        return 1.0
    return tf.math.divide(
        (5.0 + tf.cast(sequence_lengths, tf.float32)) ** penalty_factor,
        (5.0 + 1.0) ** penalty_factor,
    )