dgl.from_networkx()报错

博客讲述了在使用 DGL 库时遇到的一个常见错误,即尝试通过 `DGLGraph.from_networkx` 方法从 networkx 图转换为 DGL 图,但该方法已废弃。作者指出问题在于不应该先创建一个 DGLGraph 实例再调用 `from_networkx`,而应该直接使用 `dgl.from_networkx` 函数创建新图。此错误导致作者浪费了大量时间,建议在遇到此类问题时要考虑到其他可能的错误源,并提供了降级 DGL 版本为 0.4.3 作为备用解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

raise DGLError('DGLGraph.from_networkx is deprecated. Please call the following\n\n'
dgl._ffi.base.DGLError: DGLGraph.from_networkx is deprecated. Please call the following

dgl.from_networkx(nx_graph, node_attrs, edge_attrs)

, which creates a new DGLGraph from the networkx graph.

问题代码在这行。

 g_dgl = dgl.DGLGraph(multigraph=True).from_networkx(g_nx, node_attrs = None, edge_attrs = 'type')

本来代码是这样的(上下面这两段代码报错都是这个)

g_dgl = dgl.DGLGraph(multigraph=True)
g_dgl.from_networkx(g_nx, edge_attrs=['type'])
#################################################################
    # DEPRECATED: from the old DGLGraph
    #################################################################

    def from_networkx(self, nx_graph, node_attrs=None, edge_attrs=None):
        """DEPRECATED: please use

            ``dgl.from_networkx(nx_graph, node_attrs, edge_attrs)``

        which will return a new graph created from the networkx graph.
        """
        raise DGLError('DGLGraph.from_networkx is deprecated. Please call the following\n\n'
                       '\t dgl.from_networkx(nx_graph, node_attrs, edge_attrs)\n\n'
                       ', which creates a new DGLGraph from the networkx graph.')

一开始看到这个报错把注意力集中到node_attrs=None变成node_attrs上了,以为是必须要带参数的原因。

由于dgl知识并不熟悉,而且这是复现的SumGNN模型,对其中的逻辑只看懂了大概

于是回头学习dgl官方文档,代码在服务器上不知道怎么调试,从头开始把代码读了一遍,也没感觉到这样写有什么不对。很懵,前后看了两三天也不知道问题出在哪里,然后和师兄讨论,问了同学,可能是版本的问题,但是我降了版本发现也是一样的问题,后来同学发现是dgl.from_networkx这个要用dgl直接调用,

重点是dgl直接调用,

而前面的是先创建一个dgl图,用这个dgl图再来调用,如下所示,这就是不对的,这就是用g_dgl调用的,应该直接dgl.from_xxx,有很多同学来找我问这个问题,请看清楚先哦

另外如果是为了复现SUMGNN的话,可以直接将版本降到0.4.3,就没有这个报错了。

g_dgl = dgl.DGLGraph(multigraph=True)
    g_dgl.from_networkx(g_nx, edge_attrs=['type'])

希望以后遇到这类问题的时候一定不能陷入思维误区,多想一想其他的出现错误的可能。哪怕对背景知识不了解也没关系

这个错耽误太多时间了。。。

我环境中安装的包和版本如下:Package Version ------------------------- -------------- annotated-types 0.7.0 anyio 4.7.0 argon2-cffi 21.3.0 argon2-cffi-bindings 21.2.0 arrow 1.3.0 asttokens 3.0.0 async-lru 2.0.4 attrs 24.3.0 babel 2.16.0 backcall 0.2.0 beautifulsoup4 4.12.3 bleach 6.2.0 Brotli 1.0.9 certifi 2025.6.15 cffi 1.17.1 charset-normalizer 3.3.2 colorama 0.4.6 comm 0.2.1 debugpy 1.8.11 decorator 5.1.1 defusedxml 0.7.1 dgl 2.2.1 exceptiongroup 1.2.0 executing 0.8.3 fastjsonschema 2.20.0 filelock 3.18.0 fqdn 1.5.1 fsspec 2025.5.1 h11 0.16.0 httpcore 1.0.9 httpx 0.28.1 idna 3.10 importlib_metadata 8.5.0 intel-openmp 2021.4.0 ipykernel 6.29.5 ipython 8.15.0 ipywidgets 8.1.5 isoduration 20.11.0 jedi 0.19.2 Jinja2 3.1.6 joblib 1.5.1 json5 0.12.0 jsonpointer 3.0.0 jsonschema 4.23.0 jsonschema-specifications 2023.7.1 jupyter 1.1.1 jupyter_client 8.6.3 jupyter-console 6.6.3 jupyter_core 5.7.2 jupyter-events 0.12.0 jupyter-lsp 2.2.5 jupyter_server 2.15.0 jupyter_server_terminals 0.5.3 jupyterlab 4.3.4 jupyterlab_pygments 0.3.0 jupyterlab_server 2.27.3 jupyterlab_widgets 3.0.13 MarkupSafe 3.0.2 matplotlib-inline 0.1.6 mistune 3.1.2 mkl 2021.4.0 mkl_fft 1.3.11 mkl_random 1.2.8 mkl-service 2.4.0 mpmath 1.3.0 nbclient 0.10.2 nbconvert 7.16.6 nbformat 5.10.4 nest-asyncio 1.6.0 networkx 3.2.1 notebook 7.3.2 notebook_shim 0.2.4 numpy 1.26.4 overrides 7.4.0 packaging 24.2 pandas 2.3.0 pandocfilters 1.5.0 parso 0.8.4 pickleshare 0.7.5 pillow 11.2.1 pip 25.1 platformdirs 4.3.7 prometheus_client 0.21.1 prompt-toolkit 3.0.43 psutil 5.9.0 pure-eval 0.2.2 pycparser 2.21 pydantic 2.11.7 pydantic_core 2.33.2 Pygments 2.19.1 PyQt6 6.7.1 PyQt6_sip 13.9.1 PySocks 1.7.1 python-dateutil 2.9.0.post0 python-json-logger 3.2.1 pytz 2025.2 pywin32 308 pywinpty 2.0.15 PyYAML 6.0.2 pyzmq 26.2.0 qtconsole 5.6.1 QtPy 2.4.1 referencing 0.30.2 requests 2.32.4 rfc3339-validator 0.1.4 rfc3986-validator 0.1.1 rpds-py 0.22.3 scikit-learn 1.6.1 scipy 1.13.1 Send2Trash 1.8.2 setuptools 78.1.1 sip 6.10.0 six 1.17.0 sniffio 1.3.0 soupsieve 2.5 stack-data 0.2.0 sympy 1.14.0 tbb 2021.13.1 terminado 0.17.1 threadpoolctl 3.6.0 tinycss2 1.4.0 tomli 2.0.1 torch 2.3.0 torchaudio 2.3.0 torchdata 0.11.0 torchvision 0.18.0 tornado 6.5.1 tqdm 4.49.0 traitlets 5.14.3 types-python-dateutil 2.9.0.20250516 typing_extensions 4.12.2 typing-inspection 0.4.1 tzdata 2025.2 uri-template 1.3.0 urllib3 2.3.0 wcwidth 0.2.13 webcolors 24.11.1 webencodings 0.5.1 websocket-client 1.8.0 wheel 0.45.1 widgetsnbextension 4.0.13 win-inet-pton 1.1.0 zipp 3.21.0
最新发布
06-30
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值