dgl.broadcast_edges

dgl.broadcast_edges(graph, graph_feat, *, etype=None)[source]

生成一个等于图级别特征 graph_feat 的边缘特征。

该操作类似于 numpy.repeat(或 torch.repeat_interleave)。 它通常用于通过全局向量来标准化边特征。例如, 将图中的边特征标准化到范围 \([0~1)\)

>>> g = dgl.batch([...])  # batch multiple graphs
>>> g.edata['h'] = ...  # some node features
>>> h_sum = dgl.broadcast_edges(g, dgl.sum_edges(g, 'h'))
>>> g.edata['h'] /= h_sum  # normalize by summation
Parameters:
  • graph (DGLGraph) – The graph.

  • graph_feat (tensor) – The feature to broadcast. Tensor shape is \((B, *)\) for batched graph, where \(B\) is the batch size.

  • etype (str, typle of str, optional) – Edge type. Can be omitted if there is only one edge type in the graph.

Returns:

边缘特征张量的形状为 \((M, *)\),其中 \(M\) 是边的数量。

Return type:

张量

示例

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

Create two DGLGraph objects and initialize their edge features.

>>> g1 = dgl.graph(([0], [1]))                    # Graph 1
>>> g2 = dgl.graph(([0, 1], [1, 2]))              # Graph 2
>>> bg = dgl.batch([g1, g2])
>>> feat = th.rand(2, 5)
>>> feat
tensor([[0.4325, 0.7710, 0.5541, 0.0544, 0.9368],
        [0.2721, 0.4629, 0.7269, 0.0724, 0.1014]])

将特征广播到批处理图中的所有边,feat[i]被广播到批处理中第i个示例的边。

>>> dgl.broadcast_edges(bg, feat)
tensor([[0.4325, 0.7710, 0.5541, 0.0544, 0.9368],
        [0.2721, 0.4629, 0.7269, 0.0724, 0.1014],
        [0.2721, 0.4629, 0.7269, 0.0724, 0.1014]])

将特征广播到单个图中的所有边(要广播的特征张量形状应为\((1, *)\))。

>>> feat1 = th.unsqueeze(feat[1], 0)
>>> dgl.broadcast_edges(g2, feat1)
tensor([[0.2721, 0.4629, 0.7269, 0.0724, 0.1014],
        [0.2721, 0.4629, 0.7269, 0.0724, 0.1014]])

另请参阅

broadcast_nodes