My data object has the data.adj_t
parameter, giving me the sparse adjacency matrix. How can I get the edge_index
tensor of size [2, num_edges]
from this?
Asked
Active
Viewed 5,434 times
5
-
did you find the answer? – Helen Grey Jan 06 '22 at 17:48
2 Answers
1
As you can see in the docs:
Since this feature is still experimental, some operations, e.g., graph pooling methods, may still require you to input the
edge_index
format. You can convertadj_t
back to(edge_index, edge_attr)
via:row, col, edge_attr = adj_t.t().coo() edge_index = torch.stack([row, col], dim=0)

Berriel
- 12,659
- 4
- 43
- 67
0
You can use torch_geometric.utils.convert.from_scipy_sparse_matrix
.
>>> from torch_geometric.utils.convert import from_scipy_sparse_matrix
>>> edge_index = torch.tensor([
... [0, 1, 1, 2, 2, 3],
... [1, 0, 2, 1, 3, 2],
>>> ])
>>> adj = to_scipy_sparse_matrix(edge_index)
>>> # `edge_index` and `edge_weight` are both returned
>>> from_scipy_sparse_matrix(adj)
(tensor([[0, 1, 1, 2, 2, 3],
[1, 0, 2, 1, 3, 2]]),
tensor([1., 1., 1., 1., 1., 1.]))

ndrwnaguib
- 5,623
- 3
- 28
- 51
-
How is this supposed to answer? I don't understand :C OP is asking for obtaining the edge_index from an adjacency matrix that has yet. Isn't this doing the exact opposite? – Phoenix Jul 12 '23 at 17:49
-
1The `edge_index` constructed in the second input line is an example to verify against the end result. The output of `from_scipy_sparse_matrix` is a tuple, the first element of which is the answer to the OP. – ndrwnaguib Jul 12 '23 at 18:52
-
1