jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def _create_model(self):
        # input features
        with tf.variable_scope('input_layer'):
            self._dense_input = transform_numeric(self._dense_features)
            dense_input = tf.concat(self._dense_input, axis=1)
            self._sparse_input = transform_categorical(self._sparse_features)
            sparse_input = tf.concat(self._sparse_input, axis=1)
            dnn_input = tf.concat([dense_input, sparse_input], axis=1)
            wide_input = tf.concat([dense_input, sparse_input], axis=1)
            fm_input = tf.stack(self._sparse_input, axis=1)

        if self.bf16:
            wide_input = tf.cast(wide_input, dtype=tf.bfloat16)
            fm_input = tf.cast(fm_input, dtype=tf.bfloat16)
            dnn_input = tf.cast(dnn_input, dtype=tf.bfloat16)

        # DNN part
        dnn_scope = tf.variable_scope('dnn')
        with dnn_scope.keep_weights(dtype=tf.float32) if self.bf16 \
                else dnn_scope:
            dnn_output = self._dnn(dnn_input, self._dnn_hidden_units,
                                   'dnn_layer')

        # linear / fisrt order part
        with tf.variable_scope('linear', reuse=tf.AUTO_REUSE) as linear:
            linear_output = tf.reduce_sum(wide_input, axis=1, keepdims=True)

        # FM second order part
        with tf.variable_scope('fm', reuse=tf.AUTO_REUSE) as fm:
            sum_square = tf.square(tf.reduce_sum(fm_input, axis=1))
            square_sum = tf.reduce_sum(tf.square(fm_input), axis=1)
            fm_output = 0.5 * tf.subtract(sum_square, square_sum)

        # Final dnn layer
        all_input = tf.concat([dnn_output, linear_output, fm_output], 1)
        final_dnn_scope = tf.variable_scope('final_dnn')
        with final_dnn_scope.keep_weights(dtype=tf.float32) if self.bf16 \
                else final_dnn_scope:
            dnn_logits = self._dnn(all_input, self._final_hidden_units, 'final_dnn')

        if self.bf16:
            dnn_logits = tf.cast(dnn_logits, dtype=tf.float32)

        self._logits = tf.layers.dense(dnn_logits, units=1)
        self.probability = tf.math.sigmoid(self._logits)
        self.output = tf.round(self.probability)
//  [python]
def call(self, y_true, y_pred):
        y_true = t.cast(y_true, t.int32)
        mask = tf.math.logical_not(tf.math.equal(y_true, par.pad_token))
        mask = t.cast(mask, t.float32)
        _loss = super(TransformerLoss, self).call(y_true, y_pred)
        _loss *= mask
        if self.debug:
            tf.print('loss shape:', _loss.shape, output_stream=sys.stdout)
            tf.print('output:', tf.argmax(y_pred,-1), output_stream=sys.stdout)
            tf.print(mask, output_stream=sys.stdout)
            tf.print(_loss, output_stream=sys.stdout)
        return _loss
//  [python]
def call(self, y_true, y_pred):
        y_true = tf.cast(y_true, tf.int32)
        mask = tf.math.logical_not(tf.math.equal(y_true, par.pad_token))
        mask = tf.cast(mask, tf.float32)
        _loss = super(TransformerLoss, self).call(y_true, y_pred)
        _loss *= mask
        if self.debug:
            tf.print('loss shape:', _loss.shape, output_stream=sys.stdout)
            tf.print('output:', tf.argmax(y_pred,-1), output_stream=sys.stdout)
            tf.print(mask, output_stream=sys.stdout)
            tf.print(_loss, output_stream=sys.stdout)
        return _loss
//  [python]
def create_attention_mask_from_input_mask(from_tensor, to_mask):
  """Create 3D attention mask from a 2D tensor mask.

  Args:
    from_tensor: 2D or 3D Tensor of shape [batch_size, from_seq_length, ...].
    to_mask: int32 Tensor of shape [batch_size, to_seq_length].

  Returns:
    float Tensor of shape [batch_size, from_seq_length, to_seq_length].
  """
  to_mask = tf.to_float(to_mask)

  from_shape = get_shape_list(from_tensor, expected_rank=[2, 3])
  batch_size = from_shape[0]

  to_shape = get_shape_list(to_mask, expected_rank=2)
  to_seq_length = to_shape[1]

  to_mask = tf.reshape(to_mask, [batch_size, 1, to_seq_length])
  # The mask will be automatically broadcasted to
  # [batch_size, from_seq_length, to_seq_length] when it is used in the
  # attention layer.
  return to_mask
//  [python]
def create_attention_mask_from_input_mask(from_tensor, to_mask):
  """Create 3D attention mask from a 2D tensor mask.

  Args:
    from_tensor: 2D or 3D Tensor of shape [batch_size, from_seq_length, ...].
    to_mask: int32 Tensor of shape [batch_size, to_seq_length].

  Returns:
    float Tensor of shape [batch_size, from_seq_length, to_seq_length].
  """
  to_mask = tf.cast(to_mask, dtype=tf.float32)

  from_shape = get_shape_list(from_tensor, expected_rank=[2, 3])
  batch_size = from_shape[0]

  to_shape = get_shape_list(to_mask, expected_rank=2)
  to_seq_length = to_shape[1]

  to_mask = tf.reshape(to_mask, [batch_size, 1, to_seq_length])
  # The mask will be automatically broadcasted to
  # [batch_size, from_seq_length, to_seq_length] when it is used in the
  # attention layer.
  return to_mask
