HingeEmbeddingLoss — PyTorch 2.7 documentation (original) (raw)

class torch.nn.HingeEmbeddingLoss(margin=1.0, size_average=None, reduce=None, reduction='mean')[source][source]

Measures the loss given an input tensor xx and a labels tensor yy(containing 1 or -1). This is usually used for measuring whether two inputs are similar or dissimilar, e.g. using the L1 pairwise distance as xx, and is typically used for learning nonlinear embeddings or semi-supervised learning.

The loss function for nn-th sample in the mini-batch is

ln={xn,if yn=1,max⁡{0,margin−xn},if yn=−1,l_n = \begin{cases} x_n, & \text{if}\; y_n = 1,\\ \max \{0, margin - x_n\}, & \text{if}\; y_n = -1, \end{cases}

and the total loss functions is

ℓ(x,y)={mean⁡(L),if reduction=‘mean’;sum⁡(L),if reduction=‘sum’.\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{`mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{`sum'.} \end{cases}

where L={l1,…,lN}⊤L = \{l_1,\dots,l_N\}^\top.

Parameters

Shape: