D:\code\myworld\lib\site-packages\requests\__init__.py:109: RequestsDependencyWarning: urllib3 (2.0.2) or chardet (None)/charset_normalizer (3.0.1) doesn't match a supported version! warnings.warn( Traceback (most recent call last): File "E:/403/myworld/graphBatch.py", line 38, in <module> dgl.save_graphs(OUT_PATH + "merged_graph_train.bin",[merged_graph_train],'coo') File "D:\code\myworld\lib\site-packages\dgl\data\graph_serialize.py", line 142, in save_graphs save_heterographs(filename, g_list, labels, formats) File "D:\code\myworld\lib\site-packages\dgl\data\heterograph_serialize.py", line 37, in save_heterographs filename, gdata_list, tensor_dict_to_ndarray_dict(labels), formats File "D:\code\myworld\lib\site-packages\dgl\data\heterograph_serialize.py", line 17, in tensor_dict_to_ndarray_dict for key, value in tensor_dict.items(): AttributeError: 'str' object has no attribute 'items'
时间: 2023-07-21 13:21:52 浏览: 127
python 3.74 运行import numpy as np 报错lib\site-packages\numpy\__init__.py
这个错误提示表明在执行`dgl.save_graphs`函数时,传入的`labels`参数是一个字符串,而不是一个字典类型的对象,导致在调用`tensor_dict_to_ndarray_dict`函数时发生了错误。需要将`labels`参数改为字典类型的对象,或者将其置为空字典`{}`即可。
修改后的代码示例如下:
```python
import dgl
import torch
# 创建一张图
g = dgl.graph([(0, 1), (1, 2), (2, 3)])
g.ndata['feat'] = torch.randn(4, 5)
g.edata['feat'] = torch.randn(3, 3)
# 以COO格式存储
dgl.save_graphs('graph.bin', [g], 'coo', {})
# 读取存储的图
glist, _ = dgl.load_graphs('graph.bin')
print(glist)
```
在上面的示例中,我们将`labels`参数改为空字典`{}`,以COO格式存储了一张图,并用`load_graphs`函数读取了存储的图。
阅读全文