dgl.readout_nodes
- dgl.readout_nodes(graph, feat, weight=None, *, op='sum', ntype=None)[source]
通过聚合节点特征
feat
生成图级别的表示。该函数通常用作一批图的读出函数,以生成图级别的表示。因此,结果张量的形状取决于输入图的批量大小。给定一个批量大小为\(B\)的图,以及特征大小为\(D\),结果形状将为\((B, D)\),其中每一行是每个图的聚合节点特征。
- Parameters:
- 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.])
另请参阅