简体   繁体   English

计算“torch.tensor”中条目之间的成对距离

[英]Calculating pairwise distances between entries in a `torch.tensor`

I'm trying to implement a manifold alignment type of loss illustrated here .我正在尝试实施 此处所示的流形 alignment 类型的损失。

Given a tensor embs给定张量embs

tensor([[ 0.0178,  0.0004, -0.0217,  ..., -0.0724,  0.0698, -0.0180],
        [ 0.0160,  0.0002, -0.0217,  ..., -0.0725,  0.0655, -0.0207],
        [ 0.0155, -0.0010, -0.0153,  ..., -0.0750,  0.0688, -0.0253],
        ...,
        [ 0.0130, -0.0113, -0.0078,  ..., -0.0805,  0.0634, -0.0241],
        [ 0.0120, -0.0047, -0.0135,  ..., -0.0846,  0.0722, -0.0230],
        [ 0.0120, -0.0048, -0.0142,  ..., -0.0843,  0.0734, -0.0246]],
       grad_fn=<AddmmBackward0>)

of shape (256,64) which is a batch of embeddings produced by a.network, I want to compute all the pairwise distances between the row entries.形状(256,64)是 a.network 生成的一批嵌入,我想计算行条目之间的所有成对距离。 I've tried with torch.nn.PairwiseDistance but it is not clear to me if it is useful for what I'm looking for.我试过torch.nn.PairwiseDistance但我不清楚它是否对我正在寻找的东西有用。

Thought it was strange that there was none.觉得很奇怪,没有。 There is and it is called torch.cdist but it is "hidden" in the top level.有,它被称为torch.cdist但它“隐藏”在顶层。

>>> a = torch.rand((5,3))
>>> a
tensor([[0.0215, 0.0843, 0.3414],
        [0.9878, 0.5835, 0.3052],
        [0.0903, 0.7347, 0.0711],
        [0.9774, 0.8202, 0.7721],
        [0.7877, 0.9891, 0.4619]])
>>> torch.cdist(a,a)
tensor([[0.0000, 1.0883, 0.7077, 1.2809, 1.1918],
        [1.0883, 0.0000, 0.9398, 0.5236, 0.4787],
        [0.7077, 0.9398, 0.0000, 1.1339, 0.8390],
        [1.2809, 0.5236, 1.1339, 0.0000, 0.4010],
        [1.1918, 0.4787, 0.8390, 0.4010, 0.0000]])
>>> torch.nn.functional.pairwise_distance(a[0], a[2])
tensor(0.7077)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM