当前位置:网站首页>import torch_geometric 的Data 查看
import torch_geometric 的Data 查看
2022-06-12 12:51:00 【冬炫】
欢迎关注专栏torch_geometric

Data Handling of Graphs
一个处理图库的torch_geometric. 我也是看别人共享代码时发现了这个类,和平常的类格格不入。
torch_geometric.data.Data, 它可以简单的表示一个图数据。其含有以下属性
data.x: 节点特征[节点个数,节点特征维度][num_nodes, num_node_features]data.edge_index: 必须是这个shape[2, num_edges]and typetorch.long。必须是序号性质的。首先你的data.x 就决定了各个节点的顺序index 。然后按照这个序号指定边,边的第一行是源节点,第二个节点是目标节点。data.edge_attr: 边的特征表示[num_edges, num_edge_features]data.y: 一个图数据的标签,可以是节点分类任务[num_nodes, *],也可以是图分类任务[1, *]data.pos: 点的坐标,如点在三维坐标系,num_dimensions 就是3[num_nodes, num_dimensions]
我们可以扩展Data 的属性列表,比如起个 data.face 以表达3d点云中face 的概念。利用点的序号指定哪三个点组成一个面。 [3, num_faces] and type torch.long.
一堆可运行的例子
例子①
import torch
from torch_geometric.data import Data
edge_index = torch.tensor([[0, 1, 1, 2],
[1, 0, 2, 1]], dtype=torch.long)
x = torch.tensor([[-1], [0], [1]], dtype=torch.float)
data = Data(x=x, edge_index=edge_index)
>>> Data(edge_index=[2, 4], x=[3, 1])或者edge_index 的第二种写法
例子②
import torch
from torch_geometric.data import Data
edge_index = torch.tensor([[0, 1],
[1, 0],
[1, 2],
[2, 1]], dtype=torch.long)
x = torch.tensor([[-1], [0], [1]], dtype=torch.float)
data = Data(x=x, edge_index=edge_index.t().contiguous())
>>> Data(edge_index=[2, 4], x=[3, 1])让我们输出构建的图数据信息。
print(data.keys)
>>> ['x', 'edge_index']
print(data['x'])
>>> tensor([[-1.0],
[0.0],
[1.0]])
for key, item in data:
print(f'{key} found in data')
>>> x found in data
>>> edge_index found in data
'edge_attr' in data
>>> False
data.num_nodes
>>> 3
data.num_edges
>>> 4
data.num_node_features
>>> 1
data.has_isolated_nodes()
>>> False
data.has_self_loops()
>>> False
data.is_directed()
>>> False
# Transfer data object to GPU.
device = torch.device('cuda')
data = data.to(device)边栏推荐
猜你喜欢

Help you with everything from the basics to the source code. Introduce the technology in detail

Design virtual network to realize communication between virtual machine instance and external network

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

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

Constant time delete / find any element in array

Geek challenge 2021 Web

机械臂改进的DH参数与标准DH参数理论知识

八大误区,逐个击破(2):性能差?应用程序少?你对云的这些担心很多余!

Summary of question brushing in leetcode sliding window

从基础到源码统统帮你搞定,技术详细介绍
随机推荐
Three dimensional coordinate point fitting sphere (MATLAB and C)
Embedded system hardware composition - embedded system hardware architecture
The 4th Zhejiang CTF preliminary contest web pppop
分享PDF高清版,系列篇
Tuples, arrays, and as const of typescript
功能标记是什么?一文了解它的作用,以及它的最佳实践
Dasctf Sept x Zhejiang University of technology autumn challenge Web
[HXBCTF 2021]easywill
unittest框架
hudi 键的生成(Key Generation)
LeetCode滑动窗口刷题总结
二叉树(纲领篇)
Volume mount and mirror creation
C语言【23道】经典面试题【下】
Buu question brushing record - 7
Summary of question brushing in leetcode sliding window
What is the function tag? Article to understand its role and its best practices
一行代码实现shell if else逻辑
Newoj week 10 question solution
Iterator, generator generator details