class recpack.algorithms.samplers.BootstrapSampler(num_negatives=1, batch_size=100, exact=False)

Sampler that samples positives with replacement.

This approach allows to learn multiple times from the same positive interactions. For more information on implementation see PositiveNegativeSampler

  • num_negatives (int, optional) – Number of negative samples for each positive, defaults to 1

  • batch_size (int, optional) – The number of samples returned per batch, defaults to 100

  • exact (bool, optional) – If False (default) negatives are checked agains the corresponding positive sample only, allowing for (rare) collisions. If collisions should be avoided at all costs, use exact = True, but suffer decreased performance.


sample(X[, sample_size, positives])

Sample num_negatives negatives for each sampled user-item-pair (positive).



sample(X: scipy.sparse._csr.csr_matrix, sample_size=None, positives=None) Iterator[Tuple[torch.LongTensor, torch.LongTensor, torch.LongTensor]]

Sample num_negatives negatives for each sampled user-item-pair (positive).

When sampling without replacement, sample_size cannot exceed the number of positives in X.

  • X (csr_matrix) – Matrix with interactions to sample from.

  • sample_size (int, optional) – The number of samples to create, if None, the number of positives entries in X will be used. Defaults to None.

  • positives (np.array, optional) – Restrict positives samples to only samples in this np.array of dimension (num_samples, 2).


ValueError – [description]


Iterator of (user_batch, positive_samples_batch, negative_samples_batch)

Return type

Iterator[Tuple[torch.LongTensor, torch.LongTensor, torch.LongTensor]]