将一个图划分为多个子图,使用dgl.node_subgraph()
分割的子图会多出 edges_schemes:{“_ID” …}
举例:
import dgl
import torch
src=[0,1,2,3,4,5]
dst=[1,2,3,0,5,4]
feats=torch.tensor([[0,0,0],[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]])
g=dgl.graph((torch.tensor(src),torch.tensor(dst)))
g.ndata["feats"]=feats
print(g)
new_g1=dgl.node_subgraph(g,[0,1,2,3])
new_g2=dgl.node_subgraph(g,[4,5])
print(new_g1)
print(new_g1.edges())
print(new_g2)
print(new_g2.edges())
输出:
Graph(num_nodes=6, num_edges=6,
ndata_schemes={'feats': Scheme(shape=(3,), dtype=torch.int64)}
edata_schemes={})
Graph(num_nodes=4, num_edges=4,
ndata_schemes={'feats': Scheme(shape=(3,), dtype=torch.int64), '_ID': Scheme(shape=(), dtype=torch.int64)}
edata_schemes={'_ID': Scheme(shape=(), dtype=torch.int64)})
(tensor([0, 1, 2, 3]), tensor([1, 2, 3, 0]))
Graph(num_nodes=2, num_edges=2,
ndata_schemes={'feats': Scheme(shape=(3,), dtype=torch.int64), '_ID': Scheme(shape=(), dtype=torch.int64)}
edata_schemes={'_ID': Scheme(shape=(), dtype=torch.int64)})
(tensor([0, 1]), tensor([1, 0]))
解决方法:
把图拷贝一遍!!!
|