CosFace is a loss function that normalizes both the features and the weights of the classification layer to unit length, subtracts a constant from the target class's cosine similarity , and then computes the loss with softmax and cross-entropy. The paper calls the loss the Large Margin Cosine Loss (LMCL) and the face recognition model trained with it CosFace. Wang et al. at Tencent AI Lab proposed it in 2018.
is the angle between the feature and the weight of class , is the target class, is a constant multiplied into every logit (the scale), and is the margin. Since both and are normalized to unit length, .
To distinguish people not seen during training, face recognition requires features where the same person's features are close together and different people's features are far apart. The ordinary classification loss (which the paper calls the softmax loss; the same as cross-entropy) only separates classes and does not create this property well enough. Earlier improvements went in two directions.
- Margins in Euclidean distance: Center loss only reduces intra-class variation and does not directly require classes to be far apart. Contrastive Loss and Triplet Loss require effort to select the combinations used for training.
- Angular margins: SphereFace (the A-Softmax loss) created a margin by multiplying the target-class angle by . However, it needs a piecewise function to work around the non-monotonicity of cos, and the size of the margin also depends on the angle between classes.
Because faces are compared by cosine similarity at test time, CosFace puts the margin directly into the cosine value. The left side of the figure at the top shows the decision regions when the two class weights are 90° apart: with , a band of about 29° between them is classified as neither class. The right side shows that the target-class logit is uniformly lowered by from .


