PinSage#

class libreco.algorithms.PinSage(task, data_info, loss_type='max_margin', paradigm='i2i', embed_size=16, n_epochs=20, lr=0.001, lr_decay=False, epsilon=1e-08, amsgrad=False, reg=None, batch_size=256, num_neg=1, dropout_rate=0.0, remove_edges=False, num_layers=2, num_neighbors=3, num_walks=10, neighbor_walk_len=2, sample_walk_len=5, termination_prob=0.5, margin=1.0, sampler='random', start_node='random', focus_start=False, seed=42, device='cuda', lower_upper_bound=None)[source]#

Bases: SageBase

PinSage algorithm. See PinSage for more details.

Note

This algorithm is implemented in PyTorch.

Caution

PinSage can only be used in ranking task.

New in version 0.12.0.

Parameters:
  • task ({'ranking'}) – Recommendation task. See Task.

  • data_info (DataInfo object) – Object that contains useful information for training and inference.

  • loss_type ({'cross_entropy', 'focal', 'bpr', 'max_margin'}, default: 'max_margin') – Loss for model training.

  • paradigm ({'u2i', 'i2i'}, default: 'i2i') –

    Choice for features in model.

    • 'u2i' will combine user features and item features.

    • 'i2i' will only use item features, this is the setting in the original paper.

  • embed_size (int, default: 16) – Vector size of embeddings.

  • n_epochs (int, default: 10) – Number of epochs for training.

  • lr (float, default 0.001) – Learning rate for training.

  • lr_decay (bool, default: False) – Whether to use learning rate decay.

  • epsilon (float, default: 1e-8) – A small constant added to the denominator to improve numerical stability in Adam optimizer.

  • amsgrad (bool, default: False) – Whether to use the AMSGrad variant from the paper On the Convergence of Adam and Beyond.

  • reg (float or None, default: None) – Regularization parameter, must be non-negative or None.

  • batch_size (int, default: 256) – Batch size for training.

  • num_neg (int, default: 1) – Number of negative samples for each positive sample.

  • dropout_rate (float, default: 0.0) – Probability of a node being dropped. 0.0 means dropout is not used.

  • remove_edges (bool, default: False) – Whether to remove edges between target node and its positive pair nodes when target node’s sampled neighbor nodes contain positive pair nodes. This only applies in ‘i2i’ paradigm.

  • num_layers (int, default: 2) – Number of GCN layers.

  • num_neighbors (int, default: 3) – Number of sampled neighbors in each layer

  • num_walks (int, default: 10) – Number of random walks to sample positive item pairs. This only applies in ‘i2i’ paradigm.

  • neighbor_walk_len (int, default: 2) – Length of random walk to sample neighbor nodes for target node.

  • sample_walk_len (int, default: 5) – Length of each random walk to sample positive item pairs.

  • termination_prob (float, default: 0.5) – Termination probability after one walk for neighbor random walk sampling.

  • margin (float, default: 1.0) – Margin used in max_margin loss.

  • sampler ({'random', 'unconsumed', 'popular', 'out-batch'}, default: 'random') –

    Negative sampling strategy. The 'u2i' paradigm can use 'random', 'unconsumed', 'popular', and the 'i2i' paradigm can use 'random', 'out-batch', 'popular'.

    • 'random' means random sampling.

    • 'unconsumed' samples items that the target user did not consume before. This can’t be used in 'i2i' since it has no users.

    • 'popular' has a higher probability to sample popular items as negative samples.

    • 'out-batch' samples items that didn’t appear in the batch. This can only be used in 'i2i' paradigm.

  • start_node ({'random', 'unpopular'}, default: 'random') – Strategy for choosing start nodes in random walks. 'unpopular' will place a higher probability on unpopular items, which may increase diversity but hurt metrics. This only applies in 'i2i' paradigm.

  • focus_start (bool, default: False) – Whether to keep the start nodes in random walk sampling. The purpose of the parameter start_node and focus_start is oversampling unpopular items. If you set start_node='popular' and focus_start=True, unpopular items will be kept in positive samples, which may increase diversity.

  • seed (int, default: 42) – Random seed.

  • device ({'cpu', 'cuda'}, default: 'cuda') –

    Refer to torch.device.

    Changed in version 1.0.0: Accept str type 'cpu' or 'cuda', instead of torch.device(...).

  • lower_upper_bound (tuple or None, default: None) – Lower and upper score bound for rating task.

See also

PinSageDGL

fit(train_data, neg_sampling, verbose=1, shuffle=True, eval_data=None, metrics=None, k=10, eval_batch_size=8192, eval_user_num=None, num_workers=0)#

Fit embed model on the training data.

Parameters:
  • train_data (TransformedSet object) – Data object used for training.

  • neg_sampling (bool) –

    Whether to perform negative sampling for training or evaluating data.

    New in version 1.1.0.

    Note

    Negative sampling is needed if your data is implicit(i.e., task is ranking) and ONLY contains positive labels. Otherwise, it should be False.

  • verbose (int, default: 1) –

    Print verbosity.

    • verbose <= 0: Print nothing.

    • verbose == 1: Print progress bar and training time.

    • verbose > 1 : Print evaluation metrics if eval_data is provided.

  • shuffle (bool, default: True) – Whether to shuffle the training data.

  • eval_data (TransformedSet object, default: None) – Data object used for evaluating.

  • metrics (list or None, default: None) – List of metrics for evaluating.

  • k (int, default: 10) – Parameter of metrics, e.g. recall at k, ndcg at k

  • eval_batch_size (int, default: 8192) – Batch size for evaluating.

  • eval_user_num (int or None, default: None) – Number of users for evaluating. Setting it to a positive number will sample users randomly from eval data.

  • num_workers (int, default: 0) –

    How many subprocesses to use for training data loading. 0 means that the data will be loaded in the main process, which is slower than multiprocessing.

    New in version 1.1.0.

    Caution

    Using multiprocessing(num_workers > 0) may consume more memory than single processing. See Multi-process data loading.

Raises:
get_item_embedding(item=None, include_bias=False)#

Get item embedding(s) from the model.

Parameters:
  • item (int or str or None, default: None) – Query item id. If it is None, all item embeddings will be returned.

  • include_bias (bool, default: False) – Whether to include bias term in returned embeddings.

Returns:

item_embedding – Returned item embeddings.

Return type:

numpy.ndarray

Raises:
  • ValueError – If the item does not appear in the training data.

  • AssertionError – If the model has not been trained.

get_user_embedding(user=None, include_bias=False)#

Get user embedding(s) from the model.

Parameters:
  • user (int or str or None, default: None) – Query user id. If it is None, all user embeddings will be returned.

  • include_bias (bool, default: False) – Whether to include bias term in returned embeddings.

Returns:

user_embedding – Returned user embeddings.

Return type:

numpy.ndarray

Raises:
  • ValueError – If the user does not appear in the training data.

  • AssertionError – If the model has not been trained.

init_knn(approximate, sim_type, M=100, ef_construction=200, ef_search=200)#

Initialize k-nearest-search model.

Parameters:
  • approximate (bool) – Whether to use approximate nearest neighbor search. If it is True, nmslib must be installed. The HNSW method in nmslib is used.

  • sim_type ({'cosine', 'inner-product'}) – Similarity space type.

  • M (int, default: 100) – Parameter in HNSW, refer to nmslib doc.

  • ef_construction (int, default: 200) –

    Parameter in HNSW, refer to nmslib doc.

  • ef_search (int, default: 200) –

    Parameter in HNSW, refer to nmslib doc.

Raises:
  • ValueError – If sim_type is not one of (‘cosine’, ‘inner-product’).

  • ModuleNotFoundError – If approximate=True and nmslib is not installed.

classmethod load(path, model_name, data_info, **kwargs)#

Load saved embed model for inference.

Parameters:
  • path (str) – File folder path to save model.

  • model_name (str) – Name of the saved model file.

  • data_info (DataInfo object) – Object that contains some useful information.

Returns:

model – Loaded embed model.

Return type:

type(cls)

See also

save

predict(user, item, cold_start='average', inner_id=False)#

Make prediction(s) on given user(s) and item(s).

Parameters:
  • user (int or str or array_like) – User id or batch of user ids.

  • item (int or str or array_like) – Item id or batch of item ids.

  • cold_start ({'popular', 'average'}, default: 'average') –

    Cold start strategy.

    • ’popular’ will sample from popular items.

    • ’average’ will use the average of all the user/item embeddings as the representation of the cold-start user/item.

  • inner_id (bool, default: False) – Whether to use inner_id defined in libreco. For library users inner_id may never be used.

Returns:

prediction – Predicted scores for each user-item pair.

Return type:

float or numpy.ndarray

rebuild_model(path, model_name)#

Assign the saved model variables to the newly initialized model.

This method is used before retraining the new model, in order to avoid training from scratch every time we get some new data.

Parameters:
  • path (str) – File folder path for the saved model variables.

  • model_name (str) – Name of the saved model file.

recommend_user(user, n_rec, cold_start='average', inner_id=False, filter_consumed=True, random_rec=False)#

Recommend a list of items for given user(s).

Parameters:
  • user (int or str or array_like) – User id or batch of user ids to recommend.

  • n_rec (int) – Number of recommendations to return.

  • cold_start ({'popular', 'average'}, default: 'average') –

    Cold start strategy.

    • ’popular’ will sample from popular items.

    • ’average’ will use the average of all the user/item embeddings as the representation of the cold-start user/item.

  • inner_id (bool, default: False) – Whether to use inner_id defined in libreco. For library users inner_id may never be used.

  • filter_consumed (bool, default: True) – Whether to filter out items that a user has previously consumed.

  • random_rec (bool, default: False) – Whether to choose items for recommendation based on their prediction scores.

Returns:

recommendation – Recommendation result with user ids as keys and array_like recommended items as values.

Return type:

dict of {Union[int, str, array_like] : numpy.ndarray}

save(path, model_name, inference_only=False, **kwargs)#

Save embed model for inference or retraining.

Parameters:
  • path (str) – File folder path to save model.

  • model_name (str) – Name of the saved model file.

  • inference_only (bool, default: False) – Whether to save model only for inference. If it is True, only embeddings will be saved. Otherwise, model variables will be saved.

See also

load

search_knn_items(item, k)#

Search most similar k items.

Parameters:
  • item (int or str) – Query item id.

  • k (int) – Number of similar items.

Returns:

similar items – A list of k similar items.

Return type:

list

search_knn_users(user, k)#

Search most similar k users.

Parameters:
  • user (int or str) – Query user id.

  • k (int) – Number of similar users.

Returns:

similar users – A list of k similar users.

Return type:

list