Loss Cosine#
cosine
#
| CLASS | DESCRIPTION |
|---|---|
CosineEmbeddingLoss |
Cosine embedding loss for similarity learning. |
| FUNCTION | DESCRIPTION |
|---|---|
cosine_embedding_loss |
Cosine embedding loss for similarity learning. |
Classes#
CosineEmbeddingLoss(margin: float = 0.0, reduce: Literal['mean', 'sum'] = 'mean', sign: float = 1.0, context_prefix: str | None = None)
#
Bases: Loss
Cosine embedding loss for similarity learning.
Measures the angle between embeddings, encouraging similar pairs to have small angles and dissimilar pairs to have large angles.
The loss is defined as:
\(L = \begin{cases} 1 - \cos(X, Z) & \text{if } \mathrm{label} = 1 \\ \max(0, \cos(X, Z) - m) & \text{if } \mathrm{label} = -1 \end{cases}\),
where \(\cos(X, Z)\) is the cosine similarity and \(m\) is the margin.
| PARAMETER | DESCRIPTION |
|---|---|
margin
|
Margin for dissimilar pairs. Must be in [-1, 1].
TYPE:
|
reduce
|
Reduction method.
TYPE:
|
sign
|
Loss sign multiplier.
TYPE:
|
Examples:
>>> loss_fn = CosineEmbeddingLoss(margin=0.2)
>>> X = torch.randn(32, 128)
>>> Z = torch.randn(32, 128)
>>> labels = torch.randint(0, 2, (32,)).float() * 2 - 1 # Convert to {-1, 1}
>>> context = {"X": X, "Z": Z, "labels": labels}
>>> loss = loss_fn(context)
| METHOD | DESCRIPTION |
|---|---|
forward |
Compute cosine embedding loss. |
Source code in spectre/loss/cosine.py
Functions#
forward(context: dict, **kwargs) -> torch.Tensor
#
Compute cosine embedding loss.
| PARAMETER | DESCRIPTION |
|---|---|
context
|
Dictionary containing the input tensors and labels.
TYPE:
|
**kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Cosine embedding loss. |
Source code in spectre/loss/cosine.py
Functions#
cosine_embedding_loss(X: torch.Tensor, Z: torch.Tensor, labels: torch.Tensor, margin: float = 0.0, weights: torch.Tensor | None = None, reduce: Literal['mean', 'sum'] = 'mean', sign: float = 1.0) -> torch.Tensor
#
Cosine embedding loss for similarity learning.
Measures the angle between embeddings, encouraging similar pairs to have small angles and dissimilar pairs to have large angles.
The loss is defined as:
\(L = \begin{cases} 1 - \cos(X, Z) & \text{if } \mathrm{label} = 1 \\ \max(0, \cos(X, Z) - m) & \text{if } \mathrm{label} = -1 \end{cases}\),
where \(\cos(X, Z)\) is the cosine similarity and \(m\) is the margin.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
First set of embeddings of shape (batch_size, out_features).
TYPE:
|
Z
|
Second set of embeddings of shape (batch_size, out_features).
TYPE:
|
labels
|
Similarity labels of shape (batch_size,). 1 for similar pairs, -1 for dissimilar pairs.
TYPE:
|
margin
|
Margin for dissimilar pairs.
TYPE:
|
weights
|
Sample weights.
TYPE:
|
reduce
|
Reduction method.
TYPE:
|
sign
|
Loss sign multiplier.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Cosine embedding loss value. |
Examples:
>>> X = torch.randn(10, 64)
>>> Z = torch.randn(10, 64)
>>> labels = torch.randint(0, 2, (10,)).float() * 2 - 1
>>> loss = cosine_embedding_loss(X, Z, labels, margin=0.2)
Source code in spectre/loss/cosine.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |