当前位置:网站首页>import torch_geometric 第一个图网络例子
import torch_geometric 第一个图网络例子
2022-06-12 12:51:00 【冬炫】
Learning Methods on Graphs
Cora citation 数据集上搞一个GCN层
For a high-level explanation on GCN, have a look at its blog post.
①加载数据集
from torch_geometric.datasets import Planetoid
dataset = Planetoid(root='/tmp/Cora', name='Cora')
>>> Cora()构建一个两层的GCN网络
import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
class GCN(torch.nn.Module):
def __init__(self):
super().__init__()
self.conv1 = GCNConv(dataset.num_node_features, 16)
self.conv2 = GCNConv(16, dataset.num_classes)
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = F.relu(x)
x = F.dropout(x, training=self.training)
x = self.conv2(x, edge_index)
return F.log_softmax(x, dim=1)竟然如此之简单,整个就是在一张batch 大图上进行图卷积!!!!!!!!!\
启动这个网络,train 200 轮
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = GCN().to(device)
data = dataset[0].to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
model.train()
for epoch in range(200):
optimizer.zero_grad()
out = model(data)
loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
loss.backward()
optimizer.step()训练后,在测试集测试一下:
model.eval()
pred = model(data).argmax(dim=1)
correct = (pred[data.test_mask] == data.y[data.test_mask]).sum()
acc = int(correct) / int(data.test_mask.sum())
print(f'Accuracy: {acc:.4f}')
>>> Accuracy: 0.8150The easiest way to learn more about Graph Neural Networks is to study the examples in the examples/ directory and to browse torch_geometric.nn. Happy hacking!
边栏推荐
- this.$ How to solve the problem when refs is undefined?
- 移动应用出海的“新大陆”
- The 4th Zhejiang CTF preliminary contest web pppop
- Attack and defense world re (New 1 hand zone) questions 1-12
- list和dict的应用
- 嵌入式系统概述2-嵌入式系统组成和应用
- 配准后图像对比函数itk::CheckerBoardImageFilter
- 用PyTorch进行语义分割
- Getting to know blob objects
- Detect whether the vector has an intersection
猜你喜欢

Theoretical knowledge of improved DH parameters and standard DH parameters of manipulator

嵌入式系统概述1-嵌入式系统定义、特点和发展历程

嵌入式驱动程序设计

深度学习的多个 loss 是如何平衡的?

Microsoft Word tutorial, how to insert a header or footer in word?

Embedded driver design

442个作者100页论文!谷歌耗时2年发布大模型新基准BIG-Bench | 开源
![Brush questions [de1ctf 2019]shellshellshell](/img/73/00782a567e6596eb4b561b69142277.jpg)
Brush questions [de1ctf 2019]shellshellshell

Microsoft Word 教程,如何在 Word 中插入页眉或页脚?

Simple picture preview
随机推荐
Binary tree (serialization)
从基础到源码统统帮你搞定,技术详细介绍
嵌入式驱动程序设计
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
Buu question brushing record - 5
ITK 原图种子点经过roi、降采样后index的变化
机械臂改进的DH参数与标准DH参数理论知识
R语言可视化分面图、假设检验、多变量分组t检验、可视化多变量分组分面条形图(faceting bar plot)并添加显著性水平、添加抖动数据点(jitter points)
一个ES设置操作引发的“血案”
Embedded driver design
下一个职场演讲PPT的明星,会不会是此刻的你【完美总结】
机械臂雅可比矩阵IK
STM32F1与STM32CubeIDE编程实例-设备驱动-EEPROM-AT24C256驱动
多源BFS问题 模板(附题)
unittest框架
Design virtual network to realize communication between virtual machine instance and external network
this.$ How to solve the problem when refs is undefined?
【云原生 | Kubernetes篇】深入了解Ingress
Vant tab bar + pull-up loading + pull-down refresh demo van tabs + van pull refresh + van list demo
Chaotic engineering practice of distributed kV storage in station B