dgl.DGLGraph.apply_nodes
- DGLGraph.apply_nodes(func, v='__ALL__', ntype=None)[源码]
使用提供的函数更新指定节点的特征。
- 参数:
示例
以下示例使用 PyTorch 后端。
>>> import dgl >>> import torch
同构图
>>> g = dgl.graph(([0, 1, 2, 3], [1, 2, 3, 4])) >>> g.ndata['h'] = torch.ones(5, 2) >>> g.apply_nodes(lambda nodes: {'x' : nodes.data['h'] * 2}) >>> g.ndata['x'] tensor([[2., 2.], [2., 2.], [2., 2.], [2., 2.], [2., 2.]])
异构图
>>> g = dgl.heterograph({('user', 'follows', 'user'): ([0, 1], [1, 2])}) >>> g.nodes['user'].data['h'] = torch.ones(3, 5) >>> g.apply_nodes(lambda nodes: {'h': nodes.data['h'] * 2}, ntype='user') >>> g.nodes['user'].data['h'] tensor([[2., 2., 2., 2., 2.], [2., 2., 2., 2., 2.], [2., 2., 2., 2., 2.]])
另请参阅