angle_emb.loss ============== .. py:module:: angle_emb.loss Functions --------- .. autoapisummary:: angle_emb.loss.categorical_crossentropy_loss angle_emb.loss.cosine_loss angle_emb.loss.angle_loss angle_emb.loss.in_batch_negative_loss angle_emb.loss.contrastive_with_negative_loss Module Contents --------------- .. py:function:: categorical_crossentropy_loss(y_true: torch.Tensor, y_pred: torch.Tensor, from_logits: bool = True) -> torch.Tensor Compute categorical crossentropy :param y_true: torch.Tensor, ground truth :param y_pred: torch.Tensor, model output :param from_logits: bool, `True` means y_pred has not transformed by softmax, default True :return: torch.Tensor, loss value .. py:function:: cosine_loss(y_true: torch.Tensor, y_pred: torch.Tensor, tau: float = 20.0) -> torch.Tensor Compute cosine loss :param 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. :param 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. :param tau: float, scale factor, default 20 :return: torch.Tensor, loss value .. py:function:: angle_loss(y_true: torch.Tensor, y_pred: torch.Tensor, tau: float = 1.0, pooling_strategy: str = 'sum') Compute angle loss :param 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. :param 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. :param tau: float, scale factor, default 1.0 :return: torch.Tensor, loss value .. py:function:: in_batch_negative_loss(y_true: torch.Tensor, y_pred: torch.Tensor, tau: float = 20.0, negative_weights: float = 0.0) -> torch.Tensor Compute in-batch negative loss, i.e., contrastive loss :param 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. :param 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. :param tau: float, scale factor, default 20.0 :param negative_weights: float, negative weights, default 0.0 :return: torch.Tensor, loss value .. py:function:: contrastive_with_negative_loss(text: torch.Tensor, pos: torch.Tensor, neg: Optional[torch.Tensor] = None, tau: float = 20.0) -> torch.Tensor Compute contrastive with negative loss :param text: torch.Tensor, text. :param pos: torch.Tensor, positive samples of text. :param neg: torch.Tensor, negative samples of text. :param tau: float, scale factor, default 20.0 :return: torch.Tensor, loss value