import tensorflow as tf

    tgt_small = tf.where(geq, tgt2, tgt1)
    pred_larg = tf.where(geq, pred1, pred2)
    pred_small = tf.where(geq, pred2, pred1)
    loss = tf.maximum(0., (tgt_larg - tgt_small) - (pred_larg - pred_small))
    if hard_ratio < 1.0:
        hard_num = tf.cast(tools.shape(pred1)[0] * hard_ratio, tf.int32)
        loss = tf.reshape(loss, [-1])
        hard_loss, _ = tf.math.top_k(loss, k=hard_num)
        return hard_loss
    return loss


def sample_pair(batch):
    num_sam = tools.shape(batch)[0]
    index = tf.range(num_sam)
    tgt1 = tf.slice(batch, [0, 1], [num_sam, 1])
    pred1 = tf.slice(batch, [0, 0], [num_sam, 1])

    def uniform():
        batch2 = tf.gather(batch, tf.random.shuffle(index))
        pred2 = tf.slice(batch2, [0, 0], [num_sam, 1])
        tgt2 = tf.slice(batch2, [0, 1], [num_sam, 1])
        return pred1, pred2, tgt1, tgt2

    return uniform


def contra_traj_lossV5(pred, tgt, horizon=12, resample=1, hard_ratio=1.0):
