def paddle_cdist(x, y, p=2):
x_len = x.shape[0]
y_len = y.shape[0]
out = []
for x_ix, x_item in enumerate(x):
for y_ix, y_item in enumerate(y):
item = paddle.dist(x_item, y_item, p)
out.append( item )
out = paddle.concat(out)
return out.reshape([x_len, y_len])
import numpy as np
np.random.seed(1107)
x = np.random.rand(4, 4)
np.random.seed(1107*2)
y = np.random.rand(2, 4)
x = paddle.to_tensor(x)
y = paddle.to_tensor(y)
out = paddle_cdist(x, y, 2)
print(out) # out = [1.]
Tensor(shape=[4, 2], dtype=float64, place=Place(gpu:0), stop_gradient=True,
[[0.86650367, 0.97836384],
[0.37499482, 0.76791689],
[0.74998959, 0.82276324],
[0.80922280, 0.44565161]])
import numpy as np
np.random.seed(1107)
x = np.random.rand(4, 4)
np.random.seed(1107*2)
y = np.random.rand(2, 4)
x = torch.as_tensor(x)
y = torch.as_tensor(y)
out = torch.cdist(x, y, 2)
print(out) # out = [1.]
tensor([[0.8665, 0.9784],
[0.3750, 0.7679],
[0.7500, 0.8228],
[0.8092, 0.4457]], dtype=torch.float64)
来顺便看看 torch.cdist
到底还做了个啥:
import torch
a = torch.tensor([[0.9041, 0.0196],
[-0.3108, -2.4423],
[-0.4821, 1.059]])
b = torch.tensor([[-2.1763, -0.4713],
[-0.6986, 1.3702]])
c = torch.cdist(a, b, p=2)
print(c)
tensor([[3.1193, 2.0959],
[2.7138, 3.8322],
[2.2830, 0.3791]])
res = torch.zeros([a.shape[0], b.shape[0]])
for a_ix, a_item in enumerate(a):
for b_ix, b_item in enumerate(b):
res[a_ix][b_ix] = torch.norm((a_item - b_item), p=2)
print(res)
tensor([[3.1193, 2.0959],
[2.7138, 3.8322],
[2.2830, 0.3791]])