LightFMWrapperModel

class rectools.models.lightfm.LightFMWrapperModel(model: LightFM, epochs: int = 1, num_threads: int = 1, recommend_n_threads: Optional[int] = None, recommend_use_gpu_ranking: bool = True, verbose: int = 0)[source]

Bases: FixedColdRecoModelMixin, VectorModel[LightFMWrapperModelConfig]

Wrapper for lightfm.LightFM.

See https://making.lyst.com/lightfm/docs/home.html for details of base model.

SparseFeatures are used for this model, if you use DenseFeatures, it’ll be converted to sparse. Also it’s usually better to use categorical features. If you have real features (age, price, etc.), you can binarize it.

Parameters
  • model (LightFM) – Base model that will be used.

  • epochs (int, default 1) – Will be used as epochs parameter for LightFM.fit.

  • num_threads (int, default 1) – Will be used as num_threads parameter for LightFM.fit. Should be larger then 0. Can also be used as number of threads for recommendation ranking on CPU. See recommend_n_threads for details.

  • recommend_n_threads (Optional[int], default None) – Number of threads to use for recommendation ranking on CPU. Specifying 0 means to default to the number of cores on the machine. If None, then number of threads will be set same as num_threads. If you want to change this parameter after model is initialized, you can manually assign new value to model recommend_n_threads attribute.

  • recommend_use_gpu_ranking (bool, default True) – Flag to use GPU for recommendation ranking. Please note that GPU and CPU ranking may provide different ordering of items with identical scores in recommendation table. If True, implicit.gpu.HAS_CUDA will also be checked before ranking. If you want to change this parameter after model is initialized, you can manually assign new value to model recommend_use_gpu_ranking attribute.

  • verbose (int, default 0) – Degree of verbose output. If 0, no output will be provided.

Inherited-members

Methods

dumps()

Serialize model to bytes.

fit(dataset, *args, **kwargs)

Fit model.

fit_partial(dataset, *args, **kwargs)

Fit model.

from_config(config)

Create model from config.

get_config([mode, simple_types])

Return model config.

get_params([simple_types, sep])

Return model parameters.

get_vectors(dataset[, add_biases])

Return user and item vector representations from fitted model.

load(f)

Load model from file.

loads(data)

Load model from bytes.

recommend(users, dataset, k, filter_viewed)

Recommend items for users.

recommend_to_items(target_items, dataset, k)

Recommend items for target items.

save(f)

Save model to file.

Attributes

i2i_dist

recommends_for_cold

recommends_for_warm

u2i_dist

config_class

alias of LightFMWrapperModelConfig

get_vectors(dataset: Dataset, add_biases: bool = True) Tuple[ndarray, ndarray][source]

Return user and item vector representations from fitted model.

Parameters
  • dataset (Dataset) – Dataset with input data. Usually it’s the same dataset that was used to fit model.

  • add_biases (bool, default True) – LightFM model stores separately embeddings and biases for users and items. If False, only embeddings will be returned. If True, biases will be added as 2 first columns (see Returns section for details).

Returns

User and item embeddings.

If add_biases is False, shapes are (n_users, no_components) and (n_items, no_components).

If add_biases is True, shapes are (n_users, no_components + 2) and (n_items, no_components + 2). In that case (user_biases_column, ones_column) will be added to user embeddings, and (ones_column, item_biases_column) - to item embeddings. So, if you calculate user_embeddings @ item_embeddings.T, for each user-item pair you will get value user_embedding @ item_embedding + user_bias + item_bias.

Return type

(np.ndarray, np.ndarray)