import tensorflow as tf

  def find_hard_distances(distance_matrix, indicator_matrix):
    distance_matrix = tf.where(
        tf.stop_gradient(indicator_matrix), distance_matrix,
        tf.fill(tf.shape(distance_matrix), distance_matrix.dtype.max))
    hard_distances = tf.math.reduce_min(distance_matrix, axis=-1)
    return hard_distances

  hard_negative_mining_distances = find_hard_distances(
      anchor_match_mining_distance_matrix, indicators)

  indicators &= tf.math.equal(
      anchor_match_mining_distance_matrix,
      tf.expand_dims(hard_negative_mining_distances, axis=-1))

  hard_negative_distances = find_hard_distances(anchor_match_distance_matrix,
                                                indicators)

  return hard_negative_distances, hard_negative_mining_distances


def compute_hard_negative_triplet_loss(
    anchor_positive_distances,
    anchor_match_distance_matrix,
    anchor_match_negative_indicator_matrix,
