torch_geometric.data.Data

class Data(x: Optional[Tensor] = None, edge_index: Optional[Tensor] = None, edge_attr: Optional[Tensor] = None, y: Optional[Union[Tensor, int, float]] = None, pos: Optional[Tensor] = None, time: Optional[Tensor] = None, **kwargs)[source]

Bases: BaseData, FeatureStore, GraphStore

描述同构图的数据对象。 该数据对象可以保存节点级别、链接级别和图级别的属性。 通常,Data 试图模仿常规的 字典的行为。 此外,它提供了用于分析图结构的有用功能,并提供了基本的 PyTorch 张量功能。 请参阅 这里 以获取相关教程。

from torch_geometric.data import Data

data = Data(x=x, edge_index=edge_index, ...)

# Add additional arguments to `data`:
data.train_idx = torch.tensor([...], dtype=torch.long)
data.test_mask = torch.tensor([...], dtype=torch.bool)

# Analyzing the graph structure:
data.num_nodes
>>> 23

data.is_directed()
>>> False

# PyTorch tensor functionality:
data = data.pin_memory()
data = data.to('cuda:0', non_blocking=True)
Parameters:
  • x (torch.Tensor, optional) – 节点特征矩阵,形状为 [num_nodes, num_node_features]。(默认值:None

  • edge_index (LongTensor, optional) – 以COO格式表示的图连接性,形状为 [2, num_edges]。(默认值:None

  • edge_attr (torch.Tensor, optional) – 边特征矩阵,形状为 [num_edges, num_edge_features]。(默认值:None

  • y (torch.Tensor, optional) – 图级别或节点级别的真实标签,形状任意。(默认值:None

  • pos (torch.Tensor, optional) – 节点位置矩阵,形状为 [num_nodes, num_dimensions]。 (默认: None)

  • time (torch.Tensor, optional) – 每个事件的时间戳,形状为 [num_edges][num_nodes]。(默认值:None

  • **kwargs (optional) – Additional attributes.

property num_nodes: Optional[int]

返回图中节点的数量。

注意

数据对象中的节点数量在存在节点级属性时会自动推断,例如data.x。 然而,在某些情况下,图可能仅在没有节点级属性的情况下给出。 然后根据 edge_index.max().item() + 1猜测节点数量。 然而,如果存在孤立节点,这个数字可能不正确,从而导致意外行为。 因此,我们建议通过 data.num_nodes = ... 显式设置数据对象中的节点数量。 您将收到一个警告,要求您这样做。

Return type:

Optional[int]

to_dict() Dict[str, Any][source]

返回存储的键/值对的字典。

Return type:

Dict[str, Any]

to_namedtuple() NamedTuple[source]

Returns a NamedTuple of stored key/value pairs.

Return type:

NamedTuple

update(data: Union[Self, Dict[str, Any]]) Self[source]

使用另一个数据对象中的元素更新数据对象。 添加的元素将覆盖现有的元素(在重复的情况下)。

Return type:

Self

__cat_dim__(key: str, value: Any, *args, **kwargs) Any[source]

Returns the dimension for which the value value of the attribute key will get concatenated when creating mini-batches using torch_geometric.loader.DataLoader.

注意

此方法仅供内部使用,仅在特定属性的小批量创建过程损坏时才应被重写。

Return type:

Any

__inc__(key: str, value: Any, *args, **kwargs) Any[source]

Returns the incremental count to cumulatively increase the value value of the attribute key when creating mini-batches using torch_geometric.loader.DataLoader.

注意

此方法仅供内部使用,仅在特定属性的小批量创建过程损坏时才应被重写。

Return type:

Any

validate(raise_on_error: bool = True) bool[source]

验证数据的正确性。

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

is_node_attr(key: str) bool[source]

如果键 key 处的对象表示节点级别的张量属性,则返回 True

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

is_edge_attr(key: str) bool[source]

如果键 key 处的对象表示一个边级别的张量属性,则返回 True

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

subgraph(subset: Tensor) Self[source]

返回由节点索引subset给出的诱导子图。

Parameters:

subset (LongTensorBoolTensor) – 要保留的节点。

Return type:

Self

edge_subgraph(subset: Tensor) Self[source]

返回由边索引subset给出的诱导子图。当前将保留图中的所有节点,即使它们在子图计算后是孤立的。

Parameters:

subset (LongTensorBoolTensor) – 要保留的边。

Return type:

Self

to_heterogeneous(node_type: Optional[Tensor] = None, edge_type: Optional[Tensor] = None, node_type_names: Optional[List[str]] = None, edge_type_names: Optional[List[Tuple[str, str, str]]] = None)[source]

Data对象转换为异质HeteroData对象。 为此,节点和边的属性根据节点级别和边级别的向量node_typeedge_type分别进行分割。 node_type_namesedge_type_names可以分别用于赋予有意义的节点和边类型名称。 也就是说,节点类型0node_type_names[0]给出。 如果Data对象是通过to_homogeneous()构建的,则可以在不需要传递任何额外参数的情况下重建该对象。

Parameters:
  • node_type (torch.Tensor, optional) – 一个节点级别的向量,表示每个节点的类型。(默认值:None

  • edge_type (torch.Tensor, optional) – 一个边级别的向量,表示每条边的类型。(默认值:None

  • node_type_names (List[str], optional) – 节点类型的名称。 (default: None)

  • edge_type_names (List[Tuple[str, str, str]], optional) – 边的类型名称。(默认值:None

classmethod from_dict(mapping: Dict[str, Any]) Self[source]

从字典创建一个Data对象。

Return type:

Self

property num_node_features: int

返回图中每个节点的特征数量。

Return type:

int

property num_features: int

返回图中每个节点的特征数量。 num_node_features 的别名。

Return type:

int

property num_edge_features: int

返回图中每条边的特征数量。

Return type:

int

property num_node_types: int

返回图中节点类型的数量。

Return type:

int

property num_edge_types: int

返回图中边的类型的数量。

Return type:

int

apply(func: Callable, *args: str)

Applies the function func, either to all attributes or only the ones given in *args.

apply_(func: Callable, *args: str)

Applies the in-place function func, either to all attributes or only the ones given in *args.

clone(*args: str)

Performs cloning of tensors, either for all attributes or only the ones given in *args.

coalesce() Self

Sorts and removes duplicated entries from edge indices edge_index.

Return type:

Self

concat(data: Self) Self

Concatenates self with another data object. All values needs to have matching shapes at non-concat dimensions.

Return type:

Self

contiguous(*args: str)

Ensures a contiguous memory layout, either for all attributes or only the ones given in *args.

coo(edge_types: Optional[List[Any]] = None, store: bool = False) Tuple[Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Optional[Tensor]]]

Returns the edge indices in the GraphStore in COO format.

Parameters:
  • edge_types (List[Any], optional) – The edge types of edge indices to obtain. If set to None, will return the edge indices of all existing edge types. (default: None)

  • store (bool, optional) – Whether to store converted edge indices in the GraphStore. (default: False)

Return type:

Tuple[Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Optional[Tensor]]]

cpu(*args: str)

Copies attributes to CPU memory, either for all attributes or only the ones given in *args.

csc(edge_types: Optional[List[Any]] = None, store: bool = False) Tuple[Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Optional[Tensor]]]

Returns the edge indices in the GraphStore in CSC format.

Parameters:
  • edge_types (List[Any], optional) – The edge types of edge indices to obtain. If set to None, will return the edge indices of all existing edge types. (default: None)

  • store (bool, optional) – Whether to store converted edge indices in the GraphStore. (default: False)

Return type:

Tuple[Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Optional[Tensor]]]

csr(edge_types: Optional[List[Any]] = None, store: bool = False) Tuple[Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Optional[Tensor]]]

Returns the edge indices in the GraphStore in CSR format.

Parameters:
  • edge_types (List[Any], optional) – The edge types of edge indices to obtain. If set to None, will return the edge indices of all existing edge types. (default: None)

  • store (bool, optional) – Whether to store converted edge indices in the GraphStore. (default: False)

Return type:

Tuple[Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Tensor], Dict[Tuple[str, str, str], Optional[Tensor]]]

cuda(device: Optional[Union[int, str]] = None, *args: str, non_blocking: bool = False)

Copies attributes to CUDA memory, either for all attributes or only the ones given in *args.

detach(*args: str)

Detaches attributes from the computation graph by creating a new tensor, either for all attributes or only the ones given in *args.

detach_(*args: str)

Detaches attributes from the computation graph, either for all attributes or only the ones given in *args.

edge_attrs() List[str]

返回所有边级别的张量属性名称。

Return type:

List[str]

generate_ids()

Generates and sets n_id and e_id attributes to assign each node and edge to a continuously ascending and unique ID.

get_edge_index(*args, **kwargs) Tuple[Tensor, Tensor]

Synchronously obtains an edge_index tuple from the GraphStore.

Parameters:
  • *args – Arguments passed to EdgeAttr.

  • **kwargs – Keyword arguments passed to EdgeAttr.

Raises:

KeyError – If the edge_index corresponding to the input EdgeAttr was not found.

Return type:

Tuple[Tensor, Tensor]

get_tensor(*args, convert_type: bool = False, **kwargs) Union[Tensor, ndarray]

Synchronously obtains a tensor from the FeatureStore.

Parameters:
  • *args – Arguments passed to TensorAttr.

  • convert_type (bool, optional) – Whether to convert the type of the output tensor to the type of the attribute index. (default: False)

  • **kwargs – Keyword arguments passed to TensorAttr.

Raises:

ValueError – If the input TensorAttr is not fully specified.

Return type:

Union[Tensor, ndarray]

get_tensor_size(*args, **kwargs) Optional[Tuple[int, ...]]

Obtains the size of a tensor given its TensorAttr, or None if the tensor does not exist.

Return type:

Optional[Tuple[int, ...]]

has_isolated_nodes() bool

Returns True if the graph contains isolated nodes.

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

has_self_loops() bool

Returns True if the graph contains self-loops.

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

is_coalesced() bool

Returns True if edge indices edge_index are sorted and do not contain duplicate entries.

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

property is_cuda: bool

Returns True if any torch.Tensor attribute is stored on the GPU, False otherwise.

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

is_directed() bool

Returns True if graph edges are directed.

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

is_sorted(sort_by_row: bool = True) bool

Returns True if edge indices edge_index are sorted.

Parameters:

sort_by_row (bool, optional) – If set to False, will require column-wise order/by destination node order of edge_index. (default: True)

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

is_sorted_by_time() bool

Returns True if time is sorted.

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

is_undirected() bool

Returns True if graph edges are undirected.

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

keys() List[str]

返回所有图形属性名称的列表。

Return type:

List[str]

multi_get_tensor(attrs: List[TensorAttr], convert_type: bool = False) List[Union[Tensor, ndarray]]

Synchronously obtains a list of tensors from the FeatureStore for each tensor associated with the attributes in attrs.

注意

The default implementation simply iterates over all calls to get_tensor(). Implementor classes that can provide additional, more performant functionality are recommended to to override this method.

Parameters:
  • attrs (List[TensorAttr]) – A list of input TensorAttr objects that identify the tensors to obtain.

  • convert_type (bool, optional) – Whether to convert the type of the output tensor to the type of the attribute index. (default: False)

Raises:

ValueError – If any input TensorAttr is not fully specified.

Return type:

List[Union[Tensor, ndarray]]

node_attrs() List[str]

返回所有节点级别的张量属性名称。

Return type:

List[str]

property num_edges: int

返回图中的边数。 对于无向图,这将返回双向边的数量,这是唯一边数量的两倍。

Return type:

int

pin_memory(*args: str)

Copies attributes to pinned memory, either for all attributes or only the ones given in *args.

put_edge_index(edge_index: Tuple[Tensor, Tensor], *args, **kwargs) bool

Synchronously adds an edge_index tuple to the GraphStore. Returns whether insertion was successful.

Parameters:
Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

put_tensor(tensor: Union[Tensor, ndarray], *args, **kwargs) bool

Synchronously adds a tensor to the FeatureStore. Returns whether insertion was successful.

Parameters:
  • tensor (torch.Tensor or np.ndarray) – The feature tensor to be added.

  • *args – Arguments passed to TensorAttr.

  • **kwargs – Keyword arguments passed to TensorAttr.

Raises:

ValueError – If the input TensorAttr is not fully specified.

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

record_stream(stream: Stream, *args: str)

Ensures that the tensor memory is not reused for another tensor until all current work queued on stream has been completed, either for all attributes or only the ones given in *args.

remove_edge_index(*args, **kwargs) bool

Synchronously deletes an edge_index tuple from the GraphStore. Returns whether deletion was successful.

Parameters:
  • *args – Arguments passed to EdgeAttr.

  • **kwargs – Keyword arguments passed to EdgeAttr.

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

remove_tensor(*args, **kwargs) bool

Removes a tensor from the FeatureStore. Returns whether deletion was successful.

Parameters:
Raises:

ValueError – If the input TensorAttr is not fully specified.

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

requires_grad_(*args: str, requires_grad: bool = True)

Tracks gradient computation, either for all attributes or only the ones given in *args.

share_memory_(*args: str)

Moves attributes to shared memory, either for all attributes or only the ones given in *args.

size(dim: Optional[int] = None) Optional[Union[Tuple[Optional[int], Optional[int]], int]]

返回由图形引起的邻接矩阵的大小。

Return type:

Union[Tuple[Optional[int], Optional[int]], int, None]

snapshot(start_time: Union[float, int], end_time: Union[float, int], attr: str = 'time') Self

Returns a snapshot of data to only hold events that occurred in period [start_time, end_time].

Return type:

Self

sort(sort_by_row: bool = True) Self

Sorts edge indices edge_index and their corresponding edge features.

Parameters:

sort_by_row (bool, optional) – If set to False, will sort edge_index in column-wise order/by destination node. (default: True)

Return type:

Self

sort_by_time() Self

Sorts data associated with time according to time.

Return type:

Self

to(device: Union[int, str], *args: str, non_blocking: bool = False)

Performs tensor device conversion, either for all attributes or only the ones given in *args.

up_to(end_time: Union[float, int]) Self

Returns a snapshot of data to only hold events that occurred up to end_time (inclusive of edge_time).

Return type:

Self

update_tensor(tensor: Union[Tensor, ndarray], *args, **kwargs) bool

Updates a tensor in the FeatureStore with a new value. Returns whether the update was succesful.

注意

实现类可以选择定义更高效的更新方法;默认情况下执行删除和插入操作。

Parameters:
  • tensor (torch.Tensor or np.ndarray) – The feature tensor to be updated.

  • *args – Arguments passed to TensorAttr.

  • **kwargs – Keyword arguments passed to TensorAttr.

Return type:

bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。

view(*args, **kwargs) AttrView

Returns a view of the FeatureStore given a not yet fully-specified TensorAttr.

Return type:

AttrView

property num_faces: Optional[int]

返回网格中的面数。

Return type:

Optional[int]

get_all_tensor_attrs() List[TensorAttr][source]

获取存储在Data中的所有特征属性。

Return type:

List[TensorAttr]

get_all_edge_attrs() List[EdgeAttr][source]

返回所有已注册的边缘属性。

Return type:

List[EdgeAttr]