File size: 1,006 Bytes
2bc5689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from __future__ import annotations

import numpy as np

VALUE_TOKENS = 10
QUERY_TOKENS = 4
VOCAB_SIZE = VALUE_TOKENS + QUERY_TOKENS


def generate_selective_memory(
    samples: int,
    length: int,
    seed: int,
    marked_items: int = 4,
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
    if length < marked_items + 2:
        raise ValueError("Sequence is too short for the requested marked items.")
    rng = np.random.default_rng(seed)
    tokens = rng.integers(0, VALUE_TOKENS, size=(samples, length), dtype=np.int64)
    markers = np.zeros((samples, length), dtype=np.float32)
    targets = np.zeros(samples, dtype=np.int64)
    for row in range(samples):
        positions = np.sort(
            rng.choice(np.arange(1, length - 1), marked_items, replace=False)
        )
        markers[row, positions] = 1
        query = int(rng.integers(0, marked_items))
        targets[row] = tokens[row, positions[query]]
        tokens[row, -1] = VALUE_TOKENS + query
    return tokens, markers, targets