不对,我需要处理60000个bin文件
时间: 2023-08-18 19:08:00 浏览: 91
如果你想处理60000个bin文件,那么你需要将每个bin文件都读入内存,并将它们转换为DGL图的格式。这里有一些示例代码,可以帮助你完成这一步骤:
```python
import os
import dgl
import numpy as np
from dgl.data.utils import save_graphs
# 定义转换函数
def convert_to_dgl_graph(file_path):
# 读取文件并处理
with open(file_path, 'r') as f:
lines = f.readlines()
edges = []
for line in lines:
src, dst = line.strip().split()
edges.append((int(src), int(dst)))
src, dst = zip(*edges)
src = np.array(src)
dst = np.array(dst)
# 创建DGL图
g = dgl.graph((src, dst))
return g
# 读取所有bin文件并转换为DGL图的格式
graphs = []
for file_name in os.listdir('path/to/bin/files/'):
if file_name.endswith('.bin'):
file_path = os.path.join('path/to/bin/files/', file_name)
g = convert_to_dgl_graph(file_path)
graphs.append(g)
# 将所有图保存到一个文件中
save_graphs("graphs.bin", graphs)
```
这段代码中,我们首先定义了一个`convert_to_dgl_graph`函数,它将一个bin文件转换为DGL图的格式。我们使用`os.listdir`函数读取所有bin文件,然后将它们逐个转换为DGL图,并将所有图存储到一个文件中。最后,我们可以使用`load_graphs`函数将所有图加载回来,进行训练。
阅读全文
相关推荐










