TGNMemory
¶
TGNMemory(
num_nodes: int,
raw_msg_dim: int,
memory_dim: int,
time_dim: int,
message_module: Callable,
aggregator_module: Callable,
)
Bases: Module
The Temporal Graph Network (TGN) memory model from the "Temporal Graph Networks for Deep Learning on Dynamic Graphs" paper, as PyG's
torch_geometric.nn.TGNMemory.
Every node has a memory vector, updated with a GRU cell from the messages
of the events it takes part in. The events of a mini-batch are stored as
raw messages by update_state() and only turned into memory updates
when the nodes are next accessed, so that the memory read for a
mini-batch never contains information from that mini-batch itself.
In training mode, memory(n_id) computes the memory of n_id updated
with the pending messages (differentiable w.r.t. the message modules, the
time encoder and the GRU), and update_state() first writes those
updates to the state and then stores the new messages. In evaluation
mode, memory(n_id) returns the stored memory and update_state()
applies the new messages right away. Switching from training to
evaluation mode (.eval() / .train(False), on this module or on a
model containing it) flushes all pending messages into the memory, as in
PyG.
The state is not part of parameters() (see the
design notes of mlx_graphs.nn.models.tgn): read it with
memory and last_update. In particular,
save_weights() / load_weights only save and
restore the learnable parameters, not the memory (PyG's state_dict
also holds the memory and last_update buffers). To checkpoint
them, use memory_state() and load_memory_state(), e.g.
mx.savez("memory.npz", **memory.memory_state()). As with PyG's
buffers, the pending messages are not part of it.
Timestamps are integers (int64, as PyG's TemporalData.t).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int
|
Number of nodes to keep a memory for |
required |
raw_msg_dim
|
int
|
Size of the raw messages (event features) |
required |
memory_dim
|
int
|
Size of the memory |
required |
time_dim
|
int
|
Size of the time encoding |
required |
message_module
|
Callable
|
Message function, called as |
required |
aggregator_module
|
Callable
|
Aggregation of the messages of each node, called as
|
required |
Example:
import mlx.core as mx
from mlx_graphs.nn import TGNMemory
from mlx_graphs.nn.models.tgn import IdentityMessage, LastAggregator
memory = TGNMemory(
num_nodes=100,
raw_msg_dim=16,
memory_dim=32,
time_dim=32,
message_module=IdentityMessage(16, 32, 32),
aggregator_module=LastAggregator(),
)
n_id = mx.array([0, 1, 2])
z, last_update = memory(n_id) # [3, 32], [3]
memory.update_state(src, dst, t, msg) # after computing the loss
memory.detach()
References
Methods:
| Name | Description |
|---|---|
reset_parameters |
Re-initializes all learnable parameters and resets the state. |
reset_state |
Resets the memory, the last update times and the message stores. |
memory_state |
The memory state as arrays, the equivalent of PyG's |
load_memory_state |
Restores a state returned by |
detach |
Detaches the memory from gradient computation (and evaluates it). |
__call__ |
Returns the current memory and last update time of the nodes |
update_state |
Updates the state with the new events |
Attributes:
| Name | Type | Description |
|---|---|---|
memory |
array
|
The memory of all nodes, shape |
last_update |
array
|
The time of the last memory update of every node, shape |
Source code in mlx_graphs/nn/models/tgn.py
last_update
property
¶
The time of the last memory update of every node, shape
[num_nodes].
reset_parameters
¶
Re-initializes all learnable parameters and resets the state.
Source code in mlx_graphs/nn/models/tgn.py
reset_state
¶
Resets the memory, the last update times and the message stores.
Source code in mlx_graphs/nn/models/tgn.py
memory_state
¶
memory_state() -> dict
The memory state as arrays, the equivalent of PyG's memory
and last_update buffers in state_dict: a dict with
"memory" ([num_nodes, memory_dim]) and "last_update"
([num_nodes], int64).
Source code in mlx_graphs/nn/models/tgn.py
load_memory_state
¶
load_memory_state(state: dict)
Restores a state returned by memory_state() (e.g. read back
with mx.load). The pending messages are cleared.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict
|
A dict with |
required |
Source code in mlx_graphs/nn/models/tgn.py
detach
¶
__call__
¶
__call__(n_id: IndexLike) -> Tuple[array, array]
Returns the current memory and last update time of the nodes n_id
(with the pending messages applied in training mode).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_id
|
IndexLike
|
Node indices, shape |
required |
Returns:
| Type | Description |
|---|---|
array
|
The memory, shape |
array
|
shape |
Source code in mlx_graphs/nn/models/tgn.py
update_state
¶
Updates the state with the new events (src, dst, t, raw_msg).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
IndexLike
|
Source nodes, shape |
required |
dst
|
IndexLike
|
Destination nodes, shape |
required |
t
|
IndexLike
|
Integer timestamps, shape |
required |
raw_msg
|
array
|
Raw messages, shape |
required |
Source code in mlx_graphs/nn/models/tgn.py
from mlx_graphs.nn import TGNMemory · mlx_graphs/nn/models/tgn.py