torch_geometric.data.TemporalData
- class TemporalData(src: Optional[Tensor] = None, dst: Optional[Tensor] = None, t: Optional[Tensor] = None, msg: Optional[Tensor] = None, **kwargs)[source]
Bases:
BaseDataA data object composed by a stream of events describing a temporal graph. The
TemporalDataobject can hold a list of events (that can be understood as temporal edges in a graph) with structured messages. An event is composed by a source node, a destination node, a timestamp and a message. Any Continuous-Time Dynamic Graph (CTDG) can be represented with these four values.In general,
TemporalDatatries to mimic the behavior of a regular Python dictionary. In addition, it provides useful functionality for analyzing graph structures, and provides basic PyTorch tensor functionalities.from torch import Tensor from torch_geometric.data import TemporalData events = TemporalData( src=Tensor([1,2,3,4]), dst=Tensor([2,3,4,5]), t=Tensor([1000,1010,1100,2000]), msg=Tensor([1,1,0,0]) ) # Add additional arguments to `events`: events.y = Tensor([1,1,0,0]) # It is also possible to set additional arguments in the constructor events = TemporalData( ..., y=Tensor([1,1,0,0]) ) # Get the number of events: events.num_events >>> 4 # Analyzing the graph structure: events.num_nodes >>> 5 # PyTorch tensor functionality: events = events.pin_memory() events = events.to('cuda:0', non_blocking=True)
- Parameters:
src (torch.Tensor, optional) – A list of source nodes for the events with shape
[num_events]. (default:None)dst (torch.Tensor, optional) – A list of destination nodes for the events with shape
[num_events]. (default:None)t (torch.Tensor, optional) – The timestamps for each event with shape
[num_events]. (default:None)msg (torch.Tensor, optional) – Messages feature matrix with shape
[num_events, num_msg_features]. (default:None)**kwargs (optional) – Additional attributes.
Note
The shape of
src,dst,tand the first dimension of :obj`msg` should be the same (num_events).- classmethod from_dict(mapping: Dict[str, Any]) TemporalData[source]
Creates a
TemporalDataobject from a Python dictionary.- Return type:
- to_namedtuple() NamedTuple[source]
Returns a
NamedTupleof stored key/value pairs.- Return type:
- property num_events: int
Returns the number of events loaded.
Note
In a
TemporalData, each row denotes an event. Thus, they can be also understood as edges.- Return type:
- property num_edges: int
Alias for
num_events().- Return type:
- size(dim: Optional[int] = None) Optional[Union[Tuple[Optional[int], Optional[int]], int]][source]
Returns the size of the adjacency matrix induced by the graph.
- __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.Note
This method is for internal use only, and should only be overridden in case the mini-batch creation process is corrupted for a specific attribute.
- 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.Note
This method is for internal use only, and should only be overridden in case the mini-batch creation process is corrupted for a specific attribute.
- Return type:
- train_val_test_split(val_ratio: float = 0.15, test_ratio: float = 0.15)[source]
Splits the data in training, validation and test sets according to time.
- coalesce()[source]
Sorts and removes duplicated entries from edge indices
edge_index.
- 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.
- 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.
- cpu(*args: str)
Copies attributes to CPU memory, either for all attributes or only the ones given in
*args.
- 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.
- is_coalesced() bool
Returns
Trueif edge indicesedge_indexare sorted and do not contain duplicate entries.- Return type:
- property is_cuda: bool
Returns
Trueif anytorch.Tensorattribute is stored on the GPU,Falseotherwise.- Return type:
- 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:
- pin_memory(*args: str)
Copies attributes to pinned memory, either for all attributes or only the ones given in
*args.
- 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.
- 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.
- 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.- Parameters:
sort_by_row (bool, optional) – If set to
False, will sortedge_indexin column-wise order/by destination node. (default:True)- 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
datato only hold events that occurred up toend_time(inclusive ofedge_time).- Return type:
Self
- update(data: Self) Self
Updates the data object with the elements from another data object. Added elements will override existing ones (in case of duplicates).
- Return type:
Self