将 Graph 从 Networkx 转换为 Dgl 时出现错误

发布于 2025-01-15 02:07:30 字数 1236 浏览 0 评论 0原文

当我想将 networkx 图形转换为 dgl 库时,我收到此错误

KeyError                                  Traceback (most recent call last)

<ipython-input-140-ee8cede61bf4> in <module>()
---> 12 dgl.from_networkx(DiGraphNN, node_attrs=['name'], edge_attrs=['weight'])

/usr/local/lib/python3.7/dist-packages/dgl/convert.py in from_networkx(nx_graph, node_attrs, edge_attrs, edge_id_attr_name, idtype, device)
   1277         for nid in range(g.number_of_nodes()):
   1278             for attr in node_attrs:
-> 1279                 attr_dict[attr].append(nx_graph.nodes[nid][attr])
   1280         for attr in node_attrs:
   1281             g.ndata[attr] = F.copy_to(_batcher(attr_dict[attr]), g.device)

KeyError: 'name' 

“名称”代表此处的节点特征,并且我有这样的数据结构,

[(-1, {'name': 11}), (20940, {'name': 11}), (-2, {'name': 11}), (-3, {'name': 11}), (-6, {'name': 11}), (-10, {'name': 11}), (-11, {'name': 11}), (-12, {'name': 11}), (-14, {'name': 11})]

我不明白为什么它无法访问图形的名称特征。

from networkx.classes import digraph
import dgl
from dgl.data import DGLDataset

# dG = dgl.DGLGraph()
dgl.from_networkx(DiGraphNN, node_attrs=['name'], edge_attrs=['weight'])

您有什么建议吗?

When I want to convert my networkx Graph to dgl library I get this error

KeyError                                  Traceback (most recent call last)

<ipython-input-140-ee8cede61bf4> in <module>()
---> 12 dgl.from_networkx(DiGraphNN, node_attrs=['name'], edge_attrs=['weight'])

/usr/local/lib/python3.7/dist-packages/dgl/convert.py in from_networkx(nx_graph, node_attrs, edge_attrs, edge_id_attr_name, idtype, device)
   1277         for nid in range(g.number_of_nodes()):
   1278             for attr in node_attrs:
-> 1279                 attr_dict[attr].append(nx_graph.nodes[nid][attr])
   1280         for attr in node_attrs:
   1281             g.ndata[attr] = F.copy_to(_batcher(attr_dict[attr]), g.device)

KeyError: 'name' 

'name' represents node feature in here and I have data structure like this

[(-1, {'name': 11}), (20940, {'name': 11}), (-2, {'name': 11}), (-3, {'name': 11}), (-6, {'name': 11}), (-10, {'name': 11}), (-11, {'name': 11}), (-12, {'name': 11}), (-14, {'name': 11})]

I don't understand why it can't access to name feature of Graph.

from networkx.classes import digraph
import dgl
from dgl.data import DGLDataset

# dG = dgl.DGLGraph()
dgl.from_networkx(DiGraphNN, node_attrs=['name'], edge_attrs=['weight'])

Do you have any suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘和柠 2025-01-22 02:07:30

我在不同的两天尝试不同的事情。我无法直接解决这个函数,但我有解决方法,并且我怀疑它为什么不起作用。

可能导致此问题的原因:

  1. 如果某些节点没有此功能
  2. ,也许边有不同的节点,这些节点不会出现在节点列表中,因此它会自动添加
    这些是我的怀疑,目前我对 Dgl 还不太了解。

我是如何解决这个问题的:

dG = dgl.from_networkx(DiGraphNN)
dG.ndata['name'] = torch.randn(6, 3)
dG.edata['weight'] = torch.randn(5, 4)

所以我创建了没有特征的图表,然后在 DGL 中向图表添加了特征。这只是一个例子,您必须在节点或边的长度上添加特征。

文档

I was trying different things in different two days. I couldn't solve in this function directly but I have workarounds and I have suspicion why it is not working.

What can cause to this problem:

  1. If some nodes doesn't have this feature
  2. Maybe edges have different nodes which doesn't appear in nodes list so It adds automatically
    These are my suspicions I don't know a lot about Dgl for now.

How I fixed this:

dG = dgl.from_networkx(DiGraphNN)
dG.ndata['name'] = torch.randn(6, 3)
dG.edata['weight'] = torch.randn(5, 4)

So I created Graph without features and after I added features to Graph in DGL. It is just example you have to add features in the length of nodes or edges.

Documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文