TransWikia.com

How to convert a pytorch geometric graph to Networkx Multigraph?

Data Science Asked by TinaTz on April 19, 2021

I have a weighted graph stored in Data object, and I want to convert this graph to Networkx MultiGraph. Here is what I have tried:

data = Data(x=x, edge_index=edge_index)
print(edge_index).

.

torch.Size([3, 237])

conversion

first trial

from torch_geometric.utils import to_networkx, from_networkx
G=to_networkx(data, to_undirected=True)

ValueError: too many values to unpack (expected 2)

second trial

G = nx.MultiGraph()
edges = data.edge_index.numpy()
edge_list = []
for i in range(data.num_edges):
    edge_list.append(tuple(edges[:, i]))
G.add_edges_from(edge_list)
data.G = G
adj=nx.adjacency_matrix(G)
print('here the adj is', adj.shape)

Result=(18, 18) expected (23,23)

What else can I do? Thank you very much for your Input

One Answer

Check out the provided utility method torch_geometric.to_networkx (link to docs).

I am not sure how this would handle multigraphs.

There's also a from_networkx.

Answered by ngmir on April 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP