• 【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

  • 相关阅读:
    企业云性能监控
    设计模式2、抽象工厂模式 Abstract Factory
    F3L600R10W4S7FC22BPSA1 EasyPACK IGBT模块 950V(F3L600R10W4S7FC22)
    Reids实战——优惠券秒杀(全局唯一ID生成策略)
    DDD - 来自听众的16个DDD问题,美团技术团队是这样回答的
    第三代高级语言
    Web App、Hybrid App、Native App 横向对比
    继电器测试负载箱的价格和性价比如何?
    Api 接口优化有哪些技巧?
    vue serve及其与vue-cli-service serve之间的关系
  • 原文地址:https://blog.csdn.net/zfhsfdhdfajhsr/article/details/126479076