GNN图级任务代码实现,多分类,使用数据集进行训练、验证
时间: 2023-12-21 13:07:08 浏览: 119
车辆分类数据集,数千张数据,已经划分好训练集和验证集
5星 · 资源好评率100%
这里提供一个基于PyTorch的GNN图级任务的代码实现,包括数据集的加载、模型的定义和训练、验证等操作,供参考:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from sklearn.metrics import accuracy_score
from dgl import DGLGraph
import dgl.function as fn
# 定义数据集类
class GraphDataset(Dataset):
def __init__(self, graphs, labels):
self.graphs = graphs
self.labels = labels
def __getitem__(self, idx):
return self.graphs[idx], self.labels[idx]
def __len__(self):
return len(self.labels)
# 定义GNN模型
class GNN(nn.Module):
def __init__(self, in_feats, hidden_feats, out_feats):
super(GNN, self).__init__()
self.conv1 = nn.GraphConv(in_feats, hidden_feats)
self.conv2 = nn.GraphConv(hidden_feats, out_feats)
def forward(self, graph):
h = graph.ndata['feat']
h = self.conv1(graph, h)
h = torch.relu(h)
h = self.conv2(graph, h)
return h.mean(0)
# 定义训练函数
def train(model, dataloader, optimizer, criterion, device):
model.train()
for i, (graphs, labels) in enumerate(dataloader):
graphs = [graph.to(device) for graph in graphs]
labels = labels.to(device)
logits = model(graphs)
loss = criterion(logits, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 定义验证函数
def evaluate(model, dataloader, device):
model.eval()
with torch.no_grad():
all_labels = []
all_logits = []
for graphs, labels in dataloader:
graphs = [graph.to(device) for graph in graphs]
labels = labels.numpy()
logits = model(graphs).cpu().numpy()
all_labels.append(labels)
all_logits.append(logits)
all_labels = np.concatenate(all_labels)
all_logits = np.concatenate(all_logits)
acc = accuracy_score(all_labels, np.argmax(all_logits, axis=1))
return acc
# 加载数据集
graphs = [...] # 图数据,每个元素为一个DGLGraph对象
labels = [...] # 标签数据
train_dataset = GraphDataset(graphs[:800], labels[:800])
val_dataset = GraphDataset(graphs[800:], labels[800:])
train_dataloader = DataLoader(train_dataset, batch_size=32, shuffle=True)
val_dataloader = DataLoader(val_dataset, batch_size=32, shuffle=False)
# 定义模型、优化器、损失函数和设备
model = GNN(in_feats=10, hidden_feats=16, out_feats=3)
optimizer = optim.Adam(model.parameters())
criterion = nn.CrossEntropyLoss()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
# 训练模型
for epoch in range(10):
train(model, train_dataloader, optimizer, criterion, device)
acc = evaluate(model, val_dataloader, device)
print('Epoch {}, validation accuracy: {:.4f}'.format(epoch, acc))
```
在这个例子中,我们使用了一个简单的GNN模型,包含两层GraphConv层。数据集包含多个图,每个图的节点特征维度为10,类型数为3。我们使用Adam优化器和交叉熵损失函数进行训练,并在验证集上计算准确率。
阅读全文