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试图模仿常规的 Python 字典的行为。 此外,它提供了用于分析图结构的有用功能,并提供了基本的 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。 然而,在某些情况下,图可能仅在没有节点级属性的情况下给出。 PyG 然后根据edge_index.max().item() + 1来猜测节点数量。 然而,如果存在孤立节点,这个数字可能不正确,从而导致意外行为。 因此,我们建议通过data.num_nodes = ...显式设置数据对象中的节点数量。 您将收到一个警告,要求您这样做。
- to_namedtuple() NamedTuple[source]
Returns a
NamedTupleof stored key/value pairs.- Return type:
- 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
valueof the attributekeywill get concatenated when creating mini-batches usingtorch_geometric.loader.DataLoader.注意
此方法仅供内部使用,仅在特定属性的小批量创建过程损坏时才应被重写。
- Return type:
- __inc__(key: str, value: Any, *args, **kwargs) Any[source]
Returns the incremental count to cumulatively increase the value
valueof the attributekeywhen creating mini-batches usingtorch_geometric.loader.DataLoader.注意
此方法仅供内部使用,仅在特定属性的小批量创建过程损坏时才应被重写。
- Return type:
- subgraph(subset: Tensor) Self[source]
返回由节点索引
subset给出的诱导子图。- Parameters:
subset (LongTensor 或 BoolTensor) – 要保留的节点。
- Return type:
Self
- edge_subgraph(subset: Tensor) Self[source]
返回由边索引
subset给出的诱导子图。当前将保留图中的所有节点,即使它们在子图计算后是孤立的。- Parameters:
subset (LongTensor 或 BoolTensor) – 要保留的边。
- 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_type和edge_type分别进行分割。node_type_names和edge_type_names可以分别用于赋予有意义的节点和边类型名称。 也就是说,节点类型0由node_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)
- property num_features: int
返回图中每个节点的特征数量。
num_node_features的别名。- Return type:
- 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
selfwith anotherdataobject. 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
GraphStorein 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
GraphStorein 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
GraphStorein 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.
- generate_ids()
Generates and sets
n_idande_idattributes 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_indextuple from theGraphStore.
- get_tensor(*args, convert_type: bool = False, **kwargs) Union[Tensor, ndarray]
Synchronously obtains a
tensorfrom theFeatureStore.- 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
TensorAttris not fully specified.- Return type:
- get_tensor_size(*args, **kwargs) Optional[Tuple[int, ...]]
Obtains the size of a tensor given its
TensorAttr, orNoneif the tensor does not exist.
- is_coalesced() bool
Returns
Trueif edge indicesedge_indexare sorted and do not contain duplicate entries.
- property is_cuda: bool
Returns
Trueif anytorch.Tensorattribute is stored on the GPU,Falseotherwise.
- is_sorted(sort_by_row: bool = True) bool
Returns
Trueif edge indicesedge_indexare sorted.- Parameters:
sort_by_row (bool, optional) – If set to
False, will require column-wise order/by destination node order ofedge_index. (default:True)- Return type:
bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。
- multi_get_tensor(attrs: List[TensorAttr], convert_type: bool = False) List[Union[Tensor, ndarray]]
Synchronously obtains a list of tensors from the
FeatureStorefor each tensor associated with the attributes inattrs.注意
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
TensorAttrobjects 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
TensorAttris not fully specified.- Return type:
- 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_indextuple to theGraphStore. Returns whether insertion was successful.- Parameters:
edge_index (Tuple[torch.Tensor, torch.Tensor]) – The
edge_indextuple in a format specified inEdgeAttr.*args – Arguments passed to
EdgeAttr.**kwargs – Keyword arguments passed to
EdgeAttr.
- Return type:
bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。
- put_tensor(tensor: Union[Tensor, ndarray], *args, **kwargs) bool
Synchronously adds a
tensorto theFeatureStore. 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
TensorAttris 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
streamhas been completed, either for all attributes or only the ones given in*args.
- remove_edge_index(*args, **kwargs) bool
Synchronously deletes an
edge_indextuple from theGraphStore. Returns whether deletion was successful.- Parameters:
- Return type:
bool 翻译后的内容: bool 在这个例子中,`bool` 是一个Python函数名称,根据翻译规则1,不需要翻译。因此,翻译后的内容保持不变。
- remove_tensor(*args, **kwargs) bool
Removes a tensor from the
FeatureStore. Returns whether deletion was successful.- Parameters:
*args – Arguments passed to
TensorAttr.**kwargs – Keyword arguments passed to
TensorAttr.
- Raises:
ValueError – If the input
TensorAttris 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.
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]]
返回由图形引起的邻接矩阵的大小。
- snapshot(start_time: Union[float, int], end_time: Union[float, int], attr: str = 'time') Self
Returns a snapshot of
datato 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_indexand their corresponding edge features.
- 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
datato only hold events that occurred up toend_time(inclusive ofedge_time).- Return type:
Self
- update_tensor(tensor: Union[Tensor, ndarray], *args, **kwargs) bool
Updates a
tensorin theFeatureStorewith 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
FeatureStoregiven a not yet fully-specifiedTensorAttr.- Return type:
AttrView
- get_all_tensor_attrs() List[TensorAttr][source]
获取存储在Data中的所有特征属性。
- Return type: