TorchRanker

class rectools.models.rank.rank_torch.TorchRanker(distance: Distance, device: Union[device, str], subjects_factors: Union[ndarray, csr_matrix, Tensor], objects_factors: Union[ndarray, Tensor], batch_size: int = 128, dtype: Optional[dtype] = torch.float32)[source]

Bases: object

Ranker model based on torch.

This ranker is suitable for the following cases of scores calculation: 1. subject_embeddings.dot(objects_embeddings) 2. subject_interactions.dot(item-item-similarities)

Parameters
  • distance (Distance) – Distance metric.

  • device (torch.device | str) – Device to calculate on.

  • batch_size (int, default 128) – Batch size for scores calculation.

  • subjects_factors (np.ndarray | sparse.csr_matrix | torch.Tensor) – Array of subjects embeddings, shape (n_subjects, n_factors). For item-item similarity models subjects vectors from ui_csr are viewed as factors.

  • objects_factors (np.ndarray | torch.Tensor) – Array with embeddings of all objects, shape (n_objects, n_factors). For item-item similarity models item similarity vectors are viewed as factors.

  • dtype (torch.dtype, optional, default torch.float32) – dtype to convert non-torch tensors to. Conversion is skipped if provided dtype is None.

Inherited-members

Methods

rank(subject_ids[, k, filter_pairs_csr, ...])

Rank objects to proceed inference using implicit library topk cpu method.

Attributes

epsilon_cosine_dist

rank(subject_ids: Union[Sequence[int], ndarray], k: Optional[int] = None, filter_pairs_csr: Optional[csr_matrix] = None, sorted_object_whitelist: Optional[ndarray] = None) Tuple[Union[Sequence[int], ndarray], Union[Sequence[int], ndarray], Union[Sequence[float], ndarray]][source]

Rank objects to proceed inference using implicit library topk cpu method.

Parameters
  • subject_ids (InternalIds) – Array of ids to recommend for.

  • k (int, optional, default None) – Derived number of recommendations for every subject id. Return all recs if None.

  • filter_pairs_csr (sparse.csr_matrix, optional, default None) – Subject-object interactions that should be filtered from recommendations. This is relevant for u2i case.

  • sorted_object_whitelist (sparse.csr_matrix, optional, default None) – Whitelist of object ids. If given, only these items will be used for recommendations. Otherwise all items from dataset will be used.

Returns

Array of subject ids, array of recommended items, sorted by score descending and array of scores.

Return type

(InternalIds, InternalIds, Scores)