recpack.algorithms.samplers.WarpSampler

class recpack.algorithms.samplers.WarpSampler(num_negatives=10, batch_size=100, exact=False)

Samples num_negatives negatives for each positive.

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

Parameters
  • 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.

Methods

sample(X[, sample_size, positives])

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

Attributes

ALLOWED_DISTRIBUTIONS

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.

Parameters
  • 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).

Raises

ValueError – [description]

Yield

Iterator of (user_batch, positive_samples_batch, negative_samples_batch)

Return type

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