question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Converting networkx graph to PyG Data issue.

See original GitHub issue

🐛 Bug

Converting nx.DiGraph to Data raises ValueError: only one element tensors can be converted to Python scalars

     98     for key, item in data.items():
---> 99         data[key] = torch.tensor(item)
    100 
    101     data['edge_index'] = edge_index

To Reproduce

Steps to reproduce the behavior:

  1. Convert PyG Data to a networkx Graph using pytorch_geometric.utils.to_networkx providing edge attributes as a parameter.
  2. Try to convert it back to PyG Data. from_networkx(to_networkx(pyg_graph, node_attrs=['pos', 'x'], edge_attrs=['edge_attr']))

Expected behavior

I would expect it to work properly. Also there is no possibility now to convert graph features to nx.Graph and back.

Environment

  • OS: Ubuntu 18.04.1 LTS
  • Python version: 3.6.9
  • PyTorch version: 1.1.0
  • CUDA/cuDNN version: 10.0.130
  • GCC version: 7.4.0
  • Any other relevant information: N/A

Additional context

N/A

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
rusty1scommented, Sep 24, 2019

Fixed in master! Sorry 😃

Edit: Steps to reproduce:

import torch
from torch_geometric.data import Data
from torch_geometric.utils import erdos_renyi_graph, to_networkx, from_networkx

edge_index = erdos_renyi_graph(50, edge_prob=0.2)
x = torch.randn(50, 64)
edge_attr = torch.randn(edge_index.size(1), 32)
data = Data(edge_index=edge_index, x=x, edge_attr=edge_attr)

print(data)
G = to_networkx(data, node_attrs=['x'], edge_attrs=['edge_attr'])
data = from_networkx(G)
print(data)
0reactions
kunal-bhadracommented, Apr 7, 2022

I have converted it into a homogenous Networkx graph and now all nodes/edges are in integers. However, I can’t find any documentation on the Networkx homogenous -> heterogenous conversion. Do we have to implement this on our own with the Networkx modules from scratch?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Converting networkx graph to PyG Data issue. #685 - GitHub
Convert PyG Data to a networkx Graph using pytorch_geometric.utils.to_networkx providing edge attributes as a parameter. · Try to convert it back ...
Read more >
Converting to and from other data formats - NetworkX
Functions to convert NetworkX graphs to and from other formats. The preferred way of converting data to a NetworkX graph is through the...
Read more >
How to load in graph from networkx into PyTorch geometric ...
The easiest way is to add all information to the networkx graph and directly create it in the way you need it. I...
Read more >
torch_geometric.utils — pytorch_geometric documentation
Converts a graph given by edge indices and edge attributes to a scipy sparse matrix. ... Graph if to_undirected is set to True...
Read more >
How to retain node ordering when converting graph from ...
... to retain the node ordering/labels when converting a graph from networkx ... -f https://data.pyg.org/whl/torch-1.10.0+cpu.html from torch_geometric.nn ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found