Neural Layers
Encoders
ctan
Classes:
-
CTAN–An implementation of CTAN.
-
CTANMemory–The CTAN Memory model.
CTAN
CTAN(
edge_dim: int,
memory_dim: int,
time_dim: int,
node_dim: int,
num_iters: int = 1,
mean_delta_t: float = 0.0,
std_delta_t: float = 1.0,
epsilon: float = 0.1,
gamma: float = 0.1,
)
Bases: Module
An implementation of CTAN.
Parameters:
-
edge_dim(int) –Dimension of edge features.
-
memory_dim(int) –Dimension of memory embeddings.
-
time_dim(int) –Dimension of time encodings.
-
node_dim(int) –Dimension of static/dynamic node features.
-
num_iters(int, default:1) –Number of AntiSymmetricConv layers.
-
mean_delta_t(float, default:0.0) –Mean delta time between edge events (used to normalize time signal).
-
std_delta_t(float, default:1.0) –Std delta time between edge events (used to normalize time signal).
-
epsilon(float, default:0.1) –Discretization step size for AntiSymmetricConv.
-
gamma(float, default:0.1) –The strength of the diffusion in the AntiSymmetricConv.
Reference: https://arxiv.org/abs/2406.02740
Methods:
-
forward–Forward pass.
Source code in tgm/nn/encoder/ctan.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
forward
forward(
x: Tensor,
last_update: Tensor,
edge_index: Tensor,
t: Tensor,
msg: Tensor,
) -> Tensor
Forward pass.
Parameters:
-
x(PyTorch Float Tensor) –Node features.
-
last_update(PyTorch Tensor) –Last memory update timestamps.
-
edge_index(PyTorch Tensor) –Graph edge indices.
-
t(PyTorch Tensor) –Graph edge timestamps.
-
msg(PyTorch Tensor) –Memory embeddings.
Returns:
-
PyTorch Float Tensor–Embeddings for the batch of node ids.
Source code in tgm/nn/encoder/ctan.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
CTANMemory
Bases: Module
The CTAN Memory model.
Parameters:
-
num_nodes(int) –The number of nodes to save memories for.
-
memory_dim(int) –The hidden memory dimensionality.
-
aggr_module(Callable) –The message aggregator function which aggregates messages to the same destination into a single representation.
-
init_time(int, default:0) –Start time of the graph, used during memory reset.
Reference: https://arxiv.org/abs/2406.02740
Source code in tgm/nn/encoder/ctan.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
dygformer
Adapted from https://github.com/yule-BUAA/DyGLib_TGB.
Classes:
-
DyGFormer–An implementation of DyGFormer.
-
NeighborCooccurrenceEncoder–An implementation of Neighbor Co-occurrence Encoding Scheme.
-
TransformerEncoder–An implementation of Transformer Encoder.
DyGFormer
DyGFormer(
node_feat_dim: int,
edge_x_dim: int,
time_feat_dim: int,
channel_embedding_dim: int,
output_dim: int = 172,
patch_size: int = 1,
num_layers: int = 2,
num_heads: int = 2,
dropout: float = 0.1,
max_input_sequence_length: int = 512,
num_channels: int = 4,
time_encoder: Callable[..., Module] = Time2Vec,
device: str = 'cpu',
)
Bases: Module
An implementation of DyGFormer.
Parameters:
-
node_feat_dim(int) –Dimension of static/dynamic node features (
d_N). -
edge_x_dim(int) –Dimension of edge features (
d_E). -
time_feat_dim(int) –Dimension of time encodings (
d_T). -
channel_embedding_dim(int) –Dimension of each channel embedding.
-
output_dim(int, default:172) –Dimension of output embedding.
-
patch_size(int, default:1) –Path size (
\mathbf{P}). -
num_layers(int, default:2) –Number of transformer layers.
-
num_heads(int, default:2) –Number of attention heads.
-
dropout(float, default:0.1) –Drop out rate.
-
max_input_sequence_length(int, default:512) –maximal length of the input sequence for each node.
-
time_encoder(PyTorch Module), default:Time2Vec) –Time encoder module.
-
device(str), default:'cpu') –cpu or cuda
Reference: https://arxiv.org/abs/2303.13047.
Source code in tgm/nn/encoder/dygformer.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
NeighborCooccurrenceEncoder
Bases: Module
An implementation of Neighbor Co-occurrence Encoding Scheme.
Parameters:
-
feat_dim(int) –dimension of neighbor co-occurrence features (encodings).
-
device(str) –Device (cpu or gpu)
Reference: https://arxiv.org/abs/2303.13047.
Methods:
-
forward–Forward pass. Encode neighbor co-occurrence (Section 4.1).
Source code in tgm/nn/encoder/dygformer.py
23 24 25 26 27 28 29 30 31 32 | |
forward
forward(
src_neighbour_nodes_ids: Tensor,
dst_neighbour_nodes_ids: Tensor,
) -> Tuple[Tensor, Tensor]
Forward pass. Encode neighbor co-occurrence (Section 4.1).
Parameters:
-
src_neighbour_nodes_ids(Tensor) –Padded list of source node's neighbour.
-
dst_neighbour_nodes_ids(Tensor) –Padded list of destination node's neighbour.
Returns:
-
X(PyTorch Float Tensor) –Neighbor co-occurrence features (
X^{t}_{*,C}).
Source code in tgm/nn/encoder/dygformer.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
TransformerEncoder
Bases: Module
An implementation of Transformer Encoder.
Parameters:
-
attention_dim(int) –dimension of the attention vector.
-
num_heads(int) –number of attention heads.
-
dropout(float, default:0.1) –dropout rate.
Reference: https://arxiv.org/abs/2303.13047.
Methods:
-
forward–Forward pass. Encode the inputs by Transformer encoder (Section 4.1).
Source code in tgm/nn/encoder/dygformer.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
forward
forward(inputs: Tensor) -> Tensor
Forward pass. Encode the inputs by Transformer encoder (Section 4.1).
Parameters:
-
inputs(PyTorch Float Tensor) –Z^{t} = [Z^{t}_u, Z^{t}_v].
Returns:
-
H(PyTorch Float Tensor) –Representations of all nodes.
Source code in tgm/nn/encoder/dygformer.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
tpnet
Classes:
-
RandomProjectionModule–This model maintains a series of temporal walk matrices $A_^(0)(t),A_^(1)(t),...,A^(k)(t)$ through
-
TPNet–An implementation of TPNet.
RandomProjectionModule
RandomProjectionModule(
num_nodes: int,
num_layer: int,
time_decay_weight: float,
beginning_time: float,
use_matrix: bool = True,
scale_random_projection: bool = True,
enforce_dim: int | None = None,
num_edges: int | None = None,
dim_factor: int | None = None,
concat_src_dst: bool = True,
device: str = 'cpu',
)
Bases: Module
This model maintains a series of temporal walk matrices $A_^(0)(t),A_^(1)(t),...,A^(k)(t)$ through random feature propagation, and extract the pairwise features from the obtained random projections.
Parameters:
-
num_nodes(int) –the number of nodes
-
num_layer(int) –the max hop of the maintained temporal walk matrices
-
time_decay_weight(float) –the time decay weight (lambda of the original paper)
-
beginning_time(float) –the earliest time in the given temporal graph
-
use_matrix(bool, default:True) –if True, explicitly maintain the temporal walk matrices
-
scale_random_projection(bool, default:True) –if True, the inner product of nodes' random projections will be scaled
-
enforce_dim(int, default:None) –if not None, explicitly set the dimension of random projections to enforce_dim
-
num_edges(int, default:None) –the number of edges
-
dim_factor(int, default:None) –the parameter to control the dimension of random projections. Specifically, the dimension of the random projections is set to be dim_factor * log(2*edge_num)
-
concat_src_dst(bool, default:True) –If true,
random_featurewill be computed from the concatenation betweensrcanddstby model design, this is True by default. To make the model scalable, this can be set to False; however, there will be performance trade-off. -
device(str, default:'cpu') –torch device
Note: For large-scale dataset, the authors suggested to set use_matrix=False and use number of edge and dim_factor=10 to make it scalable.
Source code in tgm/nn/encoder/tpnet.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
TPNet
TPNet(
node_feat_dim: int,
edge_x_dim: int,
time_feat_dim: int,
output_dim: int,
num_neighbors: int,
num_layers: int = 2,
dropout: float = 0.1,
random_projections: RandomProjectionModule
| None = None,
device: str = 'cpu',
time_encoder: Callable[..., Module] = Time2Vec,
)
Bases: Module
An implementation of TPNet.
Parameters:
-
node_feat_dim(int) –Dimension of static/dynamic node features (
d_N). -
edge_x_dim(int) –Dimension of edge features (
d_E). -
time_feat_dim(int) –Dimension of time encodings (
d_T). -
output_dim(int) –Dimension of output embedding.
-
num_neighbors(int) –Number of recent temporal neighbors consider
-
num_layers(int, default:2) –Number of transformer layers.
-
dropout(float, default:0.1) –Drop out rate.
-
random_projections(Module, default:None) –Random projection module that maintains a series temporal walk matrices
-
device(str), default:'cpu') –cpu or cuda
-
time_encoder(PyTorch Module), default:Time2Vec) –Time encoder module.
Reference: https://arxiv.org/abs/2410.04013.
Source code in tgm/nn/encoder/tpnet.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
gclstm
Adapted from https://github.com/benedekrozemberczki/pytorch_geometric_temporal/blob/master/torch_geometric_temporal/nn/recurrent/gc_lstm.py.
Classes:
-
GCLSTM–An implementation of Integrated Graph Convolutional Long Short Term Memory Cell.
GCLSTM
GCLSTM(
in_channels: int,
out_channels: int,
K: int,
normalization: str = 'sym',
bias: bool = True,
)
Bases: Module
An implementation of Integrated Graph Convolutional Long Short Term Memory Cell.
Parameters:
-
in_channels(int) –Number of input features.
-
out_channels(int) –Number of output features.
-
K(int) –Chebyshev filter size :math:
K. -
normalization(str, default:'sym') –The normalization scheme for the graph Laplacian (default: :obj:
"sym"):-
:obj:
None: No normalization :math:\mathbf{L} = \mathbf{D} - \mathbf{A} -
:obj:
"sym": Symmetric normalization :math:\mathbf{L} = \mathbf{I} - \mathbf{D}^{-1/2} \mathbf{A} \mathbf{D}^{-1/2} -
:obj:
"rw": Random-walk normalization :math:\mathbf{L} = \mathbf{I} - \mathbf{D}^{-1} \mathbf{A}
You need to pass :obj:
lambda_maxto the :meth:forwardmethod of this operator in case the normalization is non-symmetric. :obj:\lambda_maxshould be a :class:torch.Tensorof size :obj:[num_graphs]in a mini-batch scenario and a scalar/zero-dimensional tensor when operating on single graphs. You can pre-compute :obj:lambda_maxvia the :class:torch_geometric.transforms.LaplacianLambdaMaxtransform. -
-
bias(bool, default:True) –If set to :obj:
False, the layer will not learn an additive bias. (default: :obj:True)
Reference: https://arxiv.org/abs/1812.04206
Source code in tgm/nn/encoder/gclstm.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
tgcn
Adapted from https://github.com/benedekrozemberczki/pytorch_geometric_temporal/blob/master/torch_geometric_temporal/nn/recurrent/temporalgcn.py.
Classes:
-
TGCN–An implementation of Temporal Graph Convolutional Gated Recurrent Cell.
TGCN
TGCN(
in_channels: int,
out_channels: int,
improved: bool = False,
cached: bool = False,
add_self_loops: bool = True,
)
Bases: Module
An implementation of Temporal Graph Convolutional Gated Recurrent Cell.
Parameters:
-
in_channels(int) –Number of input features.
-
out_channels(int) –Number of output features.
-
improved(bool, default:False) –Stronger self loops. Default is False. If
improved = True, the self-loops are addedA+2Iinstead ofA+Igiving each node’s own features more influence during aggregation -
cached(bool, default:False) –Caching the message weights. Default is False. The layer computes the normalized adjacency matrix only once. Speed up training but limit to transductive learning scenario (graph structure is assumed to be static)
-
add_self_loops(bool, default:True) –Adding self-loops for smoothing. Default is True.
Reference: https://arxiv.org/abs/1811.05320
Source code in tgm/nn/encoder/tgcn.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
roland
Classes:
-
ROLAND–An implementation of ROLAND.
ROLAND
ROLAND(
input_channel: int,
out_channel: int,
num_nodes: int,
dropout: float = 0.0,
update: str | None = 'learnable',
tau: float = 0.5,
)
Bases: Module
An implementation of ROLAND. https://arxiv.org/abs/2208.07239 .
Parameters:
-
input_channel(int) –Dimension of input.
-
out_channel(int) –Dimension of output.
-
num_nodes(int) –Maximum number of nodes.
-
dropout(float, default:0.0) –dropout rate
-
update(str, default:'learnable') –update mechanism. Choose from ['moving','learnable','gru','mlp',None] If
updateis set to None, the embedding will be update withtau
Reference: https://github.com/manuel-dileo/dynamic-gnn .
Source code in tgm/nn/encoder/roland.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
Decoders
graphproppred
Classes:
-
GraphPredictor–Perform pooling over provided node features and perform graph level task.
Functions:
-
mean_pooling–Default graph pooling: Mean pooling.
-
sum_pooling–Default graph pooling: Sunm pooling.
GraphPredictor
GraphPredictor(
in_dim: int,
out_dim: int = 1,
nlayers: int = 2,
hidden_dim: int = 64,
graph_pooling: str = 'mean',
)
Bases: Module
Perform pooling over provided node features and perform graph level task.
Parameters:
-
in_dim(int) –Dimension of input
-
out_dim(int, default:1) –Dimension of output
-
nlayers(int, default:2) –Number of layers
-
hidden_dim(int, default:64) –Size of each hidden embeddings
-
graph_pooling(str, default:'mean') –graph pooling operation (mean, sum.)
Methods:
-
forward–Forward pass.
Source code in tgm/nn/decoder/graphproppred.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
forward
forward(z_nodes: Tensor) -> Tensor
Forward pass.
Parameters:
-
z_nodes(Tensor) –embedding of nodes
Source code in tgm/nn/decoder/graphproppred.py
57 58 59 60 61 62 63 64 | |
mean_pooling
mean_pooling(z: Tensor) -> Tensor
Default graph pooling: Mean pooling.
Source code in tgm/nn/decoder/graphproppred.py
6 7 8 9 | |
sum_pooling
sum_pooling(z: Tensor) -> Tensor
Default graph pooling: Sunm pooling.
Source code in tgm/nn/decoder/graphproppred.py
12 13 14 15 | |
linkproppred
Classes:
-
LearnableSumMerge–Sum node-level embeddings after a linear projection.
-
LinkPredictor–Compute edge embedding given src and dst node embeddings.
Functions:
-
cat_merge–Default merging operation: Concat.
LearnableSumMerge
LearnableSumMerge(node_dim: int)
Bases: Module
Sum node-level embeddings after a linear projection.
Source code in tgm/nn/decoder/linkproppred.py
17 18 19 20 | |
LinkPredictor
LinkPredictor(
node_dim: int,
out_dim: int = 1,
nlayers: int = 2,
hidden_dim: int = 64,
merge_op: str = 'concat',
)
Bases: Module
Compute edge embedding given src and dst node embeddings.
Parameters:
-
node_dim(int) –Dimension of node embedding
-
out_dim(int, default:1) –Dimension of output
-
nlayers(int, default:2) –Number of layers
-
hidden_dim(int, default:64) –Size of each hidden embedding
-
merge_op(str, default:'concat') –Operation to merge 2 node embeddings (concat)
Methods:
-
forward–Forward pass.
Source code in tgm/nn/decoder/linkproppred.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
forward
forward(z_src: Tensor, z_dst: Tensor) -> Tensor
Forward pass.
Parameters:
-
z_src(Tensor) –embedding of src node
-
z_dst(Tensor) –embedding of dst node
Source code in tgm/nn/decoder/linkproppred.py
75 76 77 78 79 80 81 82 83 | |
cat_merge
cat_merge(z_src: Tensor, z_dst: Tensor) -> Tensor
Default merging operation: Concat.
Source code in tgm/nn/decoder/linkproppred.py
8 9 10 11 | |
nodeproppred
Classes:
-
NodePredictor–Encoder for node property prediction.
NodePredictor
Bases: Module
Encoder for node property prediction.
Parameters:
-
in_dim(int) –Dimension of input
-
out_dim(int, default:1) –Dimension of output
-
hidden_dim(int, default:64) –Size of hidden embedding
Methods:
-
forward–Forward pass.
Source code in tgm/nn/decoder/nodeproppred.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
forward
forward(z_node: Tensor) -> Tensor
Forward pass.
Parameters:
-
z_node(Tensor) –embedding of a node
Source code in tgm/nn/decoder/nodeproppred.py
34 35 36 37 38 39 40 | |
ncnpred
Classes:
-
NCNPredictor–An implementation of Temporal Neural Common Neighbor (TNCN).
NCNPredictor
NCNPredictor(
in_channels: int,
hidden_dim: int,
out_channels: int,
k: int = 2,
cn_time_decay: bool = False,
)
Bases: Module
An implementation of Temporal Neural Common Neighbor (TNCN).
Parameters:
-
in_channels(int) –Number of input features.
-
out_channels(int) –Number of output features.
-
hidden_dim(int) –Size of each hidden embedding.
-
k(int, default:2) –define k-th hop common neighbour (CN) embedding extraction (select from 2/4/8)
-
cn_time_decay(bool, default:False) –indicate whether applying decay on time
Reference: https://arxiv.org/abs/2406.07926.
Methods:
-
forward–Forward pass.
-
get_cn_emb–Obtain the CNs embeddings for each node pair in a batch (Torch version).
Source code in tgm/nn/decoder/ncnpred.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
forward
forward(
x: Tensor,
edge_index: Tensor,
tar_ei: Tensor,
time_info: Optional[Tuple[Tensor, Tensor]] = None,
) -> Tensor
Forward pass.
Parameters:
-
x(Tensor) –node features,
-
edge_index(Tensor) –edges list of subgraph,
-
tar_ei(Tensor) –edges list for prediction ,
-
time_info(Optional[Tuple[Tensor, Tensor]], default:None) –A tuple of last update and current time of each edge
Source code in tgm/nn/decoder/ncnpred.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |
get_cn_emb
get_cn_emb(
x: Tensor,
edge_index: Tensor,
tar_ei: Tensor,
time_info: Optional[Tuple[Tensor, Tensor]] = None,
) -> Tensor
Obtain the CNs embeddings for each node pair in a batch (Torch version).
Parameters:
-
x(Tensor) –node features,
-
edge_index(Tensor) –edges list of subgraph,
-
tar_ei(Tensor) –edges list for prediction,
-
time_info(Optional[Tuple[Tensor, Tensor]], default:None) –A tuple of last update and current time of each edge
Source code in tgm/nn/decoder/ncnpred.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
Modules
edgebank
Classes:
EdgeBankPredictor
EdgeBankPredictor(
src: Tensor,
dst: Tensor,
ts: Tensor,
memory_mode: Literal[
'unlimited', 'fixed'
] = 'unlimited',
window_ratio: float = 0.15,
pos_prob: float = 1.0,
)
Reference: https://arxiv.org/abs/2207.10128.
This predictor implements the EdgeBank baseline for dynamic link prediction,
introduced in https://arxiv.org/abs/2207.10128. It stores a memory of past
edges and predicts the probability of a link reoccurring based on whether
the queried edge is present in memory. The memory can be either unlimited
(retains all edges) or fixed (retains only edges within a sliding window).
Parameters:
-
src(Tensor) –Source node IDs of edges used for initialization.
-
dst(Tensor) –Destination node IDs of edges used for initialization.
-
ts(Tensor) –Timestamps of edges used for initialization.
-
memory_mode(Literal['unlimited', 'fixed'], default:'unlimited') –'unlimited': Keeps all observed edges in memory.'fixed': Keeps only edges within a sliding window of time. Defaults to'unlimited'.
-
window_ratio(float, default:0.15) –Ratio of the sliding window length to the total observed time span (only used if
memory_mode='fixed'). Must be in(0, 1]. Defaults to0.15. -
pos_prob(float, default:1.0) –The probability assigned to edges present in memory. Defaults to
1.0.
Raises:
-
ValueError–If
memory_modeis not one of'unlimited'or'fixed'. -
ValueError–If
window_ratiois not in the range(0, 1]. -
TypeError–If
src,dst, ortsare not alltorch.Tensor. -
ValueError–If
src,dst, andtsdo not have the same length, or if they are empty.
Note
- In
unlimitedmode, memory grows with the number of observed edges. - In
fixedmode, only edges within the most recent time window are retained. The window size is proportional towindow_ratio.
Methods:
-
update–Update EdgeBank memory with a batch of edges.
Attributes:
-
window_end(int | float) –Return the end timestamp of the current memory window.
-
window_ratio(float) –Return the ratio of the memory window size to the full time span.
-
window_start(int | float) –Return the start timestamp of the current memory window.
Source code in tgm/nn/modules/edgebank.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
window_ratio
property
window_ratio: float
Return the ratio of the memory window size to the full time span.
window_start
property
Return the start timestamp of the current memory window.
update
update(src: Tensor, dst: Tensor, ts: Tensor) -> None
Update EdgeBank memory with a batch of edges.
Parameters:
-
src(Tensor) –Source node IDs of the edges.
-
dst(Tensor) –Destination node IDs of the edges.
-
ts(Tensor) –Timestamps of the edges.
Raises:
-
TypeError–If inputs are not
torch.Tensor. -
ValueError–If input tensors do not have the same length, or are empty.
Source code in tgm/nn/modules/edgebank.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
time_encoding
Classes:
-
Time2Vec–
Time2Vec
Time2Vec(time_dim: int)
Bases: Module
Parameters:
-
time_dim(int) –The dimension of time encodings.
Source code in tgm/nn/modules/time_encoding.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
attention
Classes:
-
TemporalAttention–Multi-head Temporal Attention Module for dynamic/temporal graphs.
TemporalAttention
TemporalAttention(
n_heads: int,
node_dim: int,
edge_dim: int,
time_dim: int,
dropout: float = 0.1,
)
Bases: Module
Multi-head Temporal Attention Module for dynamic/temporal graphs.
This module computes attention over a node's neighbors considering node features, edge features, and time features. It supports multiple attention heads and applies residual connection, dropout, and layer normalization to the output.
Parameters:
-
n_heads(int) –Number of attention heads.
-
node_dim(int) –Dimensionality of node features.
-
edge_dim(int) –Dimensionality of edge features.
-
time_dim(int) –Dimensionality of temporal features.
-
dropout(float, default:0.1) –Dropout probability applied to attention and output layers. Default is 0.1.
Raises:
-
ValueError–If
n_heads,node_dim,edge_dim, ortime_dimare <= 0.
Note
The output dimension is node_dim + time_dim, padded to be divisible by
n_heads if necessary.
Methods:
-
forward–Forward pass of the Temporal Attention module.
Source code in tgm/nn/modules/attention.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
forward
forward(
node_feat: Tensor,
time_feat: Tensor,
edge_feat: Tensor,
nbr_node_feat: Tensor,
nbr_time_feat: Tensor,
valid_nbr_mask: Tensor,
) -> Tensor
Forward pass of the Temporal Attention module.
Computes multi-head attention over neighbors, using node, edge, and time features, followed by a residual connection, dropout, and layer normalization.
Parameters:
-
node_feat(Tensor) –Node features of shape (B, node_dim).
-
time_feat(Tensor) –Node time features of shape (B, time_dim).
-
edge_feat(Tensor) –Edge features for each neighbor of shape (B, num_nbrs, edge_dim).
-
nbr_node_feat(Tensor) –Neighbor node features of shape (B, num_nbrs, node_dim).
-
nbr_time_feat(Tensor) –Neighbor time features of shape (B, num_nbrs, time_dim).
-
valid_nbr_mask(Tensor) –Boolean mask indicating valid neighbors of shape (B, num_nbrs). True indicates valid neighbors.
Returns:
-
Tensor–torch.Tensor: Updated node features of shape (B, out_dim).
Notes
- If a node has no neighbors, masked attention values are set to -1e10 to avoid NaNs in softmax.
- The output dimension is padded if necessary to be divisible by the number of heads.
Source code in tgm/nn/modules/attention.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
poptrack
Classes:
PopTrackPredictor
PopTrackPredictor(
src: Tensor,
dst: Tensor,
ts: Tensor,
num_nodes: int,
k: int = 50,
decay: float = 0.9,
)
Reference: https://openreview.net/pdf?id=9kLDrE5rsW
This predictor implements the PopTrack baseline for dynamic link prediction,
introduced in https://openreview.net/pdf?id=9kLDrE5rsW.
It predicts the probability of a link reoccurring based on
the popularity score of the queried edge's destination.
Parameters:
-
src(Tensor) –Source node IDs of edges used for initialization.
-
dst(Tensor) –Destination node IDs of edges used for initialization.
-
ts(Tensor) –Timestamps of edges used for initialization.
-
num_nodes(int) –The total number of nodes.
-
k(int, default:50) –Number of popular nodes to retrieve from.
-
decay(float, default:0.9) –temporal decay parameter. Must be in
(0, 1]. Defaults to0.9.
Raises:
-
ValueError–If
kis nonpositive. -
TypeError–If
src,dst, ortsare not alltorch.Tensor. -
ValueError–If
src,dst, andtsdo not have the same length, or if they are empty.
Note
- The predictions are not conditional on the source.
Methods:
-
update–Update PopTrack cache with a batch of edges.
Source code in tgm/nn/modules/poptrack.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
update
update(src: Tensor, dst: Tensor, ts: Tensor) -> None
Update PopTrack cache with a batch of edges.
Parameters:
-
src(Tensor) –Source node IDs of the edges.
-
dst(Tensor) –Destination node IDs of the edges.
-
ts(Tensor) –Timestamps of the edges.
Raises:
-
TypeError–If inputs are not
torch.Tensor. -
ValueError–If input tensors do not have the same length, or are empty.
Source code in tgm/nn/modules/poptrack.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
t_comem
Classes:
tCoMemPredictor
tCoMemPredictor(
src: Tensor,
dst: Tensor,
ts: Tensor,
num_nodes: int,
k: int = 50,
window_ratio: float = 0.15,
co_occurrence_weight: float = 0.8,
)
Reference: https://www.arxiv.org/abs/2506.12764
This predictor implements the t-CoMem module for dynamic link prediction,
introduced in https://www.arxiv.org/abs/2506.12764.
It is a memory-based module that mixes popularity with co-occurence.
Parameters:
-
src(Tensor) –Source node IDs of edges used for initialization.
-
dst(Tensor) –Destination node IDs of edges used for initialization.
-
ts(Tensor) –Timestamps of edges used for initialization.
-
num_nodes(int) –Total number of nodes in the dataset.
-
k(int, default:50) –threshold for popularity effect. Defaults to 50, must be positive and smaller than
num_nodes. In general, largerkleads to better performance but higher memory usage, though this usually stops being true after a certain point. -
window_ratio(float, default:0.15) –Ratio of the sliding window length to the total observed time span (only used if
memory_mode='fixed'). Must be in(0, 1]. Defaults to0.15. -
co_occurrence_weight(float, default:0.8) –Weighting parameter for co-occurrence. Must be in
(0, 1]. Defaults to0.8.
Raises:
-
TypeError–If
src,dst, ortsare not alltorch.Tensor. -
ValueError–If
src,dst, andtsdo not have the same length, or if they are empty.
Methods:
-
update–Update EdgeBank memory with a batch of edges.
Attributes:
-
window_end(int | float) –Return the end timestamp of the current memory window.
-
window_ratio(float) –Return the ratio of the memory window size to the full time span.
-
window_size(int) –Return the absolute size of the memory window.
-
window_start(int | float) –Return the start timestamp of the current memory window.
Source code in tgm/nn/modules/t_comem.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
window_ratio
property
window_ratio: float
Return the ratio of the memory window size to the full time span.
window_start
property
Return the start timestamp of the current memory window.
update
update(src: Tensor, dst: Tensor, ts: Tensor) -> None
Update EdgeBank memory with a batch of edges.
Parameters:
-
src(Tensor) –Source node IDs of the edges.
-
dst(Tensor) –Destination node IDs of the edges.
-
ts(Tensor) –Timestamps of the edges.
Raises:
-
TypeError–If inputs are not
torch.Tensor. -
ValueError–If input tensors do not have the same length, or are empty.
Source code in tgm/nn/modules/t_comem.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |