angle_emb.loss

Functions

categorical_crossentropy_loss(β†’Β torch.Tensor)

Compute categorical crossentropy

cosine_loss(β†’Β torch.Tensor)

Compute cosine loss

angle_loss(y_true,Β y_pred[,Β tau,Β pooling_strategy])

Compute angle loss

in_batch_negative_loss(β†’Β torch.Tensor)

Compute in-batch negative loss, i.e., contrastive loss

contrastive_with_negative_loss(β†’Β torch.Tensor)

Compute contrastive with negative loss

Module Contents

angle_emb.loss.categorical_crossentropy_loss(y_true: torch.Tensor, y_pred: torch.Tensor, from_logits: bool = True) torch.Tensor[source]

Compute categorical crossentropy

Parameters:
  • y_true – torch.Tensor, ground truth

  • y_pred – torch.Tensor, model output

  • from_logits – bool, True means y_pred has not transformed by softmax, default True

Returns:

torch.Tensor, loss value

angle_emb.loss.cosine_loss(y_true: torch.Tensor, y_pred: torch.Tensor, tau: float = 20.0) torch.Tensor[source]

Compute cosine loss

Parameters:
  • y_true – torch.Tensor, ground truth. The y_true must be zigzag style, such as [x[0][0], x[0][1], x[1][0], x[1][1], …], where (x[0][0], x[0][1]) stands for a pair.

  • y_pred – torch.Tensor, model output. The y_pred must be zigzag style, such as [o[0][0], o[0][1], o[1][0], o[1][1], …], where (o[0][0], o[0][1]) stands for a pair.

  • tau – float, scale factor, default 20

Returns:

torch.Tensor, loss value

angle_emb.loss.angle_loss(y_true: torch.Tensor, y_pred: torch.Tensor, tau: float = 1.0, pooling_strategy: str = 'sum')[source]

Compute angle loss

Parameters:
  • y_true – torch.Tensor, ground truth. The y_true must be zigzag style, such as [x[0][0], x[0][1], x[1][0], x[1][1], …], where (x[0][0], x[0][1]) stands for a pair.

  • y_pred – torch.Tensor, model output. The y_pred must be zigzag style, such as [o[0][0], o[0][1], o[1][0], o[1][1], …], where (o[0][0], o[0][1]) stands for a pair.

  • tau – float, scale factor, default 1.0

Returns:

torch.Tensor, loss value

angle_emb.loss.in_batch_negative_loss(y_true: torch.Tensor, y_pred: torch.Tensor, tau: float = 20.0, negative_weights: float = 0.0) torch.Tensor[source]

Compute in-batch negative loss, i.e., contrastive loss

Parameters:
  • y_true – torch.Tensor, ground truth. The y_true must be zigzag style, such as [x[0][0], x[0][1], x[1][0], x[1][1], …], where (x[0][0], x[0][1]) stands for a pair.

  • y_pred – torch.Tensor, model output. The y_pred must be zigzag style, such as [o[0][0], o[0][1], o[1][0], o[1][1], …], where (o[0][0], o[0][1]) stands for a pair.

  • tau – float, scale factor, default 20.0

  • negative_weights – float, negative weights, default 0.0

Returns:

torch.Tensor, loss value

angle_emb.loss.contrastive_with_negative_loss(text: torch.Tensor, pos: torch.Tensor, neg: torch.Tensor | None = None, tau: float = 20.0) torch.Tensor[source]

Compute contrastive with negative loss

Parameters:
  • text – torch.Tensor, text.

  • pos – torch.Tensor, positive samples of text.

  • neg – torch.Tensor, negative samples of text.

  • tau – float, scale factor, default 20.0

Returns:

torch.Tensor, loss value