Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Recommenders team Recommenders SASRec Sampler

From Leeroopedia


Knowledge Sources
Domains Sequential Recommendation, Data Sampling, Negative Sampling
Last Updated 2026-02-10 00:00 GMT

Overview

The WarpSampler class and sample_function provide multi-process batch sampling for the SASRec model, generating training sequences with positive and negative item pairs from user interaction histories.

Description

The sample_function runs in a separate process and continuously generates training batches that are placed on a multiprocessing Queue. For each sample, it randomly selects a user (ensuring they have more than one interaction), constructs a fixed-length input sequence from their interaction history (left-padded with zeros up to maxlen), creates a positive target sequence (shifted by one position representing the next item), and samples random negative items (items the user has not interacted with) using the random_neq helper function. The WarpSampler class manages a pool of daemon worker processes that independently produce batches. Each worker is seeded with a random integer for reproducibility. The next_batch method retrieves the next batch from the shared queue, and the close method terminates all worker processes and joins them for clean shutdown. The queue has a maximum size of n_workers * 10 to limit memory usage.

Usage

Use the WarpSampler during SASRec model training to efficiently generate batches of sequential training data with negative samples. It eliminates the data loading bottleneck by preparing batches asynchronously in separate processes while the GPU performs forward and backward passes. Configure batch_size and maxlen to match the model's expected input dimensions, and n_workers to control parallelism.

Code Reference

Source Location

Signature

def random_neq(left, right, s)

def sample_function(
    user_train, usernum, itemnum, batch_size, maxlen, result_queue, seed
)

class WarpSampler(object):
    def __init__(self, User, usernum, itemnum, batch_size=64, maxlen=10, n_workers=1)
    def next_batch(self)
    def close(self)

Import

from recommenders.models.sasrec.sampler import WarpSampler

I/O Contract

Inputs

Name Type Required Description
User dict Yes Dictionary mapping user IDs (keys) to lists of interacted item IDs (values)
usernum int Yes Total number of users
itemnum int Yes Total number of items
batch_size int No Number of samples per batch (default 64)
maxlen int No Maximum input sequence length (default 10)
n_workers int No Number of parallel worker processes (default 1)

Outputs

Name Type Description
next_batch return tuple Zipped tuple of (user_ids, sequences, positive_items, negative_items), each as arrays of shape (batch_size, maxlen) or (batch_size,)

Usage Examples

Basic Usage

from recommenders.models.sasrec.sampler import WarpSampler

# Create sampler from user training data
sampler = WarpSampler(
    User=user_train,
    usernum=num_users,
    itemnum=num_items,
    batch_size=128,
    maxlen=50,
    n_workers=3,
)

# Get training batches during training loop
for step in range(num_steps):
    user, seq, pos, neg = sampler.next_batch()
    # Feed to SASRec model for training
    loss = model.train_step(seq, pos, neg)

# Clean up worker processes
sampler.close()

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment