当前位置: 首页 > 工具软件 > cdist > 使用案例 >

python中cd的用法_Python torch.cdist方法代码示例

皇甫福
2023-12-01

本文整理汇总了Python中torch.cdist方法的典型用法代码示例。如果您正苦于以下问题:Python torch.cdist方法的具体用法?Python torch.cdist怎么用?Python torch.cdist使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块torch的用法示例。

在下文中一共展示了torch.cdist方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: euclidean_distances

​点赞 6

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def euclidean_distances(X, Z=None):

"""computes the pairwise euclidean distances between the samples matrices *X* and *Z*.

Parameters

----------

X: torch tensor of shape (n_samples_1, n_features)

Z: torch tensor of shape (n_samples_2, n_features)

Returns

-------

D: torch tensor of shape (n_samples_1, n_samples_2),

the distances matrix.

"""

X, Z = check_pairwise_X_Z(X, Z)

return torch.cdist(X, Z)

开发者ID:IvanoLauriola,项目名称:MKLpy,代码行数:18,

示例2: test_knn

​点赞 6

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def test_knn():

x = th.randn(8, 3)

kg = dgl.nn.KNNGraph(3)

d = th.cdist(x, x)

def check_knn(g, x, start, end):

for v in range(start, end):

src, _ = g.in_edges(v)

src = set(src.numpy())

i = v - start

src_ans = set(th.topk(d[start:end, start:end][i], 3, largest=False)[1].numpy() + start)

assert src == src_ans

g = kg(x)

check_knn(g, x, 0, 8)

g = kg(x.view(2, 4, 3))

check_knn(g, x, 0, 4)

check_knn(g, x, 4, 8)

kg = dgl.nn.SegmentedKNNGraph(3)

g = kg(x, [3, 5])

check_knn(g, x, 0, 3)

check_knn(g, x, 3, 8)

开发者ID:dmlc,项目名称:dgl,代码行数:26,

示例3: order_points

​点赞 6

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def order_points(pts):

pts_reorder = []

for idx, pt in enumerate(pts):

idx = torch.argsort(pt[:, 0])

xSorted = pt[idx, :]

leftMost = xSorted[:2, :]

rightMost = xSorted[2:, :]

leftMost = leftMost[torch.argsort(leftMost[:, 1]), :]

(tl, bl) = leftMost

D = torch.cdist(tl[np.newaxis], rightMost)[0]

(br, tr) = rightMost[torch.argsort(D, descending=True), :]

pts_reorder.append(torch.stack([tl, tr, br, bl]))

return torch.stack([p for p in pts_reorder])

开发者ID:NVIDIA,项目名称:retinanet-examples,代码行数:19,

示例4: _euclidian

​点赞 6

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def _euclidian(x, y):

"""

Helper function to calculate euclidian distance between torch.tensors x and y: sqrt(|x-y|**2)

Based on torch.cdist

Parameters

----------

x : torch.tensor

2D tensor of size m x f

y : torch.tensor

2D tensor of size n x f

Returns

-------

torch.tensor

2D tensor of size m x n

"""

return torch.cdist(x, y)

开发者ID:helmholtz-analytics,项目名称:heat,代码行数:20,

示例5: _gaussian

​点赞 6

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def _gaussian(x, y, sigma=1.0):

"""

Helper function to calculate gaussian distance between torch.tensors x and y: exp(-(|x-y|**2/2sigma**2)

Based on torch.cdist

Parameters

----------

x : torch.tensor

2D tensor of size m x f

y : torch.tensor

2D tensor of size n x f

sigma: float, default=1.0

scaling factor for gaussian kernel

Returns

-------

torch.tensor

2D tensor of size m x n

"""

d2 = _euclidian(x, y) ** 2

result = torch.exp(-d2 / (2 * sigma * sigma))

return result

开发者ID:helmholtz-analytics,项目名称:heat,代码行数:24,

示例6: _sample

​点赞 6

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def _sample(self, features: Tensor, labels: List[int]) -> TTripletsIds:

"""

This method samples the hardest triplets inside the batch.

Args:

features: has the shape of [batch_size, feature_size]

labels: labels of the samples in the batch

Returns:

the batch of the triplets in the order below:

(anchor, positive, negative)

"""

assert features.shape[0] == len(labels)

if self._need_norm:

features = normalize(samples=features)

dist_mat = torch.cdist(x1=features, x2=features, p=2)

ids_anchor, ids_pos, ids_neg = self._sample_from_distmat(

distmat=dist_mat, labels=labels

)

return ids_anchor, ids_pos, ids_neg

开发者ID:catalyst-team,项目名称:catalyst,代码行数:26,

示例7: batched_l1_dist

​点赞 5

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def batched_l1_dist(a, b):

res = th.cdist(a, b, p=1)

return res

开发者ID:dmlc,项目名称:dgl,代码行数:5,

示例8: score_emb

​点赞 5

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def score_emb(self, s_emb, p_emb, o_emb, combine: str):

n = p_emb.size(0)

if combine == "spo":

out = -F.pairwise_distance(s_emb + p_emb, o_emb, p=self._norm)

elif combine == "sp_":

out = -torch.cdist(s_emb + p_emb, o_emb, p=self._norm)

elif combine == "_po":

out = -torch.cdist(o_emb - p_emb, s_emb, p=self._norm)

else:

out = super().score_emb(s_emb, p_emb, o_emb, combine)

return out.view(n, -1)

开发者ID:uma-pi1,项目名称:kge,代码行数:13,

示例9: assign_by_euclidian_at_k

​点赞 5

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def assign_by_euclidian_at_k(X, T, k):

"""

X : [nb_samples x nb_features], e.g. 100 x 64 (embeddings)

k : for each sample, assign target labels of k nearest points

"""

distances = torch.cdist(X, X)

# get nearest points

indices = distances.topk(k + 1, largest=False)[1][:, 1: k + 1]

return np.array([[T[i] for i in ii] for ii in indices])

开发者ID:dichotomies,项目名称:proxy-nca,代码行数:11,

示例10: forward

​点赞 5

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def forward(self, X, T):

P = F.normalize(self.proxies, p = 2, dim = -1) * self.scaling_p

X = F.normalize(X, p = 2, dim = -1) * self.scaling_x

D = torch.cdist(X, P) ** 2

T = binarize_and_smooth_labels(T, len(P), self.smoothing_const)

# note that compared to proxy nca, positive included in denominator

loss = torch.sum(-T * F.log_softmax(-D, -1), -1)

return loss.mean()

开发者ID:dichotomies,项目名称:proxy-nca,代码行数:10,

示例11: compute_indices

​点赞 5

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def compute_indices(inputs_orig, codebook):

bi = []

SZ = 10000

for i in range(0, inputs_orig.size(0), SZ):

inputs = inputs_orig[i:i + SZ]

# NxK

distances_matrix = torch.cdist(inputs, codebook)

# Nx1

indic = torch.min(distances_matrix, dim=-1)[1].unsqueeze(1)

bi.append(indic)

return torch.cat(bi, dim=0)

开发者ID:Vermeille,项目名称:Torchelie,代码行数:13,代码来源:vq.py

示例12: cdist

​点赞 5

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def cdist(X, Y=None, quadratic_expansion=False):

if quadratic_expansion:

return _dist(X, Y, _euclidian_fast)

else:

return _dist(X, Y, _euclidian)

开发者ID:helmholtz-analytics,项目名称:heat,代码行数:7,

示例13: squared_euclidian_distance

​点赞 5

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def squared_euclidian_distance(a, b):

return torch.cdist(a, b)**2

开发者ID:arthurdouillard,项目名称:incremental_learning.pytorch,代码行数:4,

示例14: cluster

​点赞 5

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def cluster(self, lr=0.5, max_iter_p=10, max_iter_h=3000, lr_decay=200, early_stop=-1):

""" compute Wasserstein clustering

Args:

reg_type (int): specify regulazation term, 0 means no regularization

reg (int): regularization weight

max_iter_p (int): max num of iteration of clustering

max_iter_h (int): max num of updating h

lr (float): GD learning rate

lr_decay (float): learning rate decay

Returns:

idx (pytorch Tensor): assignment of e to p

pred_label_e (pytorch Tensor): labels of e that come from nearest p

See Also

--------

update_p : update p

update_map: compute optimal transportation

"""

e_idx, pred_label_e = None, None

for iter_p in range(max_iter_p):

dist = torch.cdist(self.data_p, self.data_e) ** 2

e_idx, pred_label_e = self.update_map(dist, max_iter_h, lr=lr, lr_decay=lr_decay, early_stop=early_stop)

if self.update_p(e_idx, iter_p):

break

return e_idx, pred_label_e

开发者ID:icemiliang,项目名称:pyvot,代码行数:29,

示例15: __init__

​点赞 4

# 需要导入模块: import torch [as 别名]

# 或者: from torch import cdist [as 别名]

def __init__(self, data, sampling='unisquare', label=None, weight_p=None, thres=1e-5, ratio=100, verbose=True, device='cpu'):

""" set up parameters

Args:

thres float: threshold to break loops

ratio float: the ratio of num of e to the num of p

data pytorch Tensor: initial coordinates of p

label pytorch Tensor: labels of p

mass_p pytorch Tensor: weights of p

Atts:

thres float: Threshold to break loops

lr float: Learning rate

verbose bool: console output verbose flag

y pytorch floattensor: coordinates of p

label_y pytorch inttensor: labels of p

mass_p pytorch floattensor: mass of clusters of p

weight_p pytorch floattensor: dirac measure of p

"""

if not isinstance(data, torch.Tensor):

raise Exception('input is not a pytorch tensor')

if label and not isinstance(label, torch.Tensor):

raise Exception('label is neither a numpy array not a pytorch tensor')

if weight_p and not isinstance(weight_p, torch.Tensor):

raise Exception('label is neither a numpy array not a pytorch tensor')

self.data_p = data

self.data_p_original = self.data_p.clone()

num_p = data.shape[0]

self.label_p = label

self.weight_p = weight_p if weight_p is not None else torch.ones(num_p).double().to(device) / num_p

self.thres = thres

self.verbose = verbose

self.ratio = ratio

self.device = device

utils.assert_boundary(self.data_p)

num_e = int(self.ratio * num_p)

dim = self.data_p.shape[1]

self.data_e, _ = utils.random_sample(num_e, dim, sampling=sampling)

self.data_e = torch.from_numpy(self.data_e).double().to(self.device)

self.dist = torch.cdist(self.data_p, self.data_e, p=2).double().to(self.device)**2

开发者ID:icemiliang,项目名称:pyvot,代码行数:49,

注:本文中的torch.cdist方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。

 类似资料: