图神经网络(task_01)


from torch_geometric.datasets import KarateClub

引入的数据集


dataset = KarateClub()

data = dataset[0] # Get the first graph object.

print(data)

print('==============================================================')

# 获取图的⼀些信息

print(f'Number of nodes: {data.num_nodes}') # 节点数量

print(f'Number of edges: {data.num_edges}') # 边数量

print(f'Number of node features: {data.num_node_features}') # 节点属性的维度

print(f'Number of node features: {data.num_features}') # 同样是节点属性的维度

print(f'Number of edge features: {data.num_edge_features}') # 边属性的维度

print(f'Average node degree: {data.num_edges / data.num_nodes:.2f}') # 平均节点度

print(f'if edge indices are ordered and do not contain duplicate entries.:{data.is_coalesced()}') # 是否边是有序的同时不含有重复的边

print(f'Number of training nodes: {data.train_mask.sum()}') # ⽤作训练集的节点

print(f'Training node label rate: {int(data.train_mask.sum()) /data.num_nodes:.2f}') # ⽤作训练集的节点数占⽐

print(f'Contains isolated nodes: {data.contains_isolated_nodes()}') # 此图是否包含孤⽴的节点

print(f'Contains self-loops: {data.contains_self_loops()}') # 此图是否包含⾃环的边

print(f'Is undirected: {data.is_undirected()}') # 此图是否是⽆向图

Data(edge_index=[2, 156], train_mask=[34], x=[34, 34], y=[34])

==============================================================

Number of nodes: 34

Number of edges: 156

Number of node features: 34

Number of node features: 34

Number of edge features: 0

Average node degree: 4.59

if edge indices are ordered and do not contain duplicate entries.:True

Number of training nodes: 4

Training node label rate: 0.12

Contains isolated nodes: False

Contains self-loops: False

Is undirected: True

from torch_geometric.datasets import Planetoid


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

# Cora()

len(dataset)

# 1

dataset.num_classes

# 7

dataset.num_node_features

# 1433

Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.x

Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.tx

Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.allx

Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.y

Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.ty

Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.ally

Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.graph

Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.test.index

Processing...

Done!



1433

Cora数据集


data = dataset[0]

# Data(edge_index=[2, 10556], test_mask=[2708],

# train_mask=[2708], val_mask=[2708], x=[2708, 1433], y=[2708])

print(data.is_undirected())

# True

print(data.train_mask.sum().item())

# 140

print(data.val_mask.sum().item())

# 500

print(data.test_mask.sum().item())

True

140

500

1000

print(data.x.size())

torch.Size([2708, 1433])

 from torch_geometric.data.data import Data

# 请通过继承Data 类实现⼀个类,专⻔⽤于表示“机构-作者-论⽂”的⽹络。该⽹络包含“机构“、”作者“和”论⽂”

# 三类节点,以及“作者-机构“和“作者-论⽂“两类边。对要实现的类的要求:1)⽤不同的属性存储不同节点的属

# 性;2)⽤不同的属性存储不同的边(边没有属性);3)逐⼀实现获取不同节点数量的⽅法。

class Network(Data):

    def __init__(self, org=None,author=None,article=None,

                 oa_edge_index=None,aa_edge_index=None,  **kwargs):

                 # edge_attr=None, y=None, **kwargs):

        r"""

        Args:

        org (Tensor, optional): 节点属性矩阵,⼤⼩为`[num_nodes, num_node_features]`

        author (Tensor, optional): 节点属性矩阵,⼤⼩为`[num_nodes, num_node_features]`

        article (Tensor, optional): 节点属性矩阵,⼤⼩为`[num_nodes, num_node_features]`

        oa_edge_index (LongTensor, optional): 边索引矩阵,⼤⼩为`[2, num_edges]`,第0⾏为尾节点,第1⾏为头节点,头指向尾

        aa_edge_index (LongTensor, optional): 边索引矩阵,⼤⼩为`[2, num_edges]`,第0⾏为尾节点,第1⾏为头节点,头指向尾

        """

        self.org = org

        self.author = author

        self.article = article

        self.oa_edge_index = oa_edge_index

        self.aa_edge_index = aa_edge_index

        for key, item in kwargs.items():

            if key == 'num_nodes':

                self.__num_nodes__ = item

            else:

                self[key] = item

    @property

    def num_org_nodes(self):

        if self.org is None:

            return 0

        else:

            return self.org.shape[0]

    @property

    def num_author_nodes(self):

        if self.author is None:

            return 0

        else:

            return self.author.shape[0]

    @property

    def num_article_nodes(self):

        if self.article is None:

            return 0

        else:

            return self.article.shape[0]


network = Network(org=data.x,author=data.x,article=data.x,oa_edge_index=data.edge_index,aa_edge_index = data.edge_index)


print('单位的数量:',network.num_org_nodes)

print('作者数量:',network.num_author_nodes)

print('文章数量',network.num_article_nodes)