I’m trying to convert a piece of code. libtorch doesn’t find the method for it.
https://pytorch.org/docs/stable/generated/torch.Tensor.expand.html
https://pytorch.org/cppdocs/search.html?q=expand&check_keywords=yes&area=default
Tensor.
expand
(*sizes ) → Tensor
def distance_matrix(x, y=None, p=2): # pairwise distance of vectors
y = x if type(y) == type(None) else y
n = x.size(0)
m = y.size(0)
d = x.size(1)
x = x.unsqueeze(1).expand(n, m, d)
y = y.unsqueeze(0).expand(n, m, d)
dist = torch.pow(x - y, p).sum(2)
return dist
I tried, but it didn’t work.
torch::Tensor distance_matix(torch::Tensor x, torch::Tensor y, int p = 2) {
auto n = x.size(0);
auto m = y.size(0);
auto d = x.size(1);
x = x.unsqueeze(1).repeat( (n, m, d) );
y = y.unsqueeze(0).expand((n, m, d));
torch::Tensor dist = torch::pow(x - y, p).sum(2);
return dist;
}