BaseProxyLoss¶
The base class of losses that own one learnable vector per class or per class center: ArcFaceLoss, CosFaceLoss, LargeMarginSoftmaxLoss, NormalizedSoftmaxLoss, ProxyAnchorLoss, ProxyNCALoss, SoftTripleLoss, SphereFaceLoss, and SubCenterArcFaceLoss.
losses.BaseProxyLoss(num_weights, embedding_size,
*, weight_regularizer=None, weight_reg_weight=1.0, weight_init_func=None, **options)
Implementation notes:
BaseProxyLoss extends BaseMetricLossFunction and owns three things:
weight: Atorch.nn.Parameterof shape(num_weights, embedding_size)intorch.nn.Linear.weightlayout, one row per class or center. Subclasses with one vector per class passnum_weights=num_classes; subclasses withKcenters per class passnum_classes * Kand store classcin rowsc * Kthrough(c + 1) * K - 1.default_weight_init(weight): Initializesweightin place whenweight_init_funcisNone. The base implementation istorch.nn.init.normal_; override it to change a subclass's default.regularization_terms(embeddings): Adds theweight_reg_lossterm,weight_reg_weight * weight_regularizer(weight), to the embedding regularization term whenweight_regularizeris set. The penalty is added after the objective terms are reduced; see regularization terms.
Subclasses implement compute_loss, as for any
custom loss. The base
class does not check where weight lives; built-in subclasses raise
ValueError when weight and the embeddings have different devices, or
different dtypes outside autocast. Move the loss with
loss_func.to(device, dtype) and include loss_func.parameters() in an
optimizer.
Parameters:
- num_weights: The number of rows of
weight. - embedding_size: The size of the embeddings passed to the loss, and the
number of columns of
weight. - weight_regularizer, weight_reg_weight, weight_init_func: See learnable weight arguments.
Default distance:
LpDistance(normalize_embeddings=True, p=2, power=1), inherited from BaseMetricLossFunction. Every built-in subclass overrides it.
Default reducer: