PyG-GAT-Cora(在Cora数据集上应用GAT做节点分类)

文章目录

model.py

py 复制代码
import torch.nn as nn
from torch_geometric.nn import GATConv
import torch.nn.functional as F
class gat_cls(nn.Module):
    def __init__(self,in_dim,hid_dim,out_dim,dropout_size=0.5):
        super(gat_cls,self).__init__()
        self.conv1 = GATConv(in_dim,hid_dim)
        self.conv2 = GATConv(hid_dim,hid_dim)
        self.fc = nn.Linear(hid_dim,out_dim)
        self.relu  = nn.ReLU()
        self.dropout_size = dropout_size
    def forward(self,x,edge_index):
        x = self.conv1(x,edge_index)
        x = F.dropout(x,p=self.dropout_size,training=self.training)
        x = self.relu(x)
        x = self.conv2(x,edge_index)
        x = self.relu(x)
        x = self.fc(x)
        return x

main.py

py 复制代码
import torch
import torch.nn as nn
from torch_geometric.datasets import Planetoid
from model import gat_cls
import torch.optim as optim
dataset = Planetoid(root='./data/Cora', name='Cora')
print(dataset[0])
cora_data = dataset[0]

epochs = 50
lr = 1e-3
weight_decay = 5e-3
momentum = 0.5
hidden_dim = 128
output_dim = 7


net = gat_cls(cora_data.x.shape[1],hidden_dim,output_dim)
optimizer = optim.AdamW(net.parameters(),lr=lr,weight_decay=weight_decay)
#optimizer = optim.SGD(net.parameters(),lr = lr,momentum=momentum)
criterion = nn.CrossEntropyLoss()
print("****************Begin Training****************")
net.train()
for epoch in range(epochs):
    out = net(cora_data.x,cora_data.edge_index)
    optimizer.zero_grad()
    loss_train = criterion(out[cora_data.train_mask],cora_data.y[cora_data.train_mask])
    loss_val   = criterion(out[cora_data.val_mask],cora_data.y[cora_data.val_mask])
    loss_train.backward()
    print('epoch',epoch+1,'loss-train {:.2f}'.format(loss_train),'loss-val {:.2f}'.format(loss_val))
    optimizer.step()

net.eval()
out = net(cora_data.x,cora_data.edge_index)
loss_test = criterion(out[cora_data.test_mask],cora_data.y[cora_data.test_mask])
_,pred = torch.max(out,dim=1)
pred_label = pred[cora_data.test_mask]
true_label = cora_data.y[cora_data.test_mask]
acc = sum(pred_label==true_label)/len(pred_label)
print("****************Begin Testing****************")
print('loss-test {:.2f}'.format(loss_test),'acc {:.2f}'.format(acc))

参数设置

bash 复制代码
epochs = 50
lr = 1e-3
weight_decay = 5e-3
momentum = 0.5
hidden_dim = 128
output_dim = 7

运行图

相关推荐
阿坡RPA7 小时前
手搓MCP客户端&服务端:从零到实战极速了解MCP是什么?
人工智能·aigc
用户27784491049937 小时前
借助DeepSeek智能生成测试用例:从提示词到Excel表格的全流程实践
人工智能·python
机器之心7 小时前
刚刚,DeepSeek公布推理时Scaling新论文,R2要来了?
人工智能
算AI9 小时前
人工智能+牙科:临床应用中的几个问题
人工智能·算法
JavaEdge在掘金9 小时前
ssl.SSLCertVerificationError报错解决方案
python
我不会编程55510 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
凯子坚持 c10 小时前
基于飞桨框架3.0本地DeepSeek-R1蒸馏版部署实战
人工智能·paddlepaddle
老歌老听老掉牙10 小时前
平面旋转与交线投影夹角计算
python·线性代数·平面·sympy
满怀101510 小时前
Python入门(7):模块
python
无名之逆10 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust