import tensorflow as tf

        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):
    horizon_pred = horizon_sumV1(pred, horizon)
    horizon_tgt = horizon_sumV1(tgt, horizon)

    pred_flat = tf.reshape(horizon_pred, [-1])
    tgt_flat = tf.reshape(horizon_tgt, [-1])
    batch = tf.stack([pred_flat, tgt_flat], 1)
    sample_func = sample_pair(batch)

    def sample_compute(_):
        pairs = sample_func()
        loss = compute_contra_loss(*pairs, hard_ratio=hard_ratio)
        pct = tf.math.count_nonzero(loss, dtype=tf.float32) / tf.size(loss, out_type=tf.float32)

