Skip to content

KGTripletLoader

KGTripletLoader(
    head_index: array,
    rel_type: array,
    tail_index: array,
    batch_size: int = 1,
    shuffle: bool = False,
    drop_last: bool = False,
)

Iterates over mini-batches of (head_index, rel_type, tail_index) triplets.

Parameters:

Name Type Description Default
head_index array

The head indices, shape [num_triplets]

required
rel_type array

The relation types, shape [num_triplets]

required
tail_index array

The tail indices, shape [num_triplets]

required
batch_size int

Number of triplets per batch. Default: 1

1
shuffle bool

Whether to shuffle the triplets at every epoch (with mx.random). Default: False

False
drop_last bool

Whether to drop the last incomplete batch. Default: False

False

Example:

loader = model.loader(
    head_index, rel_type, tail_index, batch_size=1000, shuffle=True
)
for head, rel, tail in loader:
    ...
Source code in mlx_graphs/nn/models/kge.py
def __init__(
    self,
    head_index: mx.array,
    rel_type: mx.array,
    tail_index: mx.array,
    batch_size: int = 1,
    shuffle: bool = False,
    drop_last: bool = False,
):
    if not (head_index.size == rel_type.size == tail_index.size):
        raise ValueError("head_index, rel_type and tail_index differ in size")
    self.head_index = head_index.reshape(-1)
    self.rel_type = rel_type.reshape(-1)
    self.tail_index = tail_index.reshape(-1)
    self.batch_size = batch_size
    self.shuffle = shuffle
    self.drop_last = drop_last

from mlx_graphs.nn import KGTripletLoader · mlx_graphs/nn/models/kge.py