• 【dgl学习】dgl处理图中的节点/边的属性/特征/类型


    1 dgl.DGLGraph.nodes

      函数的主要功能有两个

    • 返回图中单个节点类型的ID
    • 获取或者设置单个节点类型的特征

    主要有两个应用同构图和异构图:

    1.1 举个例子

    import dgl
    import torch
    
    # 定义同构图
    g = dgl.graph((torch.tensor([0, 1]), torch.tensor([1, 2])))
    # 定义异构图
    hg = dgl.heterograph({
        ('user', 'follows', 'user'): (torch.tensor([0, 1]), torch.tensor([1, 2])),
        ('user', 'plays', 'game'): (torch.tensor([3, 4]), torch.tensor([5, 6]))
    })
    
    # 获取节点ID
    g.nodes()
    # tensor([0, 1, 2])
    
    hg.nodes('user')
    # tensor([0, 1, 2, 3, 4])
    
    ++++++++++++++++++++++
    # 设置获取节点特征
    hg.nodes['user'].data['h'] = torch.ones(5, 1)
    hg.nodes['user'].data['h']
    # tensor([[1.], [1.], [1.], [1.], [1.]])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    3 dgl.DGLGraph.edges

    见参考文献2

    dgl.DGLGraph.edata

    函数功能:

    • Return an edge data view for setting/getting edge features.(返回获取或者设置边的特征,下面是两种不同类型图的获取方式

    • Let g be a DGLGraph. If g is a graph of a single edge type, g.edata[feat] returns the edge feature associated with the name feat.通过g.edata[feat] 返回一个样例。

    • If g is a graph of multiple edge types, g.edata[feat] returns a dict[str, Tensor] mapping canonical edge types to the edge features associated with the name feat for the corresponding type. 通过g.edata[feat] 返回一个字典。

    • Notes : For setting features, the device of the features must be the same as the device of the graph.

    举个例子

    import dgl
    import torch
    
    # Set and get feature ‘h’ for a graph of a single edge type.
    g = dgl.graph((torch.tensor([0, 1]), torch.tensor([1, 2])))
    g.edata['h'] = torch.ones(2, 1)
    g.edata['h']
    
    # Set and get feature ‘h’ for a graph of multiple edge types.
    g = dgl.heterograph({
        ('user', 'follows', 'user'): (torch.tensor([1, 2]), torch.tensor([3, 4])),
        ('user', 'plays', 'user'): (torch.tensor([2, 2]), torch.tensor([1, 1])),
        ('player', 'plays', 'game'): (torch.tensor([2, 2]), torch.tensor([1, 1]))
    })
    g.edata['h'] = {('user', 'follows', 'user'): torch.zeros(2, 1),
                    ('user', 'plays', 'user'): torch.ones(2, 1)}
    g.edata['h']
    # {('user', 'follows', 'user'): tensor([[0.], [0.]]),
    # ('user', 'plays', 'user'): tensor([[1.], [1.]])}
     
    g.edata['h'] = {('user', 'follows', 'user'): torch.ones(2, 1)}
    g.edata['h']
    
    # {('user', 'follows', 'user'): tensor([[1.], [1.]]),
    # ('user', 'plays', 'user'): tensor([[1.], [1.]])}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    3 参考文献

    [1]dgl.DGLGraph.node
    [2]dgl.DGLGraph.edges
    [3]dgl.DGLGraph.edata

  • 相关阅读:
    反病毒网关
    jar增量打包
    STC15单片机-低功耗设计
    CMake常用命令(三)set命令
    一种异步延迟队列的实现方式
    (17)不重启服务动态调整RabbitMQ消费者数量
    Redis:实现全局唯一id
    LeetCode 面试题 04.02. 最小高度树
    12.3 实现模拟鼠标录制回放
    算法题:摆动序列
  • 原文地址:https://blog.csdn.net/zfhsfdhdfajhsr/article/details/126479076