2021-06-13

PyG

  • PyTorch geometric

  • 图(Graph)是用于建立对象(节点)之间的成对关系,或者说是 “ 边关系 " 的模型。

https://pytorch-geometric.readthedocs.io/en/latest/

1.环境配置

conda create -n e python=3.8
conda activate e

conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c conda-forge

pip install torch-scatter -f https://pytorch-geometric.com/whl/torch-1.8.0+cu111.html
pip install torch-sparse -f https://pytorch-geometric.com/whl/torch-1.8.0+cu111.html
pip install torch-cluster -f https://pytorch-geometric.com/whl/torch-1.8.0+cu111.html
pip install torch-spline-conv -f https://pytorch-geometric.com/whl/torch-1.8.0+cu111.html
pip install torch-geometric

2. 项目组织结构

./dataset/Cora/raw/...             # Cora引文数据集,未处理过的。
./dataset/Cora/processed/...       

./main.py

Data

构造函数

class Data(object):

    def __init__(self, x=None, edge_index=None, edge_attr=None, y=None, **kwargs):
        r"""
        Args:
            x (Tensor, optional): 节点属性矩阵,大小为`[num_nodes, num_node_features]`
            edge_index (LongTensor, optional): 边索引矩阵,大小为`[2, num_edges]`,第0行为尾节点,第1行为头节点,尾指向头
            edge_attr (Tensor, optional): 边属性矩阵,大小为`[num_edges, num_edge_features]`
            y (Tensor, optional): 节点或图的标签,任意大小(,其实也可以是边的标签)

        """
        self.x = x
        self.edge_index = edge_index
        self.edge_attr = edge_attr
        self.y = y

        for key, item in kwargs.items():
            if key == 'num_nodes':
                self.__num_nodes__ = item
            else:
                self[key] = item

类型转换

@classmethod
def from_dict(cls, dictionary):
    r"""Creates a data object from a python dictionary."""
    data = cls()
    for key, item in dictionary.items():
        data[key] = item

    return data

Dataset

from torch_geometric.datasets import Planetoid

dataset = Planetoid(root='/dataset', name='Cora')
# Cora()

len(dataset)
# 1

dataset.num_classes
# 7

dataset.num_node_features
# 1433
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值