当前位置:网站首页>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
边栏推荐
- 【yarn】CDP集群 Yarn配置capacity调度器批量分配
- Dependency in dependencymanagement cannot be downloaded and red is reported
- 4、安装部署Spark(Spark on Yarn模式)
- FreeRTOS 任务函数里面的死循环
- 機器學習--線性回歸(sklearn)
- Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
- Using LinkedHashMap to realize the caching of an LRU algorithm
- STM32 如何定位导致发生 hard fault 的代码段
- 常用正则表达式整理
- 高通&MTK&麒麟 手机平台USB3.0方案对比
猜你喜欢

第4阶段 Mysql数据库

Wangeditor rich text reference and table usage
![C language callback function [C language]](/img/7b/910016123738240e24549ddea8a162.png)
C language callback function [C language]

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

Redis面试题

Implementation scheme of distributed transaction

RT-Thread的main线程“卡死”的一种可能原因及解决方案

Mysql的索引实现之B树和B+树

RT-Thread API参考手册

FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
随机推荐
SQL time injection
JS array + array method reconstruction
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
[template] KMP string matching
高通&MTK&麒麟 手機平臺USB3.0方案對比
Matlab learning and actual combat notes
Contiki source code + principle + function + programming + transplantation + drive + network (turn)
2019腾讯暑期实习生正式笔试
Implementation scheme of distributed transaction
mysql实现读写分离
关键字 inline (内联函数)用法解析【C语言】
nodejs连接Mysql
选择法排序与冒泡法排序【C语言】
分布式節點免密登錄
Those commonly used tool classes and methods in hutool
arduino JSON数据信息解析
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
物联网系统框架学习
Oppo vooc fast charging circuit and protocol
STM32 如何定位导致发生 hard fault 的代码段