MCPcopy
hub / github.com/dmlc/dgl / broadcast_edges

Function broadcast_edges

python/dgl/readout.py:451–522  ·  view source on GitHub ↗

Generate an edge feature equal to the graph-level feature :attr:`graph_feat`. The operation is similar to ``numpy.repeat`` (or ``torch.repeat_interleave``). It is commonly used to normalize edge features by a global vector. For example, to normalize edge features across graph to range :

(graph, graph_feat, *, etype=None)

Source from the content-addressed store, hash-verified

449
450
451def broadcast_edges(graph, graph_feat, *, etype=None):
452 """Generate an edge feature equal to the graph-level feature :attr:`graph_feat`.
453
454 The operation is similar to ``numpy.repeat`` (or ``torch.repeat_interleave``).
455 It is commonly used to normalize edge features by a global vector. For example,
456 to normalize edge features across graph to range :math:`[0~1)`:
457
458 >>> g = dgl.batch([...]) # batch multiple graphs
459 >>> g.edata['h'] = ... # some node features
460 >>> h_sum = dgl.broadcast_edges(g, dgl.sum_edges(g, 'h'))
461 >>> g.edata['h'] /= h_sum # normalize by summation
462
463 Parameters
464 ----------
465 graph : DGLGraph
466 The graph.
467 graph_feat : tensor
468 The feature to broadcast. Tensor shape is :math:`(B, *)` for batched graph,
469 where :math:`B` is the batch size.
470 etype : str, typle of str, optional
471 Edge type. Can be omitted if there is only one edge type in the graph.
472
473 Returns
474 -------
475 Tensor
476 The edge features tensor with shape :math:`(M, *)`, where :math:`M` is the
477 number of edges.
478
479 Examples
480 --------
481
482 >>> import dgl
483 >>> import torch as th
484
485 Create two :class:`~dgl.DGLGraph` objects and initialize their
486 edge features.
487
488 >>> g1 = dgl.graph(([0], [1])) # Graph 1
489 >>> g2 = dgl.graph(([0, 1], [1, 2])) # Graph 2
490 >>> bg = dgl.batch([g1, g2])
491 >>> feat = th.rand(2, 5)
492 >>> feat
493 tensor([[0.4325, 0.7710, 0.5541, 0.0544, 0.9368],
494 [0.2721, 0.4629, 0.7269, 0.0724, 0.1014]])
495
496 Broadcast feature to all edges in the batched graph, feat[i] is broadcast to edges
497 in the i-th example in the batch.
498
499 >>> dgl.broadcast_edges(bg, feat)
500 tensor([[0.4325, 0.7710, 0.5541, 0.0544, 0.9368],
501 [0.2721, 0.4629, 0.7269, 0.0724, 0.1014],
502 [0.2721, 0.4629, 0.7269, 0.0724, 0.1014]])
503
504 Broadcast feature to all edges in the single graph (the feature tensor shape
505 to broadcast should be :math:`(1, *)`).
506
507 >>> feat1 = th.unsqueeze(feat[1], 0)
508 >>> dgl.broadcast_edges(g2, feat1)

Callers

nothing calls this directly

Calls 3

dgl_warningFunction · 0.85
batch_num_edgesMethod · 0.80
shapeMethod · 0.45

Tested by

no test coverage detected