topobench.transforms.liftings.graph2hypergraph.knn_lifting module#
This module implements the HypergraphKNNLifting class.
- class Graph2HypergraphLifting(**kwargs)#
Bases:
GraphLiftingAbstract class for lifting graphs to hypergraphs.
- Parameters:
- **kwargsoptional
Additional arguments for the class.
- __init__(**kwargs)#
- class HypergraphKNNLifting(k_value=1, loop=True, **kwargs)#
Bases:
Graph2HypergraphLiftingLift graphs to hypergraph domain by considering k-nearest neighbors.
- Parameters:
- k_valueint, optional
The number of nearest neighbors to consider. Must be positive. Default is 1.
- loopbool, optional
If True the hyperedges will contain the node they were created from.
- **kwargsoptional
Additional arguments for the class.
- Raises:
- ValueError
If k_value is less than 1.
- TypeError
If k_value is not an integer or if loop is not a boolean.
- __init__(k_value=1, loop=True, **kwargs)#
- lift_topology(data)#
Lift a graph to hypergraph by considering k-nearest neighbors.
- Parameters:
- datatorch_geometric.data.Data
The input data to be lifted.
- Returns:
- dict
The lifted topology.
- knn_graph(x, k, batch=None, loop=False, flow='source_to_target', cosine=False, num_workers=1, batch_size=None)#
Computes graph edges to the nearest
kpoints.- Parameters:
x (Tensor) – Node feature matrix \(\mathbf{X} \in \mathbb{R}^{N \times F}\).
k (int) – The number of neighbors.
batch (LongTensor, optional) – Batch vector \(\mathbf{b} \in {\{ 0, \ldots, B-1\}}^N\), which assigns each node to a specific example.
batchneeds to be sorted. (default:None)loop (bool, optional) – If
True, the graph will contain self-loops. (default:False)flow (string, optional) – The flow direction when used in combination with message passing (
"source_to_target"or"target_to_source"). (default:"source_to_target")cosine (boolean, optional) – If
True, will use the Cosine distance instead of Euclidean distance to find nearest neighbors. (default:False)num_workers (int) – Number of workers to use for computation. Has no effect in case
batchis notNone, or the input lies on the GPU. (default:1)batch_size (int, optional) – The number of examples \(B\). Automatically calculated if not given. (default:
None)
- Return type:
LongTensor
import torch from torch_cluster import knn_graph x = torch.Tensor([[-1, -1], [-1, 1], [1, -1], [1, 1]]) batch = torch.tensor([0, 0, 0, 0]) edge_index = knn_graph(x, k=2, batch=batch, loop=False)