//  [python]
def add_packet_loss_noise(wave_tensor, max_num_bursts, max_length_rate):
  """Simulate burst packet loss noise deterministically to the input `wave_tensor`.

  Args:
    wave_tensor: The input waveform signals to add noise to.
    max_num_bursts: The number of burst losses to simulate.
    max_length_rate: The maximum ratio of each burst loss length to the input
      signal length. The ratio is uniformly sampled for each burst loss in range
      (0, max_rength_rate].

  Returns:
    Noise injected waveform signals.
  """
  positive_sum = t.cast(t.reduce_sum(t.maximum(wave_tensor, 0)), t.int32)
  negative_sum = t.cast(t.reduce_sum(t.maximum(-wave_tensor, 0)), t.int32)

  max_length = t.cast(
      t.round(t.cast(t.size(wave_tensor), t.float32) * max_length_rate),
      t.int32)

  mask_lengths = t.random.stateless_uniform([max_num_bursts],
                                             [positive_sum, negative_sum],
                                             1,
                                             max_length + 1,
                                             dtype=t.int32)

  mask_start_idxs = t.math.floormod(
      t.abs(
          t.random.stateless_uniform([max_num_bursts],
                                      [positive_sum + 1, negative_sum + 1],
                                      None,
                                      None,
                                      dtype=t.int32)),
      t.size(wave_tensor) - mask_lengths)

  mask_end_idxs = mask_start_idxs + mask_lengths

  timestamps = t.repeat(
      t.range(t.size(wave_tensor))[None, :], max_num_bursts, 0)

  time_mask = t.logical_and(
      t.greater_equal(timestamps, mask_start_idxs[:, None]),
      t.less(timestamps, mask_end_idxs[:, None]))
  time_mask = t.cast(t.reduce_any(time_mask, 0), t.float32)

  noised_wave_tensor = wave_tensor * (1 - time_mask)

  return noised_wave_tensor
//  [python]
def add_packet_loss_noise(wave_tensor, max_num_bursts, max_length_rate):
  """Simulate burst packet loss noise deterministically to the input `wave_tensor`.

  Args:
    wave_tensor: The input waveform signals to add noise to.
    max_num_bursts: The number of burst losses to simulate.
    max_length_rate: The maximum ratio of each burst loss length to the input
      signal length. The ratio is uniformly sampled for each burst loss in range
      (0, max_rength_rate].

  Returns:
    Noise injected waveform signals.
  """
  positive_sum = tf.cast(tf.reduce_sum(tf.maximum(wave_tensor, 0)), tf.int32)
  negative_sum = tf.cast(tf.reduce_sum(tf.maximum(-wave_tensor, 0)), tf.int32)

  max_length = tf.cast(
      tf.round(tf.cast(tf.size(wave_tensor), tf.float32) * max_length_rate),
      tf.int32)

  mask_lengths = tf.random.stateless_uniform([max_num_bursts],
                                             [positive_sum, negative_sum],
                                             1,
                                             max_length + 1,
                                             dtype=tf.int32)

  mask_start_idxs = tf.math.floormod(
      tf.abs(
          tf.random.stateless_uniform([max_num_bursts],
                                      [positive_sum + 1, negative_sum + 1],
                                      None,
                                      None,
                                      dtype=tf.int32)),
      tf.size(wave_tensor) - mask_lengths)

  mask_end_idxs = mask_start_idxs + mask_lengths

  timestamps = tf.repeat(
      tf.range(tf.size(wave_tensor))[None, :], max_num_bursts, 0)

  time_mask = tf.logical_and(
      tf.greater_equal(timestamps, mask_start_idxs[:, None]),
      tf.less(timestamps, mask_end_idxs[:, None]))
  time_mask = tf.cast(tf.reduce_any(time_mask, 0), tf.float32)

  noised_wave_tensor = wave_tensor * (1 - time_mask)

  return noised_wave_tensor
//  [python]
def add_environment_noise(wave_tensor, snr, noise_db):
  """Adds random environment noise deterministically chosen from `noise_db`.

  Args:
    wave_tensor: The input waveform signals to add noise to.
    snr: The signal to noise ratio for controlling the power of the noise.
    noise_db: A two dimensional tensor where the first dimension corresponds to
      the number of noise on which we sample the random noise.

  Returns:
    Noise injected waveform signals.
  """

  positive_sum = t.cast(t.reduce_sum(t.maximum(wave_tensor, 0)), t.int32)
  negative_sum = t.cast(t.reduce_sum(t.maximum(-wave_tensor, 0)), t.int32)

  snr = t.cast(snr, wave_tensor.dtype)

  noise_idx = t.random.stateless_uniform([], [positive_sum, negative_sum], 0,
                                          t.shape(noise_db)[0], t.int32)
  target_noise = t.cast(noise_db[noise_idx], wave_tensor.dtype)

  pos = t.reduce_mean(t.square(wave_tensor))
  if t.size(target_noise) >= t.size(wave_tensor):
    start_idx = t.random.stateless_uniform(
        [], [positive_sum + 1, negative_sum + 1], 0,
        t.size(target_noise) - t.size(wave_tensor) + 1, t.int32)
    target_noise = target_noise[start_idx:start_idx + t.size(wave_tensor)]
    nos = t.reduce_mean(t.square(target_noise))
  else:
    start_idx = t.random.stateless_uniform(
        [], [positive_sum + 1, negative_sum + 1], 0,
        t.size(wave_tensor) - t.size(target_noise) + 1, t.int32)
    nos = t.reduce_mean(t.square(target_noise))
    target_noise = t.pad(target_noise, [[
        start_idx,
        t.size(wave_tensor) - (start_idx + t.size(target_noise))
    ]])

  multiplier = t.math.divide_no_nan(
      1., t.sqrt(t.math.divide_no_nan(nos * snr, pos)))
  wave_tensor = wave_tensor + target_noise * multiplier

  return wave_tensor
//  [python]
def add_environment_noise(wave_tensor, snr, noise_db):
  """Adds random environment noise deterministically chosen from `noise_db`.

  Args:
    wave_tensor: The input waveform signals to add noise to.
    snr: The signal to noise ratio for controlling the power of the noise.
    noise_db: A two dimensional tensor where the first dimension corresponds to
      the number of noise on which we sample the random noise.

  Returns:
    Noise injected waveform signals.
  """

  positive_sum = tf.cast(tf.reduce_sum(tf.maximum(wave_tensor, 0)), tf.int32)
  negative_sum = tf.cast(tf.reduce_sum(tf.maximum(-wave_tensor, 0)), tf.int32)

  snr = tf.cast(snr, wave_tensor.dtype)

  noise_idx = tf.random.stateless_uniform([], [positive_sum, negative_sum], 0,
                                          tf.shape(noise_db)[0], tf.int32)
  target_noise = tf.cast(noise_db[noise_idx], wave_tensor.dtype)

  pos = tf.reduce_mean(tf.square(wave_tensor))
  if tf.size(target_noise) >= tf.size(wave_tensor):
    start_idx = tf.random.stateless_uniform(
        [], [positive_sum + 1, negative_sum + 1], 0,
        tf.size(target_noise) - tf.size(wave_tensor) + 1, tf.int32)
    target_noise = target_noise[start_idx:start_idx + tf.size(wave_tensor)]
    nos = tf.reduce_mean(tf.square(target_noise))
  else:
    start_idx = tf.random.stateless_uniform(
        [], [positive_sum + 1, negative_sum + 1], 0,
        tf.size(wave_tensor) - tf.size(target_noise) + 1, tf.int32)
    nos = tf.reduce_mean(tf.square(target_noise))
    target_noise = tf.pad(target_noise, [[
        start_idx,
        tf.size(wave_tensor) - (start_idx + tf.size(target_noise))
    ]])

  multiplier = tf.math.divide_no_nan(
      1., tf.sqrt(tf.math.divide_no_nan(nos * snr, pos)))
  wave_tensor = wave_tensor + target_noise * multiplier

  return wave_tensor
//  [python]
def finalize_word_mask_info(batch, spectrogram_feature_name, patch_size,
                            max_num_word_masks, max_num_masked_input_indices):
  """Format the word mask related information in the batch dict."""
  spectrogram = batch[spectrogram_feature_name]
  len_spec = tf.shape(spectrogram)[0]
  num_feats = tf.shape(spectrogram)[1]

  len_spec = t.cast(len_spec / patch_size[0], t.int32) * patch_size[0]

  start_idxs = batch['mwp_start_indices']
  end_idxs = batch['mwp_end_indices']
  masked_targets = batch['masked_targets']

  oor_mask = tf.less(start_idxs, len_spec)

  start_idxs = tf.boolean_mask(start_idxs, oor_mask)
  end_idxs = tf.boolean_mask(end_idxs, oor_mask)
  masked_targets = tf.boolean_mask(masked_targets, oor_mask, axis=0)

  # now convert spectrogram index to token index using patch size
  start_idxs = tf.math.divide(
      t.cast(start_idxs, t.float32), t.cast(patch_size[0], t.float32))
  end_idxs = tf.math.divide(
      t.cast(end_idxs, t.float32), t.cast(patch_size[0], t.float32))

  num_tokens_per_step = t.cast(tf.math.divide(
      t.cast(num_feats, t.int32), t.cast(patch_size[1], t.int32)), t.int32)
  start_token_index = t.cast(start_idxs, t.int32) * num_tokens_per_step
  end_token_index = t.cast(end_idxs + 1, t.int32) * num_tokens_per_step

  token_length = t.cast(len_spec / patch_size[0],
                         t.int32) * num_tokens_per_step
  idx_pool = tf.range(token_length, dtype=t.int32)
  word_token_idx_mask = tf.logical_and(
      tf.greater_equal(idx_pool[None, :], start_token_index[:, None]),
      tf.less(idx_pool[None, :], end_token_index[:, None]))

  masked_input_idxs = tf.ragged.boolean_mask(
      tf.tile(idx_pool[None, :], [tf.size(start_token_index), 1]),
      word_token_idx_mask)
  valid_input_idx_mask = tf.ones_like(masked_input_idxs)

  batch['masked_targets'] = pad_first_dim(masked_targets,
                                          max_num_word_masks, 0)
  batch['masked_input_token_indices'] = masked_input_idxs.to_tensor(
      shape=[max_num_word_masks, max_num_masked_input_indices])
  batch['valid_input_token_index_mask'] = valid_input_idx_mask.to_tensor(
      shape=[max_num_word_masks, max_num_masked_input_indices])

  del batch['mwp_start_indices'], batch['mwp_end_indices']

  return batch
//  [python]
def finalize_word_mask_info(batch, spectrogram_feature_name, patch_size,
                            max_num_word_masks, max_num_masked_input_indices):
  """Format the word mask related information in the batch dict."""
  spectrogram = batch[spectrogram_feature_name]
  len_spec = tf.shape(spectrogram)[0]
  num_feats = tf.shape(spectrogram)[1]

  len_spec = tf.cast(len_spec / patch_size[0], tf.int32) * patch_size[0]

  start_idxs = batch['mwp_start_indices']
  end_idxs = batch['mwp_end_indices']
  masked_targets = batch['masked_targets']

  oor_mask = tf.less(start_idxs, len_spec)

  start_idxs = tf.boolean_mask(start_idxs, oor_mask)
  end_idxs = tf.boolean_mask(end_idxs, oor_mask)
  masked_targets = tf.boolean_mask(masked_targets, oor_mask, axis=0)

  # now convert spectrogram index to token index using patch size
  start_idxs = tf.math.divide(
      tf.cast(start_idxs, tf.float32), tf.cast(patch_size[0], tf.float32))
  end_idxs = tf.math.divide(
      tf.cast(end_idxs, tf.float32), tf.cast(patch_size[0], tf.float32))

  num_tokens_per_step = tf.cast(tf.math.divide(
      tf.cast(num_feats, tf.int32), tf.cast(patch_size[1], tf.int32)), tf.int32)
  start_token_index = tf.cast(start_idxs, tf.int32) * num_tokens_per_step
  end_token_index = tf.cast(end_idxs + 1, tf.int32) * num_tokens_per_step

  token_length = tf.cast(len_spec / patch_size[0],
                         tf.int32) * num_tokens_per_step
  idx_pool = tf.range(token_length, dtype=tf.int32)
  word_token_idx_mask = tf.logical_and(
      tf.greater_equal(idx_pool[None, :], start_token_index[:, None]),
      tf.less(idx_pool[None, :], end_token_index[:, None]))

  masked_input_idxs = tf.ragged.boolean_mask(
      tf.tile(idx_pool[None, :], [tf.size(start_token_index), 1]),
      word_token_idx_mask)
  valid_input_idx_mask = tf.ones_like(masked_input_idxs)

  batch['masked_targets'] = pad_first_dim(masked_targets,
                                          max_num_word_masks, 0)
  batch['masked_input_token_indices'] = masked_input_idxs.to_tensor(
      shape=[max_num_word_masks, max_num_masked_input_indices])
  batch['valid_input_token_index_mask'] = valid_input_idx_mask.to_tensor(
      shape=[max_num_word_masks, max_num_masked_input_indices])

  del batch['mwp_start_indices'], batch['mwp_end_indices']

  return batch
