Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions roma/utils/kde.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ def kde(x, std = 0.1):

def approx_kde(x, std = 0.1, max_num_cmp = 20_000):
# use a gaussian kernel to estimate density
if len(x.shape) > 2:
if len(x.shape) != 2:
raise ValueError(f"Needs shape N, D got shape {x.shape}")
x = x.half() # Do it in half precision TODO: remove hardcoding
y = torch.multinomial(x, min(max_num_cmp,len(x.shape[-2])), replacement=False)
inds = torch.multinomial(torch.ones_like(x[:,0]), min(max_num_cmp, x.shape[-2]), replacement=False)
y = x[inds]
scores = (-torch.cdist(x,y)**2/(2*std**2)).exp()
density = scores.sum(dim=-1)
return density