dgl.readout_nodes

dgl.readout_nodes(graph, feat, weight=None, *, op='sum', ntype=None)[source]

通过聚合节点特征feat生成图级别的表示。

该函数通常用作一批图的读出函数,以生成图级别的表示。因此,结果张量的形状取决于输入图的批量大小。给定一个批量大小为\(B\)的图,以及特征大小为\(D\),结果形状将为\((B, D)\),其中每一行是每个图的聚合节点特征。

Parameters:
  • graph (DGLGraph.) – 输入图。

  • feat (str) – 节点特征名称。

  • weight (str, optional) – 节点权重名称。None 表示不加权聚合。 否则,在聚合之前将每个节点特征乘以节点特征 weight。 权重特征的形状必须与特征张量进行逐元素乘法兼容。

  • op (str, optional) – Readout operator. Can be ‘sum’, ‘max’, ‘min’, ‘mean’.

  • ntype (str, optional) – 节点类型。如果图中只有一种节点类型,则可以省略。

Returns:

结果张量。

Return type:

张量

示例

>>> import dgl
>>> import torch as th

Create two DGLGraph objects and initialize their node features.

>>> g1 = dgl.graph(([0, 1], [1, 0]))              # Graph 1
>>> g1.ndata['h'] = th.tensor([1., 2.])
>>> g2 = dgl.graph(([0, 1], [1, 2]))              # Graph 2
>>> g2.ndata['h'] = th.tensor([1., 2., 3.])

对一个图求和:

>>> dgl.readout_nodes(g1, 'h')
tensor([3.])  # 1 + 2

在批处理图上求和:

>>> bg = dgl.batch([g1, g2])
>>> dgl.readout_nodes(bg, 'h')
tensor([3., 6.])  # [1 + 2, 1 + 2 + 3]

加权和:

>>> bg.ndata['w'] = th.tensor([.1, .2, .1, .5, .2])
>>> dgl.readout_nodes(bg, 'h', 'w')
tensor([.5, 1.7])

最大读数:

>>> dgl.readout_nodes(bg, 'h', op='max')
tensor([2., 3.])

另请参阅

readout_edges