Replay methods store a small buffer of past examples and mix them into every training batch. The buffer policy decides which examples survive. Implement two of them as classes with the same interface.
ReservoirBuffer(capacity, seed=0)
add(x, y) processes one example (x any object, y an int label): while the buffer has room, append; afterwards, with the -th example seen ( counted from 1), draw j = rng.integers(0, n) and replace slot j only if j < capacity.np.random.default_rng(seed) in the constructor and make exactly one integers draw per example once the buffer is full, so results are reproducible.__len__ returns the number stored, labels() returns the list of stored labels in slot order, n_seen counts every example ever added.ClassBalancedRing(capacity, n_classes)
capacity // n_classes slots (any remainder is unused). add(x, y) appends to class y's list if it has room, otherwise overwrites that class's oldest example (FIFO per class).__len__, labels() (class order, then insertion order within a class) and n_seen as above.sample_batch(buffer, k, rng)
k stored examples without replacement using rng.choice(len(buffer), size=k, replace=False) over slot indices in the order returned by buffer.items(), which both classes implement as a list of (x, y) pairs. Return the list of chosen (x, y) pairs.collections.deque with maxlen, gives the ring behaviour.items() for the ring must flatten classes in class order so sample_batch sees a stable layout.