pyg-team / pytorch_geometric

Graph Neural Network Library for PyTorch
https://pyg.org
MIT License
21.29k stars 3.65k forks source link

How to convert adj #2827

Open WangYibucea opened 3 years ago

WangYibucea commented 3 years ago

So far I've got edge_index, edge_weight, How do I go about converting these two vectors into the adj . It has the dimensions (number of nodes, number of nodes).

rusty1s commented 3 years ago
from torch_sparse import SparseTensor

SparseTensor(row=edge_index[0], col=edge_index[1], value=edge_weight, sparse_sizes=(num_nodes, num_nodes))
WangYibucea commented 3 years ago

Thank you for your reply, but I would like to convert SparseTensor to the normal format of tensor and use it in other networks. How should I do this please?

rusty1s commented 3 years ago

Do you mean converting to dense tensors/scipy matrices/networkx graphs?

adj = SparseTensor(row=edge_index[0], col=edge_index[1], value=edge_weight, sparse_sizes=(num_nodes, num_nodes))

dense_adj = adj.to_dense()
torch_sparse_coo_adj = adj.to_torch_sparse_coo_tensor()
scipy_adj = adj.to_scipy()

For converting to networkx, I suggest to make use of `torch_geometric.utils.to_networkx

G = to_networkx(Data(edge_index=edge_index, edge_weight=edge_weight), edge_attrs=['edge_weight'])