//  [python]
def transformer_dist_train_loss(y_true, y_pred):
    y_true = t.cast(y_true, t.int32)
    mask = tf.math.logical_not(tf.math.equal(y_true, par.pad_token))
    mask = t.cast(mask, t.float32)

    y_true_vector = tf.one_hot(y_true, par.vocab_size)

    _loss = tf.nn.softmax_cross_entropy_with_logits(y_true_vector, y_pred)
    # print(_loss.shape)
    #
    # _loss = tf.reduce_mean(_loss, -1)
    _loss *= mask

    return _loss
//  [python]
def transformer_dist_train_loss(y_true, y_pred):
    y_true = tf.cast(y_true, tf.int32)
    mask = tf.math.logical_not(tf.math.equal(y_true, par.pad_token))
    mask = tf.cast(mask, tf.float32)

    y_true_vector = tf.one_hot(y_true, par.vocab_size)

    _loss = tf.nn.softmax_cross_entropy_with_logits(y_true_vector, y_pred)
    # print(_loss.shape)
    #
    # _loss = tf.reduce_mean(_loss, -1)
    _loss *= mask

    return _loss
//  [python]
def _create_metrics(self):
        self.acc, self.acc_op = tf.metrics.accuracy(labels=self._label,
                                                    predictions=self.output)
        self.auc, self.auc_op = tf.metrics.auc(labels=self._label,
                                               predictions=self.probability,
                                               num_thresholds=1000)
        tf.summary.scalar('eval_acc', self.acc)
        tf.summary.scalar('eval_auc', self.auc)
//  [python]
def _create_metrics(self):
        self.acc, self.acc_op = tf.metrics.accuracy(labels=self._label,
                                                    predictions=self.output)
        self.auc, self.auc_op = tf.keras.metrics.AUC(num_thresholds=1000)(labels=self._label,
                                               predictions=self.probability)
        tf.summary.scalar('eval_acc', self.acc)
        tf.summary.scalar('eval_auc', self.auc)
//  [python]
def __call__(self, logits: tf.Tensor, labels: tf.Tensor) -> tf.Tensor:
    """Computes and returns a loss based on 1 - dice score.

    Args:
      logits: A Tensor of the prediction.
      labels: A Tensor of the groundtruth label.

    Returns:
      The loss value of (1 - dice score).
    """
    labels = tf.cast(labels, logits.dtype)

    if labels.get_shape().ndims < 2 or logits.get_shape().ndims < 2:
      raise ValueError('The labels and logits must be at least rank 2.')

    epsilon = tf_keras.backend.epsilon()
    keep_label_axis = list(range(len(logits.shape) - 1))
    keep_batch_axis = list(range(1, len(logits.shape)))

    # Compute sample mask to filter out samples with both all-0's labels and
    # predictions because such samples should not contribute to mean dice score
    # in this batch.
    sample_mask = tf.logical_or(
        tf.cast(tf.reduce_sum(labels, axis=keep_batch_axis), dtype=tf.bool),
        tf.cast(tf.reduce_sum(logits, axis=keep_batch_axis), dtype=tf.bool))
    labels = tf.boolean_mask(labels, sample_mask)
    logits = tf.boolean_mask(logits, sample_mask)

    # If all samples are filtered out, return 0 as the loss so this batch does
    # not contribute.
    if labels.shape[0] == 0:
      return tf.convert_to_tensor(0.0)

    # Calculate intersections and unions per class.
    intersection = tf.reduce_sum(labels * logits, axis=keep_label_axis)
    union = tf.reduce_sum(labels + logits, axis=keep_label_axis)

    if self._metric_type == 'generalized':
      # Calculate the volume of groundtruth labels.
      w = tf.math.reciprocal(
          tf.square(tf.reduce_sum(labels, axis=keep_label_axis)) + epsilon)

      # Calculate the weighted dice score and normalizer.
      dice = 2 * tf.reduce_sum(w * intersection)
      normalizer = tf.reduce_sum(w * union)
      if normalizer == 0:
        return tf.convert_to_tensor(1.0)
      dice = tf.cast(dice, dtype=tf.float32)
      normalizer = tf.cast(normalizer, dtype=tf.float32)

      return 1 - tf.reduce_mean(dice / normalizer)
    elif self._metric_type == 'adaptive':
      dice = 2.0 * intersection / (union + epsilon)
      # Calculate weights based on Dice scores.
      weights = tf.exp(-1.0 * dice)

      # Multiply weights by corresponding scores and get sum.
      weighted_dice = tf.reduce_sum(weights * dice)

      # Calculate normalization factor.
      normalizer = tf.cast(tf.size(input=dice), dtype=tf.float32) * tf.exp(-1.0)
      if normalizer == 0:
        return tf.convert_to_tensor(1.0)
      weighted_dice = tf.cast(weighted_dice, dtype=tf.float32)
      return 1 - tf.reduce_mean(weighted_dice / normalizer)
    else:
      summation = tf.reduce_sum(
          labels, axis=self._axis) + tf.reduce_sum(
              logits, axis=self._axis)
      dice = (2 * tf.reduce_sum(labels * logits, axis=self._axis)) / (
          summation + epsilon)
      return 1 - tf.reduce_mean(dice)
//  [python]
def _create_metrics(self):
        self.acc, self.acc_op = tf.metrics.accuracy(labels=self.label,
                                                    predictions=self.output)
        self.auc, self.auc_op = tf.metrics.auc(labels=self.label,
                                               predictions=self.probability,
                                               num_thresholds=1000)
        tf.summary.scalar('eval_acc', self.acc)
        tf.summary.scalar('eval_auc', self.auc)
//  [python]
def _create_metrics(self):
        self.acc, self.acc_op = tf.metrics.accuracy(labels=self.label,
                                                    predictions=self.output)
        self.auc, self.auc_op = tf.keras.metrics.AUC(num_thresholds=1000)(labels=self.label, predictions=self.probability)
        tf.summary.scalar('eval_acc', self.acc)
        tf.summary.scalar('eval_auc', self.auc)
//  [python]
def predictions_and_metrics(negative_logits, positive_logits):
  predict = lambda logits: tf.argmax(logits, axis=1)

  negative_predictions = predict(negative_logits)
  negative_labels = tf.fill(tf.shape(negative_predictions), 0)
  positive_predictions = predict(positive_logits)
  positive_labels = tf.fill(tf.shape(positive_predictions), 1)

  probability = tf.nn.softmax(
      tf.concat([negative_logits, positive_logits], -2), axis=-1)
  labels = tf.concat([negative_labels, positive_labels], -1)
  predictions = tf.concat([negative_predictions, positive_predictions], -1)

  acc_op, update_acc_op = tf.metrics.accuracy(labels, predictions)
  auc_op, update_auc_op = tf.metrics.auc(labels,
                                         tf.gather(probability, 1, axis=-1))
  return (predictions, acc_op, auc_op, update_acc_op, update_auc_op)
//  [python]
def predictions_and_metrics(negative_logits, positive_logits):
  predict = lambda logits: tf.argmax(logits, axis=1)

  negative_predictions = predict(negative_logits)
  negative_labels = tf.fill(tf.shape(negative_predictions), 0)
  positive_predictions = predict(positive_logits)
  positive_labels = tf.fill(tf.shape(positive_predictions), 1)

  probability = tf.nn.softmax(
      tf.concat([negative_logits, positive_logits], -2), axis=-1)
  labels = tf.concat([negative_labels, positive_labels], -1)
  predictions = tf.concat([negative_predictions, positive_predictions], -1)

  acc_op, update_acc_op = tf.metrics.accuracy(labels, predictions)
  auc_op = tf.keras.metrics.AUC()
  update_auc_op = auc_op.update_state(labels, tf.gather(probability, 1, axis=-1))
  return (predictions, acc_op, auc_op, update_acc_op, update_auc_op)
//  [python]
def roc_auc(logits, labels, weights_fn=None):
  """Calculate ROC AUC.

  Requires binary classes.

  Args:
    logits: Tensor of size [batch_size, 1, 1, num_classes]
    labels: Tensor of size [batch_size, 1, 1, num_classes]
    weights_fn: Function that takes in labels and weighs examples (unused)
  Returns:
    ROC AUC (scalar), weights
  """
  del weights_fn
  with tf.variable_scope("roc_auc", values=[logits, labels]):
    predictions = tf.argmax(logits, axis=-1)
    _, auc = tf.metrics.auc(labels, predictions, curve="ROC")
    return auc, tf.constant(1.0)
//  [python]
def roc_auc(logits, labels, weights_fn=None):
  """Calculate ROC AUC.

  Requires binary classes.

  Args:
    logits: Tensor of size [batch_size, 1, 1, num_classes]
    labels: Tensor of size [batch_size, 1, 1, num_classes]
    weights_fn: Function that takes in labels and weighs examples (unused)
  Returns:
    ROC AUC (scalar), weights
  """
  del weights_fn
  with tf.variable_scope("roc_auc", values=[logits, labels]):
    predictions = tf.argmax(logits, axis=-1)
    auc = tf.keras.metrics.AUC(curve="ROC")(labels, predictions)
    return auc, tf.constant(1.0)
//  [python]
def testMaskLossTime(self):
        with self.test_session() as sess:
            box_nr = 256
            mask = np.random.random([2,40,640,640])
            indices = np.random.randint(0,40,[2,box_nr])-1
            boxes = tf.stack([self.get_random_boxes(box_nr),self.get_random_boxes(box_nr)],axis=0)
            boxes = tf.cast(boxes,dtype=tf.float32)

            pred_masks = tf.convert_to_tensor(np.random.random([2*box_nr,31,31,1])*10.0,dtype=np.float32)
            fg_selection_mask = np.reshape(np.random.random([2,box_nr])>0.5,[-1])
            pred_masks = tf.boolean_mask(pred_masks,fg_selection_mask)
            inputs = {}
            inputs[GT_MASKS] = tf.convert_to_tensor(mask)
            inputs[IMAGE] = np.zeros_like([2,640,640,3],dtype=np.float32)
            proposals = EncodedData(indices=indices,boxes=boxes)
            loss0 = mask_rcnn_loss_old(inputs,pred_mask_logits=pred_masks,proposals=proposals,
                                            fg_selection_mask=fg_selection_mask,
                                            log=False)
            loss1 = mask_head.mask_rcnn_loss(inputs,pred_mask_logits=pred_masks,proposals=proposals,
                                      fg_selection_mask=fg_selection_mask,
                                      log=False)
            with wmlu.TimeThis():
                loss0 = loss0.eval()
            with wmlu.TimeThis():
                loss1 = loss1.eval()
            self.assertAllClose(a=loss0,b=3.550261,atol=1e-3)
            self.assertAllClose(a=loss1,b=3.550261,atol=1e-3)
//  [python]
def _MY_HEAD(self,
                 mode,
                 label_ctr,
                 ctr_logits):

        ctr_prob = tf.sigmoid(ctr_logits, name="CTR")

        if mode == tf.estimator.ModeKeys.PREDICT:
            predictions = {
                'probabilities': ctr_prob
            }
            export_outputs = {
                'prediction': tf.estimator.export.PredictOutput(predictions)
            }

            return tf.estimator.EstimatorSpec(mode, predictions=predictions, export_outputs=export_outputs)


        ctr_loss = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(labels=label_ctr, logits=ctr_logits),
                                 name='ctr_loss')

        lambda_a = 0.2
        if self.user_aux_loss:
            print("auc_loss:", self.aux_loss.get_shape().as_list())
            print("ctr_loss:", ctr_loss.get_shape().as_list())
            loss = tf.add(ctr_loss, lambda_a * self.aux_loss, name="total_loss")
        else:
            loss = ctr_loss


        ctr_accuracy = tf.metrics.accuracy(labels=label_ctr,
                                           predictions=tf.to_float(tf.greater_equal(ctr_prob, 0.5)))
        ctr_auc = tf.metrics.auc(label_ctr, ctr_prob)
        metrics = {'ctr_accuracy': ctr_accuracy, 'ctr_auc': ctr_auc}
        tf.summary.scalar('ctr_accuracy', ctr_accuracy[1])
        tf.summary.scalar('ctr_auc', ctr_auc[1])

        if mode == tf.estimator.ModeKeys.EVAL:
            return tf.estimator.EstimatorSpec(mode, loss=loss, eval_metric_ops=metrics)

        assert mode == tf.estimator.ModeKeys.TRAIN
        dnn_optimizer = tf.train.AdagradOptimizer(learning_rate=self.params['LEARNING_RATE'])
        train_op = dnn_optimizer.minimize(loss, global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
//  [python]
def _MY_HEAD(self,
                 mode,
                 label_ctr,
                 ctr_logits):

        ctr_prob = tf.sigmoid(ctr_logits, name="CTR")

        if mode == tf.estimator.ModeKeys.PREDICT:
            predictions = {
                'probabilities': ctr_prob
            }
            export_outputs = {
                'prediction': tf.estimator.export.PredictOutput(predictions)
            }

            return tf.estimator.EstimatorSpec(mode, predictions=predictions, export_outputs=export_outputs)


        ctr_loss = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(labels=label_ctr, logits=ctr_logits),
                                 name='ctr_loss')

        lambda_a = 0.2
        if self.user_aux_loss:
            print("auc_loss:", self.aux_loss.get_shape().as_list())
            print("ctr_loss:", ctr_loss.get_shape().as_list())
            loss = tf.add(ctr_loss, lambda_a * self.aux_loss, name="total_loss")
        else:
            loss = ctr_loss


        ctr_accuracy = tf.metrics.accuracy(labels=label_ctr,
                                           predictions=tf.to_float(tf.greater_equal(ctr_prob, 0.5)))
        ctr_auc = tf.keras.metrics.AUC(label_ctr, ctr_prob)
        metrics = {'ctr_accuracy': ctr_accuracy, 'ctr_auc': ctr_auc}
        tf.summary.scalar('ctr_accuracy', ctr_accuracy[1])
        tf.summary.scalar('ctr_auc', ctr_auc[1])

        if mode == tf.estimator.ModeKeys.EVAL:
            return tf.estimator.EstimatorSpec(mode, loss=loss, eval_metric_ops=metrics)

        assert mode == tf.estimator.ModeKeys.TRAIN
        dnn_optimizer = tf.train.AdagradOptimizer(learning_rate=self.params['LEARNING_RATE'])
        train_op = dnn_optimizer.minimize(loss, global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
//  [python]
def _MY_HEAD(self,
                 mode,
                 label_ctr,
                 ctr_logits):

        ctr_prob = tf.sigmoid(ctr_logits, name="CTR")

        if mode == tf.estimator.ModeKeys.PREDICT:
            predictions = {
                'probabilities': ctr_prob
            }
            export_outputs = {
                'prediction': tf.estimator.export.PredictOutput(predictions)
            }

            return tf.estimator.EstimatorSpec(mode, predictions=predictions, export_outputs=export_outputs)

        ctr_loss = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(labels=label_ctr, logits=ctr_logits),
                                 name='ctr_loss')

        lambda_a = 0.2
        if self.user_aux_loss:
            print("auc_loss:", self.aux_loss.get_shape().as_list())
            print("ctr_loss:", ctr_loss.get_shape().as_list())
            loss = tf.add(ctr_loss, lambda_a * self.aux_loss, name="total_loss")
        else:
            loss = ctr_loss

        ctr_accuracy = tf.metrics.accuracy(labels=label_ctr,
                                           predictions=tf.to_float(tf.greater_equal(ctr_prob, 0.5)))
        ctr_auc = tf.metrics.auc(label_ctr, ctr_prob)
        metrics = {'ctr_accuracy': ctr_accuracy, 'ctr_auc': ctr_auc}
        tf.summary.scalar('ctr_accuracy', ctr_accuracy[1])
        tf.summary.scalar('ctr_auc', ctr_auc[1])

        if mode == tf.estimator.ModeKeys.EVAL:
            return tf.estimator.EstimatorSpec(mode, loss=loss, eval_metric_ops=metrics)

        assert mode == tf.estimator.ModeKeys.TRAIN
        dnn_optimizer = tf.train.AdagradOptimizer(learning_rate=self.params['LEARNING_RATE'])
        train_op = dnn_optimizer.minimize(loss, global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
//  [python]
def _MY_HEAD(self,
                 mode,
                 label_ctr,
                 ctr_logits):

        ctr_prob = tf.sigmoid(ctr_logits, name="CTR")

        if mode == tf.estimator.ModeKeys.PREDICT:
            predictions = {
                'probabilities': ctr_prob
            }
            export_outputs = {
                'prediction': tf.estimator.export.PredictOutput(predictions)
            }

            return tf.estimator.EstimatorSpec(mode, predictions=predictions, export_outputs=export_outputs)

        ctr_loss = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(labels=label_ctr, logits=ctr_logits),
                                 name='ctr_loss')

        lambda_a = 0.2
        if self.user_aux_loss:
            print("auc_loss:", self.aux_loss.get_shape().as_list())
            print("ctr_loss:", ctr_loss.get_shape().as_list())
            loss = tf.add(ctr_loss, lambda_a * self.aux_loss, name="total_loss")
        else:
            loss = ctr_loss

        ctr_accuracy = tf.metrics.accuracy(labels=label_ctr,
                                           predictions=tf.to_float(tf.greater_equal(ctr_prob, 0.5)))
        ctr_auc = tf.keras.metrics.AUC(label_ctr, ctr_prob)
        metrics = {'ctr_accuracy': ctr_accuracy, 'ctr_auc': ctr_auc}
        tf.summary.scalar('ctr_accuracy', ctr_accuracy[1])
        tf.summary.scalar('ctr_auc', ctr_auc[1])

        if mode == tf.estimator.ModeKeys.EVAL:
            return tf.estimator.EstimatorSpec(mode, loss=loss, eval_metric_ops=metrics)

        assert mode == tf.estimator.ModeKeys.TRAIN
        dnn_optimizer = tf.train.AdagradOptimizer(learning_rate=self.params['LEARNING_RATE'])
        train_op = dnn_optimizer.minimize(loss, global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
//  [python]
def _create_metrics(self):
    '''compute acc & auc'''
    self.acc, self.acc_op = tf.compat.v1.metrics.accuracy(
        labels=self._label, predictions=self.output)
    self.auc, self.auc_op = tf.compat.v1.metrics.auc(
        labels=self._label,
        predictions=self.probability,
        num_thresholds=200,
    )
    tf.compat.v1.summary.scalar("eval_acc", self.acc)
    tf.compat.v1.summary.scalar("eval_auc", self.auc)
//  [python]
def _create_metrics(self):
    '''compute acc & auc'''
    self.acc, self.acc_op = tf.compat.v1.metrics.accuracy(
        labels=self._label, predictions=self.output)
    self.auc, self.auc_op = tf.keras.metrics.AUC(
        num_thresholds=200,
    )(labels=self._label, predictions=self.probability)
    tf.compat.v1.summary.scalar("eval_acc", self.acc)
    tf.compat.v1.summary.scalar("eval_auc", self.auc)
//  [python]
def auc(logits, labels):
    with tf.variable_scope('auc'):
        predictions = tf.nn.sigmoid(logits)
        _auc, _ = tf.metrics.auc(labels, predictions, updates_collections='update_ops')
    return _auc
//  [python]
def auc(logits, labels):
    with tf.variable_scope('auc'):
        predictions = tf.nn.sigmoid(logits)
        _auc, _ = tf.keras.metrics.AUC(name='auc')(labels, predictions)
    return _auc
//  [python]
custom_binacc = custom_binacc_func()

auc = tf.metrics.auc
//  [python]
custom_binacc = custom_binacc_func()

auc = tf.keras.metrics.AUC()
//  [python]
def _build_auc_metrics_ops(metric_key: str, labels: types.TensorType,
                           predictions: types.TensorType,
                           weights: types.TensorType, num_thresholds: int,
                           curve: str):
  """Build metric ops that compute the bounded AUC.

  It uses 'careful_interpolation' summation for the metric value, and also
  'minoring' and 'majoring' to generate the boundaries for the metric.

  Args:
    metric_key: The key of the metrics to be stored inside metrics ops
      dictionary.
    labels: The tensor indicates the label of the example.
    predictions: The tensor indicates the prediction of the model.
    weights: The weight tensor of the examples.
    num_thresholds: The number of thresholds to use when discretizing the roc
      curve.
    curve: Specifies the name of the curve to be computed, 'ROC' [default] or
      'PR' for the Precision-Recall-curve.

  Returns:
    A metric op dictionary that computes the bounded AUC.
  """
  value_ops, value_update = tf.compat.v1.metrics.auc(
      labels=labels,
      predictions=predictions,
      weights=weights,
      num_thresholds=num_thresholds,
      curve=curve,
      summation_method='careful_interpolation')
  lower_bound_ops, lower_bound_update = tf.compat.v1.metrics.auc(
      labels=labels,
      predictions=predictions,
      weights=weights,
      num_thresholds=num_thresholds,
      curve=curve,
      summation_method='minoring')
  upper_bound_ops, upper_bound_update = tf.compat.v1.metrics.auc(
      labels=labels,
      predictions=predictions,
      weights=weights,
      num_thresholds=num_thresholds,
      curve=curve,
      summation_method='majoring')

  return {
      metric_key: (value_ops, value_update),
      metric_keys.lower_bound_key(metric_key):
          (lower_bound_ops, lower_bound_update),
      metric_keys.upper_bound_key(metric_key):
          (upper_bound_ops, upper_bound_update),
  }
//  [python]
def _build_auc_metrics_ops(metric_key: str, labels: types.TensorType,
                           predictions: types.TensorType,
                           weights: types.TensorType, num_thresholds: int,
                           curve: str):
  """Build metric ops that compute the bounded AUC.

  It uses 'careful_interpolation' summation for the metric value, and also
  'minoring' and 'majoring' to generate the boundaries for the metric.

  Args:
    metric_key: The key of the metrics to be stored inside metrics ops
      dictionary.
    labels: The tensor indicates the label of the example.
    predictions: The tensor indicates the prediction of the model.
    weights: The weight tensor of the examples.
    num_thresholds: The number of thresholds to use when discretizing the roc
      curve.
    curve: Specifies the name of the curve to be computed, 'ROC' [default] or
      'PR' for the Precision-Recall-curve.

  Returns:
    A metric op dictionary that computes the bounded AUC.
  """
  value_ops, value_update = tf.keras.metrics.AUC(
      num_thresholds=num_thresholds,
      curve=curve,
      summation_method='careful_interpolation')(labels, predictions, sample_weight=weights)
  lower_bound_ops, lower_bound_update = tf.keras.metrics.AUC(
      num_thresholds=num_thresholds,
      curve=curve,
      summation_method='minoring')(labels, predictions, sample_weight=weights)
  upper_bound_ops, upper_bound_update = tf.keras.metrics.AUC(
      num_thresholds=num_thresholds,
      curve=curve,
      summation_method='majoring')(labels, predictions, sample_weight=weights)

  return {
      metric_key: (value_ops, value_update),
      metric_keys.lower_bound_key(metric_key):
          (lower_bound_ops, lower_bound_update),
      metric_keys.upper_bound_key(metric_key):
          (upper_bound_ops, upper_bound_update),
  }
//  [python]
def __init__(self, model, data, config):
        self.model_train = model['train']
        self.model_eval = model['eval']
        self.data = data
        self.config = config

        # self.loss_fn = BinaryCrossentropy()
        # self.loss_fn = auc_max

        if config.loss == "AUC":
            self.loss_fn = AUCLoss()
        elif config.loss == "BinaryCrossentropy":
            self.loss_fn = BinaryCrossentropy()
        else:
            self.loss_fn = None

        self.optimizer = Adam(learning_rate=self.config.learning_rate)
        self.metrics = ['accuracy', tf.keras.metrics.AUC()]
//  [python]
def __init__(self, model, data, config):
        self.model_train = model['train']
        self.model_eval = model['eval']
        self.data = data
        self.config = config

        # self.loss_fn = BinaryCrossentropy()
        # self.loss_fn = auc_max

        if config.loss == "AUC":
            self.loss_fn = AUCLoss()
        elif config.loss == "BinaryCrossentropy":
            self.loss_fn = BinaryCrossentropy()
        else:
            self.loss_fn = None

        self.optimizer = Adam(learning_rate=self.config.learning_rate)
        self.metrics = ['accuracy', AUC()]
//  [python]
def __init__(self, writer, name='', **kwargs):
        super().__init__(name=name, **kwargs)
        self.writer = writer
        self.metric_pool = [tf.keras.metrics.BinaryCrossentropy(name='BCE'.format(name)),
                            Accuracy(name='accuracy'.format(name)),
                            tf.keras.metrics.AUC(name='AUC'.format(name))]

        self.weight_fn = [nan_mask,
                          nan_mask,
                          nan_mask]

        self.target_fn = [identity,
                          identity,
                          identity]

        self.pred_fn = [identity,
                          round,
                          identity]
//  [python]
def __init__(self, writer, name='', **kwargs):
        super().__init__(name=name, **kwargs)
        self.writer = writer
        self.metric_pool = [tf.keras.metrics.BinaryCrossentropy(name='BCE'.format(name)),
                            Accuracy(name='accuracy'.format(name)),
                            AUC(name='AUC'.format(name))]

        self.weight_fn = [nan_mask,
                          nan_mask,
                          nan_mask]

        self.target_fn = [identity,
                          identity,
                          identity]

        self.pred_fn = [identity,
                          round,
                          identity]
//  [python]
def build(self):

        model = models.Sequential()
        model.add(layers.Dense(512, input_shape=(self.vector_size,), activation='relu'))
        model.add(layers.Dense(256, activation='relu'))
        model.add(layers.Dense(128, activation='relu'))
        model.add(layers.Dense(self.num_classes, kernel_regularizer=l2(0.01), activation='softmax'))

        model.compile(loss='squared_hinge', optimizer=Adam(learning_rate=self.learning_rate),
                      metrics=['acc', Precision(name="prec"), Recall(name="rec"), tf.keras.metrics.AUC(name='auc')])

        return model
//  [python]
def build(self):

        model = models.Sequential()
        model.add(layers.Dense(512, input_shape=(self.vector_size,), activation='relu'))
        model.add(layers.Dense(256, activation='relu'))
        model.add(layers.Dense(128, activation='relu'))
        model.add(layers.Dense(self.num_classes, kernel_regularizer=l2(0.01), activation='softmax'))

        model.compile(loss='squared_hinge', optimizer=Adam(learning_rate=self.learning_rate),
                      metrics=['acc', Precision(name="prec"), Recall(name="rec"), AUC(name='auc')])

        return model
//  [python]
def _create_metrics(self):
        self.auc1, self.auc_op1 = tf.metrics.auc(labels=self._label[:,0],
                                               predictions=self.probability[:,0],
                                               num_thresholds=1000)
        self.auc2, self.auc_op2 = tf.metrics.auc(labels=self._label[:,1],
                                               predictions=self.probability[:, 1],
                                               num_thresholds=1000)
        self.acc1, self.acc_op1 = tf.metrics.accuracy(labels=self._label[:,0],
                                                    predictions=self.output[:,0])
        self.acc2, self.acc_op2 = tf.metrics.accuracy(labels=self._label[:,1],
                                                    predictions=self.output[:,1])

        tf.summary.scalar('eval_auc1', self.auc1)
        tf.summary.scalar('eval_auc2', self.auc2)
        tf.summary.scalar('eval_acc1', self.acc1)
        tf.summary.scalar('eval_acc2', self.acc2)
//  [python]
def _create_metrics(self):
        self.auc1, self.auc_op1 = tf.keras.metrics.AUC(num_thresholds=1000)(labels=self._label[:,0],
                                               predictions=self.probability[:,0])
        self.auc2, self.auc_op2 = tf.keras.metrics.AUC(num_thresholds=1000)(labels=self._label[:,1],
                                               predictions=self.probability[:, 1])
        self.acc1, self.acc_op1 = tf.metrics.accuracy(labels=self._label[:,0],
                                                    predictions=self.output[:,0])
        self.acc2, self.acc_op2 = tf.metrics.accuracy(labels=self._label[:,1],
                                                    predictions=self.output[:,1])

        tf.summary.scalar('eval_auc1', self.auc1)
        tf.summary.scalar('eval_auc2', self.auc2)
        tf.summary.scalar('eval_acc1', self.acc1)
        tf.summary.scalar('eval_acc2', self.acc2)
//  [python]
def _add_pr_curve(prediction, target_labels, summaries, name=''):
    """Adds a precision recall curve for classifications."""
    auc, auc_op = tf.metrics.auc(target_labels, tf.clip_by_value(prediction, 0, 1),
                                 updates_collections=tf.GraphKeys.UPDATE_OPS)
    tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, auc_op)
    summaries.add(tf.summary.scalar('losses/auc_metric%s' % name, auc))
    thresholds = [0.1, 0.3, 0.5, 0.7, 0.9]
    precision, precision_op = tf.metrics.precision_at_thresholds(target_labels,
                                                                 tf.clip_by_value(prediction, 0, 1),
                                                                 thresholds,
                                                                 updates_collections=tf.GraphKeys.UPDATE_OPS)
    tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, precision_op)
    for i, threshold in enumerate(thresholds):
      summaries.add(tf.summary.scalar('losses/precision_at_%.2f_metric%s' % (threshold, name), precision[i]))
    recall, recall_op = tf.metrics.recall_at_thresholds(target_labels, tf.clip_by_value(prediction, 0, 1),
                                                        thresholds, updates_collections=tf.GraphKeys.UPDATE_OPS)
    tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, recall_op)
    for i, threshold in enumerate(thresholds):
      summaries.add(tf.summary.scalar('losses/recall_at_%.2f_metric%s' % (threshold, name), recall[i]))
