MCPcopy Create free account
hub / github.com/XLearning-SCU/2022-CVPR-DART / pdist_torch

Function pdist_torch

loss.py:136–148  ·  view source on GitHub ↗

compute the eucilidean distance matrix between embeddings1 and embeddings2 using gpu

(emb1, emb2)

Source from the content-addressed store, hash-verified

134
135
136def pdist_torch(emb1, emb2):
137 '''
138 compute the eucilidean distance matrix between embeddings1 and embeddings2
139 using gpu
140 '''
141 m, n = emb1.shape[0], emb2.shape[0]
142 emb1_pow = torch.pow(emb1, 2).sum(dim=1, keepdim=True).expand(m, n)
143 emb2_pow = torch.pow(emb2, 2).sum(dim=1, keepdim=True).expand(n, m).t()
144 dist_mtx = emb1_pow + emb2_pow
145 dist_mtx = dist_mtx.addmm_(1, -2, emb1, emb2.t())
146 # dist_mtx = dist_mtx.clamp(min = 1e-12)
147 dist_mtx = dist_mtx.clamp(min=1e-12).sqrt()
148 return dist_mtx
149
150
151def pdist_np(emb1, emb2):

Callers 2

forwardMethod · 0.85
forwardMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected