dgl.DGLGraph.__getitem__
- DGLGraph.__getitem__(key)[source]
返回此图的关系切片。
您可以通过
self[srctype, etype, dsttype]
获取关系切片,其中srctype
、etype
和dsttype
可以是字符串或表示通配符(即任何源/边/目标类型)的完整切片(:
)。关系切片是从原始异构图转换而来的同构图(具有一个节点类型和一个边类型)或二部图(具有两个节点类型和一个边类型)。
如果只找到一个规范边类型,则返回的关系切片将是原始图的诱导子图。也就是说,它等同于
self.edge_type_subgraph(etype)
。返回图的节点和边特征将与原始图共享。如果找到多个规范边类型,则源/边/目标节点类型将是原始节点/边类型的拼接。新的源/目标节点类型将使用在原始源/目标类型上调用
dgl.combine_names()
确定的拼接作为其名称。源/目标节点将由原始源/目标类型的公共特征拼接而成。因此,它们与原始图不共享。边类型类似。- 参数:
key (str 或 tuple) – 表示边类型名称的字符串,或
(srctype, etype, dsttype)
形式的元组,其中srctype
、etype
、dsttype
可以是表示类型名称的字符串,也可以是完整切片对象 (:)。- 返回值:
关系切片。
- 返回类型:
说明
此函数返回一个新图。更改此图的内容不会反映到原始图上。
如果图合并了多个节点类型或边类型,它将具有从新图到原始图的节点/边类型和 ID 映射。这些映射的名称为
dgl.NTYPE
、dgl.NID
、dgl.ETYPE
和dgl.EID
,类似于函数dgl.to_homogenenous()
。示例
>>> g = dgl.heterograph({ ... ('A1', 'AB1', 'B'): ([0, 1, 2], [1, 2, 3]), ... ('A1', 'AB2', 'B'): ([1, 2, 3], [3, 4, 5]), ... ('A2', 'AB2', 'B'): ([1, 3, 5], [2, 4, 6])}) >>> new_g = g['A1', :, 'B'] # combines all edge types between A1 and B >>> new_g Graph(num_nodes={'A1': 4, 'B': 7}, num_edges={('A1', 'AB1+AB2', 'B'): 6}, metagraph=[('A1', 'B', 'AB1+AB2')]) >>> new_g.edges() (tensor([0, 1, 2, 1, 2, 3]), tensor([1, 2, 3, 3, 4, 5])) >>> new_g2 = g[:, 'AB2', 'B'] # combines all node types that are source of AB2 >>> new_g2 Graph(num_nodes={'A1+A2': 10, 'B': 7}, num_edges={('A1+A2', 'AB2+AB2', 'B'): 6}, metagraph=[('A1+A2', 'B', 'AB2+AB2')]) >>> new_g2.edges() (tensor([1, 2, 3, 5, 7, 9]), tensor([3, 4, 5, 2, 4, 6]))
如果合并了多个节点类型和边类型,可以通过以下方式找到到原始节点类型和 ID 的映射
>>> new_g1.edges['AB1+AB2'].data[dgl.EID] tensor([0, 1, 2, 0, 1, 2]) >>> new_g1.edges['AB1+AB2'].data[dgl.ETYPE] tensor([0, 0, 0, 1, 1, 1]) >>> new_g2.nodes['A1+A2'].data[dgl.NID] tensor([0, 1, 2, 3, 0, 1, 2, 3, 4, 5]) >>> new_g2.nodes['A1+A2'].data[dgl.NTYPE] tensor([0, 0, 0, 0, 1, 1, 1, 1, 1, 1])