import tensorflow as tf

    final_loss = tf.reduce_mean(loss)
    return final_loss, cstr_pct

def contra_traj_lossV4(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()
