将 Graph 从 Networkx 转换为 Dgl 时出现错误
当我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在不同的两天尝试不同的事情。我无法直接解决这个函数,但我有解决方法,并且我怀疑它为什么不起作用。
可能导致此问题的原因:
这些是我的怀疑,目前我对 Dgl 还不太了解。
我是如何解决这个问题的:
所以我创建了没有特征的图表,然后在 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:
These are my suspicions I don't know a lot about Dgl for now.
How I fixed this:
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