当前位置:网站首页>import torch_ Data view of geometric

import torch_ Data view of geometric

2022-06-12 13:05:00 Dongxuan

​​​​​​ Welcome to the column torch_geometric

 

Data Handling of Graphs

A processing library torch_geometric. I also found this class when watching others share code , It is out of place with the usual class .

 torch_geometric.data.Data, It can simply represent a graph data . It contains the following properties

  • data.x: Node characteristics [ Number of nodes , Node feature dimension ] [num_nodes, num_node_features]

  • data.edge_index: It has to be this shape[2, num_edges] and type torch.long. Must be serial number . First of all, yours. data.x It determines the order of each node index . Then specify the edges according to this sequence number , The first row of the edge is the source node , The second node is the target node .

  • data.edge_attr: Feature representation of edges  [num_edges, num_edge_features]

  • data.y: A label for graph data , It can be a node classification task  [num_nodes, *] , It can also be a graph classification task  [1, *]

  • data.pos: The coordinates of point , For example, the point is in the three-dimensional coordinate system ,num_dimensions Namely 3 [num_nodes, num_dimensions]

We can expand Data List of properties , For example  data.face  To express 3d In the clouds face The concept of . Use the sequence number of points to specify which three points form a face . [3, num_faces] and type torch.long.

A bunch of runnable examples

Example ① 

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])

perhaps edge_index The second way to write

Example ②

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])

Let's output the built graph data information .

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)
原网站

版权声明
本文为[Dongxuan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121250499576.html