当前位置:网站首页>import torch_ Geometric first graph network example
import torch_ Geometric first graph network example
2022-06-12 13:06:00 【Dongxuan】
Learning Methods on Graphs
Cora citation Get one on the dataset GCN layer
For a high-level explanation on GCN, have a look at its blog post.
① Load data set
from torch_geometric.datasets import Planetoid
dataset = Planetoid(root='/tmp/Cora', name='Cora')
>>> Cora()Build a two-tier GCN The Internet
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)It is so simple , It's all in one batch Convolution of graphs on large graphs !!!!!!!!!\
Start this network ,train 200 round
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()After training , Test in the test set :
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!
边栏推荐
- Microsoft Word tutorial, how to insert a header or footer in word?
- 号称下一代监控系统!来看看它有多牛逼
- It is enough to read this article. Web Chinese development
- "New continent" of mobile application going to sea
- Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver
- Mui login database improvement and Ajax asynchronous processing [mui+flask+mongodb+hbuilderx]
- LeetCode滑动窗口刷题总结
- Theoretical knowledge of improved DH parameters and standard DH parameters of manipulator
- Actual combat | realizing monocular camera ranging by skillfully using pose solution
- A brief introduction to Verilog mode
猜你喜欢

Embedded driver design

分享PDF高清版,系列篇

关于派文的问题
![Will the next star of PPT for workplace speech be you [perfect summary] at the moment](/img/11/ac67db2641f42ef3d09417b790feb8.png)
Will the next star of PPT for workplace speech be you [perfect summary] at the moment

Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题

It is enough to read this article. Web Chinese development

嵌入式系統硬件構成-基於ARM的嵌入式開發板介紹

Experience and learning path of introductory deep learning and machine learning

移动应用出海的“新大陆”

用PyTorch进行语义分割
随机推荐
JVM 运行时参数
【云原生 | Kubernetes篇】深入了解Ingress
C语言【23道】经典面试题【下】
VGA显示彩条和图片(FPGA)
号称下一代监控系统!来看看它有多牛逼
LeetCode滑动窗口刷题总结
[wechat applet development] Part 1: development tool installation and program configuration
[you code, I fix] whitesource was officially renamed mend
VNCTF2022 [WEB]
Improve pipeline efficiency: you need to know how to identify the main obstacles in ci/cd pipeline
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
Successful job hopping Ali, advanced learning
5V升压到12.6V的锂电池充电IC芯片方案FS4062B
Openstack network
功能标记是什么?一文了解它的作用,以及它的最佳实践
【云原生 | Kubernetes篇】Ingress案例实战
干货满满,这些知识你必须拿下
Binary tree (construction)
下一个职场演讲PPT的明星,会不会是此刻的你【完美总结】
Source of routing information