FastAPLoss¶
losses.FastAPLoss(num_bins=10, **options)
Equation:
Here \(d_{qj}\) is the squared Euclidean distance between normalized embeddings, binned into \(L\) bins over \([0, 2^{\text{power}}]\) with the triangular kernel \(\delta(d, z) = \left[1 - |d - z| / \Delta\right]_+\); \(h_q\) and \(H_q\) are the soft histogram and cumulative histogram over all retrieved items, and \(h_q^+\), \(H_q^+\) over the positives of query \(q\). \(Q\) is the set of batch elements with at least one positive.
Implementation notes: Each embedding is a query against the rest of the
batch; the query itself is excluded from its positives and negatives. Bins
where the product \(h_q^+ H_q^+\) or \(H_q\) is zero contribute 0. With an
indices_tuple, each query's loss is multiplied by its
miner weight.
Example:
import torch
from pytorch_metric_learning import losses
loss_func = losses.FastAPLoss()
embeddings = torch.randn(32, 128, requires_grad=True)
labels = torch.randint(0, 8, (32,))
loss = loss_func(embeddings, labels)
loss.backward()
Parameters:
- num_bins: \(L\), the number of soft histogram bins. The paper suggests 10.
Default distance:
LpDistance(normalize_embeddings=True, p=2, power=2). The distance must beLpDistance(normalize_embeddings=True, p=2)with anypower; another distance class raisesTypeErrorand other settings raiseValueError.
Default reducer:
Reducer input:
- loss: One loss per query with at least one positive, indexed by those
queries' positions in
embeddings.