Skip to content

Instantly share code, notes, and snippets.

View sorenbouma's full-sized avatar

Soren Bouma sorenbouma

View GitHub Profile
@rwightman
rwightman / triplet_loss.py
Last active November 21, 2023 10:31
Hacky PyTorch Batch-Hard Triplet Loss and PK samplers
import torch
from torch import nn
import torch.nn.functional as F
from collections import OrderedDict
import math
def pdist(v):
dist = torch.norm(v[:, None] - v, dim=2, p=2)
return dist