当前位置:网站首页>The first simple case of GNN: Cora classification
The first simple case of GNN: Cora classification
2022-07-06 11:59:00 【Want to be a kite】
GNN–Cora classification
Cora The dataset is GNN A classic dataset in , take 2708 The papers are divided into seven categories :1) Based on the case 、2) Genetic algorithm (ga) 、3) neural network 、4) Probability method 、5)、 Reinforcement learning 、6) Rule learning 、7) theory . Each paper is regarded as a node , Each node has 1433 Features .
import os
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch_geometric.datasets import Planetoid
import torch_geometric.nn as pyg_nn
#load Cora dataset
def get_data(root_dir='D:\Python\python_dataset\GNN_Dataset\Cora',data_name='Cora'):
Cora_dataset = Planetoid(root=root_dir,name=data_name)
print(Cora_dataset)
return Cora_dataset
Cora_dataset = get_data()
print(Cora_dataset.num_classes,Cora_dataset.num_node_features,Cora_dataset.num_edge_features)
print(Cora_dataset.data)
Cora()
7 1433 0
Data(x=[2708, 1433], edge_index=[2, 10556], y=[2708], train_mask=[2708], val_mask=[2708], test_mask=[2708])
The code gives GCN、GAT、SGConv、ChebConv、SAGEConv Simple implementation of
import os
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch_geometric.datasets import Planetoid
import torch_geometric.nn as pyg_nn
#load Cora dataset
def get_data(root_dir='D:\Python\python_dataset\GNN_Dataset\Cora',data_name='Cora'):
Cora_dataset = Planetoid(root=root_dir,name=data_name)
print(Cora_dataset)
return Cora_dataset
#create the Graph cnn model
""" 2-GATConv """
# class GATConv(nn.Module):
# def __init__(self,in_c,hid_c,out_c):
# super(GATConv,self).__init__()
# self.GATConv1 = pyg_nn.GATConv(in_channels=in_c,out_channels=hid_c)
# self.GATConv2 = pyg_nn.GATConv(in_channels=hid_c, out_channels=hid_c)
#
# def forward(self,data):
# x = data.x
# edge_index = data.edge_index
# hid = self.GATConv1(x=x,edge_index=edge_index)
# hid = F.relu(hid)
#
# out = self.GATConv2(hid,edge_index=edge_index)
# out = F.log_softmax(out,dim=1)
#
# return out
""" 2-SAGE 0.788 """
# class SAGEConv(nn.Module):
# def __init__(self,in_c,hid_c,out_c):
# super(SAGEConv,self).__init__()
# self.SAGEConv1 = pyg_nn.SAGEConv(in_channels=in_c,out_channels=hid_c)
# self.SAGEConv2 = pyg_nn.SAGEConv(in_channels=hid_c, out_channels=hid_c)
#
# def forward(self,data):
# x = data.x
# edge_index = data.edge_index
# hid = self.SAGEConv1(x=x,edge_index=edge_index)
# hid = F.relu(hid)
#
# out = self.SAGEConv2(hid,edge_index=edge_index)
# out = F.log_softmax(out,dim=1)
#
# return out
""" 2-SGConv 0.79 """
class SGConv(nn.Module):
def __init__(self,in_c,hid_c,out_c):
super(SGConv,self).__init__()
self.SGConv1 = pyg_nn.SGConv(in_channels=in_c,out_channels=hid_c)
self.SGConv2 = pyg_nn.SGConv(in_channels=hid_c, out_channels=hid_c)
def forward(self,data):
x = data.x
edge_index = data.edge_index
hid = self.SGConv1(x=x,edge_index=edge_index)
hid = F.relu(hid)
out = self.SGConv2(hid,edge_index=edge_index)
out = F.log_softmax(out,dim=1)
return out
""" 2-ChebConv """
# class ChebConv(nn.Module):
# def __init__(self,in_c,hid_c,out_c):
# super(ChebConv,self).__init__()
#
# self.ChebConv1 = pyg_nn.ChebConv(in_channels=in_c,out_channels=hid_c,K=1)
# self.ChebConv2 = pyg_nn.ChebConv(in_channels=hid_c,out_channels=out_c,K=1)
#
# def forward(self,data):
# x = data.x
# edge_index = data.edge_index
# hid = self.ChebConv1(x=x,edge_index=edge_index)
# hid = F.relu(hid)
#
# out = self.ChebConv2(hid,edge_index=edge_index)
# out = F.log_softmax(out,dim=1)
#
# return out
""" 2-GCN """
# class GraphCNN(nn.Module):
# def __init__(self, in_c,hid_c,out_c):
# super(GraphCNN,self).__init__()
#
# self.conv1 = pyg_nn.GCNConv(in_channels=in_c,out_channels=hid_c)
# self.conv2 = pyg_nn.GCNConv(in_channels=hid_c,out_channels=out_c)
#
# def forward(self,data):
# #data.x,data.edge_index
# x = data.x # [N,C]
# edge_index = data.edge_index # [2,E]
# hid = self.conv1(x=x,edge_index=edge_index) #[N,D]
# hid = F.relu(hid)
#
# out = self.conv2(hid,edge_index=edge_index) # [N,out_c]
#
# out = F.log_softmax(out,dim=1)
#
# return out
def main():
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
Cora_dataset = get_data()
#my_net = GATConv(in_c=Cora_dataset.num_node_features, hid_c=100, out_c=Cora_dataset.num_classes)
#my_net = SAGEConv(in_c=Cora_dataset.num_node_features, hid_c=40, out_c=Cora_dataset.num_classes)
my_net = SGConv(in_c=Cora_dataset.num_node_features,hid_c=100,out_c=Cora_dataset.num_classes)
#my_net = ChebConv(in_c=Cora_dataset.num_node_features,hid_c=20,out_c=Cora_dataset.num_classes)
# my_net = GraphCNN(in_c=Cora_dataset.num_node_features,hid_c=12,out_c=Cora_dataset.num_classes)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
my_net = my_net.to(device)
data = Cora_dataset[0].to(device)
optimizer = torch.optim.Adam(my_net.parameters(),lr=1e-3)
#model train
my_net.train()
for epoch in range(500):
optimizer.zero_grad()
output = my_net(data)
loss = F.nll_loss(output[data.train_mask],data.y[data.train_mask])
loss.backward()
optimizer.step()
print("Epoch",epoch+1,"Loss",loss.item())
#model test
my_net.eval()
_,prediction = my_net(data).max(dim=1)
target = data.y
test_correct = prediction[data.test_mask].eq(target[data.test_mask]).sum().item()
test_number = data.test_mask.sum().item()
print("Accuracy of Test Sample:",test_correct/test_number)
if __name__ == '__main__':
main()
Cora()
Epoch 1 Loss 4.600048542022705
Epoch 2 Loss 4.569146156311035
Epoch 3 Loss 4.535804271697998
Epoch 4 Loss 4.498434543609619
Epoch 5 Loss 4.456351280212402
Epoch 6 Loss 4.409425258636475
Epoch 7 Loss 4.357522964477539
Epoch 8 Loss 4.3007612228393555
Epoch 9 Loss 4.2392096519470215
Epoch 10 Loss 4.172731876373291
Epoch 11 Loss 4.101400375366211
Epoch 12 Loss 4.025243282318115
...............
Epoch 494 Loss 0.004426263272762299
Epoch 495 Loss 0.004407935775816441
Epoch 496 Loss 0.004389731213450432
Epoch 497 Loss 0.004371633753180504
Epoch 498 Loss 0.004353662021458149
Epoch 499 Loss 0.0043357922695577145
Epoch 500 Loss 0.004318032879382372
Accuracy of Test Sample: 0.794
边栏推荐
- XML文件详解:XML是什么、XML配置文件、XML数据文件、XML文件解析教程
- I2C总线时序详解
- I2C bus timing explanation
- 高通&MTK&麒麟 手機平臺USB3.0方案對比
- Connexion sans mot de passe du noeud distribué
- Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries(XGBoost)
- RT-Thread 线程的时间片轮询调度
- C语言回调函数【C语言】
- B tree and b+ tree of MySQL index implementation
- 【flink】flink学习
猜你喜欢

FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation

Correspondence between STM32 model and contex M

机器学习--决策树(sklearn)

STM32 如何定位导致发生 hard fault 的代码段

Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0

IOT system framework learning

Apprentissage automatique - - régression linéaire (sklearn)

mysql实现读写分离

Linux yum安装MySQL

Connexion sans mot de passe du noeud distribué
随机推荐
SQL time injection
Encodermappreduce notes
Gallery's image browsing and component learning
Variable star user module
荣耀Magic 3Pro 充电架构分析
C语言函数之可变参数原理:va_start、va_arg及va_end
小L的试卷
Internet protocol details
电商数据分析--用户行为分析
物联网系统框架学习
MySQL主从复制的原理以及实现
Raspberry pie tap switch button to use
Small L's test paper
Redis interview questions
Wangeditor rich text reference and table usage
Basic use of pytest
XML file explanation: what is XML, XML configuration file, XML data file, XML file parsing tutorial
{one week summary} take you into the ocean of JS knowledge
C language callback function [C language]
Word排版(小計)