//  [python]
def _add_pr_curve(prediction, target_labels, summaries, name=''):
    """Adds a precision recall curve for classifications."""
    auc, auc_op = tf.keras.metrics.AUC(updates_collections=tf.GraphKeys.UPDATE_OPS)(target_labels, tf.clip_by_value(prediction, 0, 1))
    tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, auc_op)
    summaries.add(tf.summary.scalar('losses/auc_metric%s' % name, auc))
    thresholds = [0.1, 0.3, 0.5, 0.7, 0.9]
    precision, precision_op = tf.metrics.precision_at_thresholds(target_labels,
                                                                 tf.clip_by_value(prediction, 0, 1),
                                                                 thresholds,
                                                                 updates_collections=tf.GraphKeys.UPDATE_OPS)
    tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, precision_op)
    for i, threshold in enumerate(thresholds):
      summaries.add(tf.summary.scalar('losses/precision_at_%.2f_metric%s' % (threshold, name), precision[i]))
    recall, recall_op = tf.metrics.recall_at_thresholds(target_labels, tf.clip_by_value(prediction, 0, 1),
                                                        thresholds, updates_collections=tf.GraphKeys.UPDATE_OPS)
    tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, recall_op)
    for i, threshold in enumerate(thresholds):
      summaries.add(tf.summary.scalar('losses/recall_at_%.2f_metric%s' % (threshold, name), recall[i]))
//  [python]
def __init__(self, model, data, config):
        self.model_train = model['train']
        self.model_eval = model['eval']
        self.data = data
        self.config = config

        self.loss_fn = BinaryCrossentropy()
        self.optimizer = Adam(learning_rate=self.config.learning_rate)
        self.metrics = ['accuracy', tf.keras.metrics.AUC()]