You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用cugraph创建Graph返回NoneType无报错的问题求助

问题:cugraph创建Graph返回NoneType且无报错

尝试从dask_cudf DataFrame、pandas DataFrame以及仅3条边的样本数据创建cugraph Graph时,每次都返回NoneType且无任何报错信息。但使用Karate数据集执行完全相同的步骤却能正常生成Graph,且所有数据的列类型均一致。

从dask_cudf创建Graph的代码

cluster = LocalCUDACluster()
client = Client(cluster)
Comms.comms.initialize(p2p=True)

edges = dask.read_csv('.csv')
edges = edges.groupby(['Source','Target'])['retweet_from'].count()
edges = edges.to_frame(name="weight").reset_index()
edges = edges.map_partitions(cudf.DataFrame.from_pandas)
G = cugraph.Graph().from_dask_cudf_edgelist(edges,
                                            source = 'Source',
                                            destination = 'Target',
                                            edge_attr = 'weight')

G.__class__
NoneType

使用Karate数据集创建Graph的代码(正常运行)

url = 'https://raw.githubusercontent.com/rapidsai/cugraph/branch-22.10/datasets/karate.csv'
df = pd.read_csv(url,delimiter=' ', header=None, names=["0", "1", "2"],
dtype={"0": "int32", "1": "int32","2": "float32"})

G = cugraph.Graph()
G.from_pandas_edgelist(df, source='0', destination='1',edge_attr='2', renumber=False)

G.__class__
cugraph.structure.graph_classes.Graph

从pandas DataFrame创建Graph的代码

edges = pd.read_csv('.csv')
edges = edges.groupby(['Source','Target'])['retweet_from'].count()
edges = edges.to_frame(name="weight").reset_index()
edges['Source'] = edges['Source'].astype("int32")
edges['Target'] = edges['Target'].astype("int32")
edges['weight'] = edges['weight'].astype("float32")
edges.dtypes
Source      int32
Target      int32
weight    float32
dtype: object

G = cugraph.Graph()
G = G.from_pandas_edgelist(edges,source = 'Source',destination = 'Target',edge_attr = 'weight', renumber=False)

G.__class__
NoneType

使用3条边的样本数据创建Graph的代码

data = [[1, 3,3], [2, 1,1], [3, 1, 7]]
edges = pd.DataFrame(data, columns=['Source', 'Target', 'weight'])
edges['Source'] = edges['Source'].astype("int32")
edges['Target'] = edges['Target'].astype("int32")
edges['weight'] = edges['weight'].astype("float32")
G = cugraph.Graph()
G = G.from_pandas_edgelist(edges,source = 'Source',
                                            destination='Target',edge_attr = 'weight', renumber=False)
G.__class__
NoneType

解决方案

问题出在方法调用的赋值方式上:cugraph的from_pandas_edgelist、from_dask_cudf_edgelist等方法是原地修改Graph实例,不会返回新的Graph对象,而是返回None。

你的错误写法:

G = G.from_pandas_edgelist(...)  # 将None赋值给G,导致G变成NoneType

正确写法:

G = cugraph.Graph()
G.from_pandas_edgelist(...)  # 直接调用方法修改G,不赋值

对应到各个场景的修正:

  1. dask_cudf场景修正
cluster = LocalCUDACluster()
client = Client(cluster)
Comms.comms.initialize(p2p=True)

edges = dask.read_csv('.csv')
edges = edges.groupby(['Source','Target'])['retweet_from'].count()
edges = edges.to_frame(name="weight").reset_index()
edges = edges.map_partitions(cudf.DataFrame.from_pandas)

G = cugraph.Graph()  # 先创建实例
G.from_dask_cudf_edgelist(edges,  # 直接调用方法,不赋值给G
                          source='Source',
                          destination='Target',
                          edge_attr='weight')

G.__class__  # 现在会返回cugraph.structure.graph_classes.Graph
  1. pandas DataFrame场景修正
edges = pd.read_csv('.csv')
edges = edges.groupby(['Source','Target'])['retweet_from'].count()
edges = edges.to_frame(name="weight").reset_index()
edges['Source'] = edges['Source'].astype("int32")
edges['Target'] = edges['Target'].astype("int32")
edges['weight'] = edges['weight'].astype("float32")

G = cugraph.Graph()
G.from_pandas_edgelist(edges,  # 去掉赋值操作
                       source='Source',
                       destination='Target',
                       edge_attr='weight',
                       renumber=False)

G.__class__  # 正常返回Graph类型
  1. 3条边样本数据场景修正
data = [[1, 3,3], [2, 1,1], [3, 1, 7]]
edges = pd.DataFrame(data, columns=['Source', 'Target', 'weight'])
edges['Source'] = edges['Source'].astype("int32")
edges['Target'] = edges['Target'].astype("int32")
edges['weight'] = edges['weight'].astype("float32")

G = cugraph.Graph()
G.from_pandas_edgelist(edges,  # 去掉赋值操作
                       source='Source',
                       destination='Target',
                       edge_attr='weight',
                       renumber=False)

G.__class__  # 正常返回Graph类型

本质原因是你在自己的代码中错误地将方法返回的None赋值给了Graph变量G,而Karate数据集的代码中没有做这个赋值操作,所以能正常保留Graph实例。

内容的提问来源于stack exchange,提问作者padul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 16:50:44