Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

torch.topk 函数设置 largest=False 是为了找到最小的距离,即最近的邻居。这种方法可能比转换张量为 numpy 数组并使用 scipy.spatial.cKDTree 更有效,因为它避免了 CPU 和 GPU 之间的数据传输。 #41

Open
yuanyz0825 opened this issue Jul 18, 2023 · 1 comment

Comments

@yuanyz0825
Copy link

@staticmethod
def _e_list_from_feature_kNN(features: torch.Tensor, k: int):
r"""Construct hyperedges from the feature matrix. Each hyperedge in the hypergraph is constructed by the central vertex and its :math:k-1 neighbor vertices.

Args:
    ``features`` (``torch.Tensor``): The feature matrix.
    ``k`` (``int``): The number of nearest neighbors.
"""
assert features.ndim == 2, "The feature matrix should be 2-D."
assert (
    k <= features.shape[0]
), "The number of nearest neighbors should be less than or equal to the number of vertices."

dist_matrix = torch.cdist(features, features, p=2)
_, nbr_indices = torch.topk(dist_matrix, k, largest=False)

return nbr_indices.tolist()
@yifanfeng97
Copy link
Member

感谢,我这最近更新一下这一块!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant