0
$\begingroup$

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

$\endgroup$

1 Answer 1

1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.