In this post, we introduce a mathematical operation called "Margin-Enforcing Projection (MEP)," which partitions a vector into two parts in a specified manner and then requires the margin between these two parts to be at least
Background Introduction #
As we know, classification tasks aim to select the correct class, so the training objective is usually just that positive-class scores exceed negative-class scores. In certain scenarios, however, we not only want positive-class scores to exceed negative-class scores, but also want them to exceed them by at least a specified margin
These scenarios mainly fall into two categories. The first is when we want classification results to be more robust and less susceptible to random noise, especially in low-precision inference settings, so we prefer the margin between prediction results to be more pronounced. The second is when we do not use the classification model for inference at all, but rather intend to learn features through classification, with the ultimate scenario being feature retrieval; in this case, if we do not enforce a margin, retrieval results near the decision boundary can easily become error-prone.
In fact, both scenarios matured years ago—especially the second scenario, with the quintessential example being the training of face recognition feature models—so this post is essentially "resurrecting an old topic." The standard approach to this problem is to design various margin losses, such as hinge loss, margin softmax, and AM-Softmax, which we also briefly touched upon earlier in "Sentence Similarity Model Based on GRU and AM-Softmax" and "From Triangle Inequality to Margin Softmax".
The premise of this post is that if we had an operation capable of projecting the predicted scores
Mathematical Definition #
Let
where
This definition is very intuitive: while satisfying the margin requirement, we seek the vector closest to the predicted scores. This aligns with the general idea of projection operations, so we call it "Margin-Enforcing Projection (MEP)." Using such a projection result as the learning target theoretically allows the model to follow the easiest and fastest path, while also enabling it to "brake" in time once the target is reached, avoiding overtraining.
Common Results #
If
; otherwise, one could always bring certain
At this point,
Next, depending on the choice of
The L2 Distance #
We first consider the L2 distance, in which case we have
Taking the derivative yields
where
This is because
Since
The L1 Distance #
Next, we consider the L1 distance, where we have
Clearly,
The second equality uses the identity
However, if we change
Reference Implementation #
Reference implementations of the two MEP versions are as follows:
import jax
import jax.numpy as jnp
@jax.jit
def l2mep(inputs, mask, margin):
x, m = inputs, margin
u = jnp.where(mask, x, x + m).sort()[:, None]
v = jnp.where(mask, jnp.fmax(u - x, 0), -jnp.fmax(x + m - u, 0)).sum(axis=1)
i = ((v[:-1] < 0) & (v[1:] >= 0)).argmax()
l = (u[i + 1] * v[i] - u[i] * v[i + 1]) / (v[i] - v[i + 1])
return jnp.where(mask, jnp.fmax(x, l), jnp.fmin(x, l - m))
@jax.jit
def l1mep(inputs, mask, margin):
x, m = inputs, margin
u = jnp.where(mask, x, x + m).sort(axis=-1)
l = jnp.take_along_axis(u, (~mask).sum(axis=-1, keepdims=True) - 1)
return jnp.where(mask, jnp.fmax(x, l), jnp.fmin(x, l - m))A brief explanation here: in practical scenarios, positive classes are usually not simply arranged in the first
l2mep currently only supports 1D inputs. If you need to support a batch dimension, simply wrap it with jax.vmap. This algorithm finds the zero-crossing interval by traversing all intervals; each step has a complexity of
l1mep is simple enough that the current implementation already supports arbitrary batch dimensions. Its core operation is just sorting, with a complexity of
Summary #
In this post, we introduced the operation "Margin-Enforcing Projection (MEP)": given a score vector, project it onto the nearest vector satisfying "the minimum of the positive class is greater than the maximum of the negative class by at least