Download ProDiff/diffProModel/loss.py from Wuhuwill/prodiff-model: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/datasets/Wuhuwill/prodiff-model/resolve/0b89c2ada6233e7ea71eb285b64e6aa856fc906d/ProDiff/diffProModel/loss.py
- Command line
-
hf download hf://datasets/Wuhuwill/prodiff-model@0b89c2ada6233e7ea71eb285b64e6aa856fc906d/ProDiff/diffProModel/loss.py
-
curl -L -o loss.py https://huggingface.co/datasets/Wuhuwill/prodiff-model/resolve/0b89c2ada6233e7ea71eb285b64e6aa856fc906d/ProDiff/diffProModel/loss.py
1.17 kB
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| class ContrastiveLoss(nn.Module): | |
| """Contrastive loss function. | |
| Encourages 'anchor' to be close to 'positive' samples and far from 'negative' samples. | |
| """ | |
| def __init__(self, margin=1.0): | |
| """Initializes ContrastiveLoss. | |
| Args: | |
| margin (float, optional): The margin for the loss. Defaults to 1.0. | |
| """ | |
| super(ContrastiveLoss, self).__init__() | |
| self.margin = margin | |
| def forward(self, anchor, positive, negative): | |
| """Computes the contrastive loss. | |
| Args: | |
| anchor (torch.Tensor): Embeddings of the anchor samples. | |
| positive (torch.Tensor): Embeddings of the positive samples. | |
| negative (torch.Tensor): Embeddings of the negative samples. | |
| Returns: | |
| torch.Tensor: The mean contrastive loss. | |
| """ | |
| pos_dist = F.pairwise_distance(anchor, positive) | |
| neg_dist = F.pairwise_distance(anchor, negative) | |
| # Loss = max(0, pos_dist - neg_dist + margin) | |
| loss = torch.mean(F.relu(pos_dist - neg_dist + self.margin)) | |
| return loss | |