### Install RePlay Basic Source: https://github.com/sb-ai-lab/replay/blob/main/CONTRIBUTING.md Installs the RePlay package using pip. This is the most straightforward way to get started with RePlay. ```bash pip install replay-rec ``` -------------------------------- ### Install rs-datasets Source: https://github.com/sb-ai-lab/replay/blob/main/examples/08_recommending_for_categories.ipynb Installs the 'rs-datasets' package quietly, which is likely used for accessing recommendation system datasets like MovieLens. ```python ! pip install rs-datasets -q ``` -------------------------------- ### Install rs-datasets Package Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Installs the `rs-datasets` package, which is required for loading datasets like MovieLens. ```Bash !pip install rs-datasets ``` -------------------------------- ### Install rs-datasets Package Source: https://github.com/sb-ai-lab/replay/blob/main/examples/10_bert4rec_example.ipynb Installs the 'rs-datasets' library, which is required for loading the MovieLens dataset used in the example. ```Python import lightning as L from lightning.pytorch.loggers import CSVLogger from lightning.pytorch.callbacks import ModelCheckpoint from torch.utils.data import DataLoader import torch from replay.metrics import OfflineMetrics, Recall, Precision, MAP, NDCG, HitRate, MRR from replay.metrics.torch_metrics_builder import metrics_to_df from replay.splitters import LastNSplitter from replay.data import ( FeatureHint, FeatureInfo, FeatureSchema, FeatureSource, FeatureType, Dataset, ) from replay.models.nn.optimizer_utils import FatOptimizerFactory from replay.models.nn.sequential.callbacks import ( ValidationMetricsCallback, SparkPredictionCallback, PandasPredictionCallback, TorchPredictionCallback, QueryEmbeddingsPredictionCallback, ) from replay.models.nn.sequential.postprocessors import RemoveSeenItems from replay.data.nn import ( SequenceTokenizer, SequentialDataset, TensorFeatureSource, TensorSchema, TensorFeatureInfo ) from replay.models.nn.sequential import Bert4Rec from replay.models.nn.sequential.bert4rec import ( Bert4RecPredictionDataset, Bert4RecTrainingDataset, Bert4RecValidationDataset, Bert4RecPredictionBatch, Bert4RecModel ) import pandas as pd ``` -------------------------------- ### Install Replay Datasets (Bash) Source: https://github.com/sb-ai-lab/replay/blob/main/examples/features_for_sequential_models.ipynb Installs the 'rs-datasets' package, which is required to easily load benchmark datasets like MovieLens for use with the Replay library. ```Bash !pip install rs-datasets ``` -------------------------------- ### Install RePlay Datasets Source: https://github.com/sb-ai-lab/replay/blob/main/examples/02_models_comparison.ipynb Installs the rs-datasets library, which is a dependency for loading datasets like MovieLens. ```Python ! pip install rs-datasets ``` -------------------------------- ### Install RePlay with all features Source: https://github.com/sb-ai-lab/replay/blob/main/README.md This command installs the RePlay library with all its optional dependencies, enabling the full range of features for recommendation system development. ```bash pip install replay-rec[all] ``` -------------------------------- ### Install RePlay with Poetry Source: https://github.com/sb-ai-lab/replay/blob/main/CONTRIBUTING.md Installs RePlay using Poetry version 1.5.1, including all extras. This command assumes Poetry is installed and sets up the project environment. ```bash pip install poetry==1.5.1 ./poetry_wrapper.sh install --all-extras ``` -------------------------------- ### Quickstart: Data Preparation and Splitting with RePlay Source: https://github.com/sb-ai-lab/replay/blob/main/README.md This Python code demonstrates a quickstart for RePlay, showing how to load the MovieLens dataset, preprocess it using Polars, and split it into training and testing sets using RePlay's RatioSplitter. It also defines a FeatureSchema for the dataset. ```python from polars import from_pandas from rs_datasets import MovieLens from replay.data import Dataset, FeatureHint, FeatureInfo, FeatureSchema, FeatureType from replay.data.dataset_utils import DatasetLabelEncoder from replay.metrics import HitRate, NDCG, Experiment from replay.models import ItemKNN from replay.utils.spark_utils import convert2spark from replay.utils.session_handler import State from replay.splitters import RatioSplitter spark = State().session ml_1m = MovieLens("1m") K = 10 # convert data to polars interactions = from_pandas(ml_1m.ratings) # data splitting splitter = RatioSplitter( test_size=0.3, divide_column="user_id", query_column="user_id", item_column="item_id", timestamp_column="timestamp", drop_cold_items=True, drop_cold_users=True, ) train, test = splitter.split(interactions) # datasets creation feature_schema = FeatureSchema( [ FeatureInfo( column="user_id", feature_type=FeatureType.CATEGORICAL, feature_hint=FeatureHint.QUERY_ID, ), FeatureInfo( column="item_id", feature_type=FeatureType.CATEGORICAL, feature_hint=FeatureHint.ITEM_ID, ), FeatureInfo( column="rating", feature_type=FeatureType.NUMERICAL, feature_hint=FeatureHint.RATING, ), FeatureInfo( column="timestamp", feature_type=FeatureType.NUMERICAL, feature_hint=FeatureHint.TIMESTAMP, ), ] ) train_dataset = Dataset(feature_schema=feature_schema, interactions=train) test_dataset = Dataset(feature_schema=feature_schema, interactions=test) ``` -------------------------------- ### Install RePlay Datasets Package Source: https://github.com/sb-ai-lab/replay/blob/main/examples/14_hierarchical_recommender.ipynb Installs the 'rs-datasets' package, which is required for loading datasets like MovieLens. ```python !pip install -q rs-datasets ``` -------------------------------- ### Install RePlay Core Package Source: https://github.com/sb-ai-lab/replay/blob/main/README.md This command installs the core RePlay package using pip. This installation includes the main functionalities but excludes dependencies for PySpark and PyTorch, as well as the experimental submodule. ```bash pip install replay-rec ``` -------------------------------- ### Install rs-datasets Package Source: https://github.com/sb-ai-lab/replay/blob/main/examples/12_neural_ts_exp.ipynb Installs the 'rs-datasets' library, which is required for loading and accessing datasets like MovieLens. ```python ! pip install rs-datasets ``` -------------------------------- ### Install Replay Datasets Source: https://github.com/sb-ai-lab/replay/blob/main/examples/03_features_preprocessing_and_lightFM.ipynb Installs the 'rs-datasets' package, which is required for loading recommendation datasets like MovieLens. ```bash ! pip install rs-datasets ``` -------------------------------- ### Install rs-datasets Package Source: https://github.com/sb-ai-lab/replay/blob/main/examples/07_filters.ipynb Installs the rs-datasets package with the '-q' flag for quiet installation. ```python ! ``` -------------------------------- ### Install RePlay with Experimental Submodule Source: https://github.com/sb-ai-lab/replay/blob/main/CONTRIBUTING.md Installs RePlay along with its experimental submodule using Poetry. This command enables features that are under active development. ```bash pip install poetry==1.5.1 ./poetry_wrapper.sh --experimental install --all-extras ``` -------------------------------- ### Install rs-datasets Package Source: https://github.com/sb-ai-lab/replay/blob/main/examples/13_personalized_bandit_comparison.ipynb Installs the 'rs-datasets' library, which is required for loading and accessing datasets like MovieLens. ```Python # ! pip install rs-datasets ``` -------------------------------- ### Install Build Essentials Source: https://github.com/sb-ai-lab/replay/blob/main/CONTRIBUTING.md Installs essential build tools required for compiling C/C++ extensions, which are dependencies for RePlay. This command is for Debian-based systems like Ubuntu. ```bash sudo apt-get install build-essential ``` -------------------------------- ### Experiment Setup for Metrics Source: https://github.com/sb-ai-lab/replay/blob/main/examples/01_replay_basics.ipynb Sets up an experiment to track multiple recommender system metrics. This involves initializing an Experiment object with a list of metrics and datasets. ```Python metrics = Experiment( [ NDCG(K), MAP(K), HitRate([1, K]), Coverage(K) ], test_dataset.interactions, train_dataset.interactions, query_column="user_id", item_column="item_id", rating_column="rating", ) ``` -------------------------------- ### Load MovieLens Dataset Source: https://github.com/sb-ai-lab/replay/blob/main/examples/08_recommending_for_categories.ipynb Loads the '10m' version of the MovieLens dataset using the rs_datasets package and displays its information. ```python data = MovieLens("10m") data.info() ``` -------------------------------- ### Install RePlay with Experimental Submodule Source: https://github.com/sb-ai-lab/replay/blob/main/README.md This command installs the RePlay package including the experimental submodule. It specifies a version with an 'rc0' suffix, indicating a release candidate version. ```bash pip install replay-rec==XX.YY.ZZrc0 ``` -------------------------------- ### Clone RePlay Repository Source: https://github.com/sb-ai-lab/replay/blob/main/CONTRIBUTING.md Clones the RePlay project from its GitHub repository to your local machine using SSH. This is the first step for installing from source. ```bash git clone git@github.com:sb-ai-lab/RePlay.git cd RePlay ``` -------------------------------- ### Install RePlay with PySpark Dependency Source: https://github.com/sb-ai-lab/replay/blob/main/README.md This command installs the RePlay package with the PySpark dependency enabled. This allows the use of PySpark functionalities within RePlay. ```bash pip install replay-rec[spark] ``` -------------------------------- ### Install RePlay with PySpark and Experimental Submodule Source: https://github.com/sb-ai-lab/replay/blob/main/README.md This command installs the RePlay package with both the experimental submodule and the PySpark dependency. It specifies a version with an 'rc0' suffix. ```bash pip install replay-rec[spark]==XX.YY.ZZrc0 ``` -------------------------------- ### Initialize Label Encoders Source: https://github.com/sb-ai-lab/replay/blob/main/examples/08_recommending_for_categories.ipynb Initializes LabelEncoder instances for 'user_id' and 'item_id' using LabelEncodingRule to prepare for re-indexing. ```python user_encoder = LabelEncoder([LabelEncodingRule("user_id")]) item_encoder = LabelEncoder([LabelEncodingRule("item_id")]) ``` -------------------------------- ### Inference for Single User Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Provides an example of launching inference for a single user without using a trainer, useful for speeding up production scripts. It details creating a user item sequence, generating a corresponding padding mask, and wrapping these in a `SasRecPredictionBatch` entity. ```Python item_sequence = torch.arange(1, 5).unsqueeze(0)[:, -MAX_SEQ_LEN:] padding_mask = torch.ones_like(item_sequence, dtype=torch.bool) sequence_item_count = item_sequence.shape[1] ``` ```Python batch = SasRecPredictionBatch( query_id=torch.arange(0, item_sequence.shape[0], 1).long(), padding_mask=padding_mask.bool(), features={ITEM_FEATURE_NAME: item_sequence.long()}, ) ``` -------------------------------- ### Import RePlay Libraries Source: https://github.com/sb-ai-lab/replay/blob/main/examples/08_recommending_for_categories.ipynb Imports necessary modules from the RePlay library for data manipulation, preprocessing, models, and utility functions. This includes Spark functionalities, dataset handling, and specific models like CatPopRec. ```python from pyspark.sql import Window from pyspark.sql.functions import split import pyspark.sql.functions as sf from replay.data import Dataset, FeatureSchema, FeatureInfo, FeatureHint, FeatureType from replay.preprocessing.label_encoder import LabelEncoder, LabelEncodingRule from replay.models import CatPopRec from replay.utils.session_handler import State from replay.splitters import TimeSplitter from replay.utils.spark_utils import get_log_info, convert2spark from rs_datasets import MovieLens ``` -------------------------------- ### Show Sample Interactions Source: https://github.com/sb-ai-lab/replay/blob/main/examples/08_recommending_for_categories.ipynb Displays the first 5 rows of the converted interactions Spark DataFrame to show the data format. ```python interactions.show(5) ``` -------------------------------- ### Prepare Dataloader and Logger for Inference Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Sets up the DataLoader for the test dataset and initializes a CSV logger for the inference stage. ```Python prediction_dataloader = DataLoader( dataset=SasRecPredictionDataset( sequential_test_dataset, max_sequence_length=MAX_SEQ_LEN, ), batch_size=BATCH_SIZE, num_workers=NUM_WORKERS, pin_memory=True, ) csv_logger = CSVLogger(save_dir=".logs/test", name="SASRec_example") ``` -------------------------------- ### Split Data for Training and Testing Source: https://github.com/sb-ai-lab/replay/blob/main/examples/08_recommending_for_categories.ipynb Splits the re-indexed log data into training and testing sets using TimeSplitter. It drops cold items and users and prints information about the resulting splits. ```python # train/test split train_spl = TimeSplitter( time_threshold=0.2, drop_cold_items=True, drop_cold_users=True, query_column="user_id", ) train, test = train_spl.split(log_replay) print('train info:\n', get_log_info(train, user_col='user_id', item_col='item_id')) print('test info:\n', get_log_info(test, user_col='user_id', item_col='item_id')) ``` -------------------------------- ### Configure Replay Logger Source: https://github.com/sb-ai-lab/replay/blob/main/docs/pages/settings.rst Provides an example of how to configure the 'replay' logger. It shows how to get the logger instance and set its logging level, for example, to DEBUG to capture more detailed information. ```python import logging logger = logging.getLogger("replay") logger.setLevel(logging.DEBUG) ``` -------------------------------- ### Analyze Number of Genres per Item Source: https://github.com/sb-ai-lab/replay/blob/main/examples/08_recommending_for_categories.ipynb Calculates the number of genres for each item and plots a histogram of the genre counts to visualize the distribution. ```python num_genres = genres.select("item_id", sf.size("genres").alias("num_genres")) num_genres.toPandas()["num_genres"].hist(bins=10) ``` -------------------------------- ### Run SasRec predict and get scores Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Executes the prediction step of the SasRec model to obtain scores for a given batch. It utilizes torch.no_grad() for efficient inference. ```Python with torch.no_grad(): scores = best_model.predict(batch) scores ``` -------------------------------- ### Get Spark Session Source: https://github.com/sb-ai-lab/replay/blob/main/examples/10_bert4rec_example.ipynb Initializes a Spark session required for operating with PySpark DataFrames using the SparkPredictionCallback. This is a prerequisite for using PySpark-related functionalities in the Replay project. ```Python from replay.utils import get_spark_session spark_session = get_spark_session() ``` -------------------------------- ### Get compiled SasRec model prediction shape Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Runs inference using the compiled SasRec model with a prepared batch and returns the shape of the resulting scores tensor. ```Python opt_model.predict(batch).shape ``` -------------------------------- ### Show Recommendations Source: https://github.com/sb-ai-lab/replay/blob/main/examples/01_replay_basics.ipynb Displays the first two generated recommendations from the SLIM model's prediction output. ```Python recs.show(2) ``` -------------------------------- ### Run SasRec predict with candidates and get scores Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Runs the prediction step of the SasRec model, allowing the specification of candidates to score. This is useful for targeted scoring scenarios. ```Python with torch.no_grad(): scores = best_model.predict(batch, candidates_to_score=CANDIDATES) scores ``` -------------------------------- ### Initialize SLIM Model Source: https://github.com/sb-ai-lab/replay/blob/main/examples/01_replay_basics.ipynb Initializes the SLIM (Sparse Linear Methods) recommender model with a specified random seed for reproducibility. ```Python slim = SLIM(seed=SEED) ``` -------------------------------- ### Import Libraries for SASRec Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Imports necessary libraries from `lightning`, `torch`, and `replay` for building and training the SASRec model. This includes data handling, metrics, callbacks, and model components. ```Python import lightning as L from lightning.pytorch.loggers import CSVLogger from lightning.pytorch.callbacks import ModelCheckpoint from torch.utils.data import DataLoader import torch from replay.metrics import OfflineMetrics, Recall, Precision, MAP, NDCG, HitRate, MRR from replay.metrics.torch_metrics_builder import metrics_to_df from replay.splitters import LastNSplitter from replay.data import ( FeatureHint, FeatureInfo, FeatureSchema, FeatureSource, FeatureType, Dataset, ) from replay.models.nn.optimizer_utils import FatOptimizerFactory from replay.models.nn.sequential.callbacks import ( ValidationMetricsCallback, SparkPredictionCallback, PandasPredictionCallback, TorchPredictionCallback, QueryEmbeddingsPredictionCallback, ) from replay.models.nn.sequential.postprocessors import RemoveSeenItems from replay.data.nn import SequenceTokenizer, SequentialDataset, TensorFeatureSource, TensorSchema, TensorFeatureInfo from replay.models.nn.sequential import SasRec from replay.models.nn.sequential.sasrec import ( SasRecPredictionDataset, SasRecTrainingDataset, SasRecValidationDataset, SasRecPredictionBatch, SasRecModel, ) import pandas as pd ``` -------------------------------- ### Show Sample Item Features Source: https://github.com/sb-ai-lab/replay/blob/main/examples/08_recommending_for_categories.ipynb Displays the first 2 rows of the converted item features Spark DataFrame to show the data format. ```python item_features.show(2) ``` -------------------------------- ### Get Subset Inference Results Source: https://github.com/sb-ai-lab/replay/blob/main/examples/10_bert4rec_example.ipynb Retrieves the inference results when a subset of items was specified using 'candidates_to_score'. The results will only contain scores for the items present in the 'candidates_to_score' tensor. ```Python pandas_prediction_callback.get_result() ``` -------------------------------- ### Sample Categorized Interactions Source: https://github.com/sb-ai-lab/replay/blob/main/examples/08_recommending_for_categories.ipynb This Python code snippet samples a small fraction (0.01%) of the categorized interaction data and displays the first 5 sampled records. ```Python train_genre.sample(0.0001).show(5) ``` -------------------------------- ### Get top 3 scored items from SasRec Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Retrieves the indices of the three items with the highest scores from the SasRec model's prediction output. This is achieved using torch.topk. ```Python with torch.no_grad(): scores = best_model.predict(batch) torch.topk(scores, k=3).indices ``` -------------------------------- ### Inverse Transform Recommendations (PySpark) Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Applies an inverse transformation to the PySpark recommendations using a tokenizer's encoder to get the original representation of labels. This is specific to PySpark and Pandas formats. ```Python recommendations = tokenizer.query_and_item_id_encoder.inverse_transform(spark_res) ``` -------------------------------- ### Create Training Dataset Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Creates a `Dataset` object for the training stage using the prepared interactions, user features, and item features, along with the appropriate feature schema. ```Python train_dataset = Dataset( feature_schema=prepare_feature_schema(is_ground_truth=False), interactions=raw_train_events, query_features=user_features, item_features=item_features, check_consistency=True, categorical_encoded=False, ) ``` -------------------------------- ### Get Inverse Representation of Recommendations Source: https://github.com/sb-ai-lab/replay/blob/main/examples/10_bert4rec_example.ipynb Obtains the inverse representation of recommendation labels using the inverse_transform method, specifically for PySpark DataFrames. This converts encoded item IDs back to their original labels. ```Python recommendations = tokenizer.query_and_item_id_encoder.inverse_transform(spark_res) ``` -------------------------------- ### Print Customized Shuffle Partitions Source: https://github.com/sb-ai-lab/replay/blob/main/examples/01_replay_basics.ipynb Prints the 'spark.sql.shuffle.partitions' configuration after customizing the Spark session, verifying the change. ```python print_config_param(spark, "spark.sql.shuffle.partitions") ``` -------------------------------- ### Get compiled SasRec model prediction shape with candidates Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Executes prediction with the compiled SasRec model, including specified candidates, and returns the shape of the output scores. This demonstrates inference with candidate scoring enabled. ```Python opt_model.predict(batch, CANDIDATES).shape ``` -------------------------------- ### Access and Manage Item Embeddings Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Provides methods to retrieve, update, and append item embeddings from a model. It covers using `get_all_embeddings` to get a dictionary of embeddings, accessing specific embeddings like 'item_embedding', and modifying them by size or by tensor. ```Python all_embeddings = best_model.get_all_embeddings() all_embeddings ``` ```Python item_embeddings = all_embeddings["item_embedding"] item_embeddings ``` ```Python item_embeddings.shape ``` ```Python assert item_embeddings.shape[0] == len(tokenizer.item_id_encoder.mapping["item_id"]) assert id(item_embeddings) != id(best_model._model.item_embedder.item_emb.weight.data) ``` ```Python best_model.set_item_embeddings_by_size(item_embeddings.shape[0] + 1) ``` ```Python new_size = best_model.get_all_embeddings()["item_embedding"].shape[0] old_size = item_embeddings.shape[0] assert new_size == old_size + 1 ``` ```Python new_embeddings_weights = torch.rand((new_size + 1, 300)) # randint used for example only best_model.set_item_embeddings_by_tensor(new_embeddings_weights) ``` ```Python old_size = new_size new_size = best_model.get_all_embeddings()["item_embedding"].shape[0] assert new_size == old_size + 1 ``` ```Python new_item_weights = torch.rand((1, 300)) # randint used for example only best_model.append_item_embeddings(new_item_weights) ``` ```Python old_size = new_size new_size = best_model.get_all_embeddings()["item_embedding"].shape[0] assert new_size == old_size + 1 ``` -------------------------------- ### Get User Embeddings with Bert4RecModel Source: https://github.com/sb-ai-lab/replay/blob/main/examples/10_bert4rec_example.ipynb Demonstrates how to obtain user embeddings using the Bert4RecModel. It involves setting up the model on the appropriate device (CUDA or CPU), preparing input data tensors, and then calling the `get_query_embeddings` method. The shape of the resulting embeddings is also shown. ```Torch device = "cuda" if torch.cuda.is_available() else "cpu" core_model = Bert4RecModel( tensor_schema, num_blocks=2, num_heads=4, max_len=MAX_SEQ_LEN, hidden_size=300, dropout=0.5 ) core_model.eval() core_model = core_model.to(device) # Get first batch of data data = next(iter(prediction_dataloader)) tensor_map, padding_mask, tokens_mask = data.features, data.padding_mask, data.tokens_mask # Ensure everything is on the same device padding_mask = padding_mask.to(device) tokens_mask = tokens_mask.to(device) tensor_map["item_id_seq"] = tensor_map["item_id_seq"].to(device) # Get user embeddings user_embeddings_batch = core_model.get_query_embeddings(tensor_map, padding_mask, tokens_mask) user_embeddings_batch ``` ```Torch user_embeddings_batch.shape ``` -------------------------------- ### Run Bert4Rec Prediction and Get Scores Source: https://github.com/sb-ai-lab/replay/blob/main/examples/10_bert4rec_example.ipynb Executes the prediction step of the Bert4Rec model to obtain scores for the input batch. It demonstrates how to use `torch.no_grad()` for inference and how to optionally provide `candidates_to_score` to the predict method. The top five item indices with the highest scores are then retrieved. ```Torch with torch.no_grad(): scores = best_model.predict(batch) scores ``` ```Torch torch.topk(scores, k=5).indices ``` ```Torch with torch.no_grad(): scores = best_model.predict(batch, candidates_to_score=CANDIDATES) scores ``` -------------------------------- ### Define Configuration Parameters Source: https://github.com/sb-ai-lab/replay/blob/main/examples/14_hierarchical_recommender.ipynb Sets up various configuration parameters used throughout the experiment, including the number of recommendations (K), lists of metrics K values, budget for certain models, and a random seed for reproducibility. ```python K = 10 K_list_metrics = [1, 5, 10] BUDGET = 20 BUDGET_NN = 10 SEED = 12345 ``` -------------------------------- ### Run Experiment Pipeline Source: https://github.com/sb-ai-lab/replay/blob/main/examples/14_hierarchical_recommender.ipynb Sets up an experiment with various evaluation metrics (MAP, NDCG, HitRate, Coverage, Surprisal, MRR) and runs the full pipeline with the defined hierarchical recommenders. ```python %%time e = Experiment( [ MAP(K), NDCG(K), HitRate(K_list_metrics), Coverage(K), Surprisal(K), MRR(K)], test, pos_neg_train, query_column="user_idx", item_column="item_idx", rating_column="relevance" ) full_pipeline(hcbs, e, train, budget=BUDGET) ``` -------------------------------- ### Install Dependencies Source: https://github.com/sb-ai-lab/replay/blob/main/examples/05_feature_generators.ipynb Installs the seaborn and matplotlib libraries, which are commonly used for data visualization in Python projects. ```Python !pip install seaborn matplotlib ``` -------------------------------- ### Generate Recommendations with SLIM Source: https://github.com/sb-ai-lab/replay/blob/main/examples/01_replay_basics.ipynb Generates recommendations using the trained SLIM model for specified queries from the test dataset. It includes options to filter out already seen items and uses the training dataset for prediction context. The `%%time` magic command measures the prediction time. ```Python %%time recs = slim.predict( k=K, queries=test_dataset.query_ids, dataset=train_dataset, filter_seen_items=False ) ``` -------------------------------- ### Create Recommendation System Datasets Source: https://github.com/sb-ai-lab/replay/blob/main/examples/02_models_comparison.ipynb This snippet demonstrates the creation of multiple Dataset objects for a recommendation system, including an all dataset, training dataset, testing dataset, and datasets for negative and optional training/validation. Each Dataset is initialized with the defined feature schema and corresponding interaction data. ```Python all_dataset = Dataset( feature_schema=feature_schema, interactions=interactions_spark, ) train_dataset = Dataset( feature_schema=feature_schema, interactions=train, ) test_dataset = Dataset( feature_schema=feature_schema, interactions=test, ) train_neg_dataset = Dataset( feature_schema=feature_schema, interactions=pos_neg_train, ) opt_train_dataset = Dataset( feature_schema=feature_schema, interactions=opt_train, ) opt_val_dataset = Dataset( feature_schema=feature_schema, interactions=opt_val, ) ``` -------------------------------- ### CQL Recommender Initialization Source: https://github.com/sb-ai-lab/replay/blob/main/docs/pages/modules/models.rst Initializes the Conservative Q-Learning (CQL) Recommender, an experimental algorithm designed for offline reinforcement learning tasks, known for achieving state-of-the-art performance. ```Python class CQL: def __init__(self): pass ``` -------------------------------- ### Initialize ItemKNN Model Source: https://github.com/sb-ai-lab/replay/blob/main/examples/06_item2item_recommendations.ipynb Initializes an instance of the ItemKNN recommendation model. ```Python model_knn = ItemKNN() ``` -------------------------------- ### Load MovieLens Dataset Source: https://github.com/sb-ai-lab/replay/blob/main/examples/14_hierarchical_recommender.ipynb Loads the MovieLens 1m dataset using the rs_datasets library and displays its information. This is the first step in preparing the data for recommender models. ```python data = MovieLens("1m") data.info() ``` -------------------------------- ### Install rs-datasets Package Source: https://github.com/sb-ai-lab/replay/blob/main/examples/11_sasrec_dataframes_comparison.ipynb Installs the 'rs-datasets' package, which is likely used for loading recommendation system datasets. ```Shell !pip install rs-datasets ``` -------------------------------- ### Create Training and Test Datasets Source: https://github.com/sb-ai-lab/replay/blob/main/examples/01_replay_basics.ipynb Creates RePlay Dataset objects for training and testing, associating the feature schema with the respective dataframes. ```Python train_dataset = Dataset( feature_schema=feature_schema, interactions=train, ) test_dataset = Dataset( feature_schema=feature_schema, interactions=test, ) ``` -------------------------------- ### Show Training Data Sample Source: https://github.com/sb-ai-lab/replay/blob/main/examples/14_hierarchical_recommender.ipynb Displays the first two rows of the 'train' DataFrame. ```Python train.show(2) ``` -------------------------------- ### Save and Load Encoder Source: https://github.com/sb-ai-lab/replay/blob/main/examples/01_replay_basics.ipynb Demonstrates how to save a fitted encoder to disk and then load it back. This is crucial for applying the same transformations to new data or for deployment. ```Python save_encoder(encoder, "./encoder") encoder_loaded = load_encoder("./encoder") ``` -------------------------------- ### Create Dataset for ItemKNN Source: https://github.com/sb-ai-lab/replay/blob/main/examples/06_item2item_recommendations.ipynb Creates a Dataset object for the ItemKNN model using the defined feature schema and the prepared training interactions. ```Python train_dataset = Dataset( feature_schema=feature_schema, interactions=train_knn, ) ``` -------------------------------- ### Prepare Data with DataPreparator Source: https://github.com/sb-ai-lab/replay/blob/main/examples/06_item2item_recommendations.ipynb Initializes a DataPreparator and transforms the raw event data into a format suitable for RePlay models. This includes mapping columns and adding a 'relevance' column with a default value of one. ```Python preparator = DataPreparator() ``` ```Python log = preparator.transform(data=data, columns_mapping={ "user_id": "user_id", "item_id": "product_id", "timestamp": "event_time" }) ``` -------------------------------- ### Display Pandas Results Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Displays the results obtained from the `PandasPredictionCallback`. ```Python pandas_res ``` -------------------------------- ### Display Spark Results Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb A simple command to display the results obtained from the `SparkPredictionCallback`. ```Python spark_res.show() ``` -------------------------------- ### ULinUCB Recommender Initialization Source: https://github.com/sb-ai-lab/replay/blob/main/docs/pages/modules/models.rst Initializes the ULInUCB Recommender, an experimental model based on the Upper Confidence Bound (UCB) algorithm, likely for personalized recommendations. ```Python class ULinUCB: def __init__(self): pass ``` -------------------------------- ### Display Item Features Head Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Displays the first few rows of the item features DataFrame. ```Python item_features.head() ``` -------------------------------- ### Display User Features Head Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Displays the first few rows of the user features DataFrame. ```Python user_features.head() ``` -------------------------------- ### Import Core RePlay and Spark Components Source: https://github.com/sb-ai-lab/replay/blob/main/examples/02_models_comparison.ipynb Imports essential components from the RePlay library, including data handling, metrics, various recommender models (ALSWrap, ItemKNN, SLIM, PopRec, RandomRec, UCB, Wilson, Word2VecRec), and utility functions for Spark integration and session management. Also imports logging and time modules. ```Python import logging import time from pyspark.sql import functions as sf from replay.data import Dataset, FeatureHint, FeatureInfo, FeatureSchema, FeatureType from replay.data.dataset_utils import DatasetLabelEncoder from replay.metrics import Coverage, HitRate, MRR, MAP, NDCG, Surprisal, Experiment, OfflineMetrics from replay.models import ( ALSWrap, ItemKNN, SLIM, PopRec, RandomRec, UCB, Wilson, Word2VecRec, ) from replay.utils.session_handler import State from replay.splitters import TimeSplitter from replay.utils.spark_utils import convert2spark, get_log_info from rs_datasets import MovieLens ``` -------------------------------- ### Train SLIM Model Source: https://github.com/sb-ai-lab/replay/blob/main/examples/01_replay_basics.ipynb Trains the initialized SLIM model using the prepared training dataset. The `%%time` magic command is used to measure the execution time. ```Python %%time slim.fit(train_dataset) ``` -------------------------------- ### Wrapper for LightFM (Experimental, Python CPU) Source: https://github.com/sb-ai-lab/replay/blob/main/docs/pages/modules/models.rst An experimental wrapper for the LightFM Python library, enabling its use with CPU. LightFM implements hybrid recommendation models. ```Python # Wrapper for LightFM library # Example placeholder: # from replay.experimental.models import LightFMWrapper # model = LightFMWrapper() # model.fit(data) # recommendations = model.predict(data) ``` -------------------------------- ### TorchPredictionCallback Source: https://github.com/sb-ai-lab/replay/blob/main/docs/pages/modules/models.rst A callback to get recommendations in PyTorch tensors format. It handles the inference process and result retrieval. ```Python from replay.models.nn.sequential.callbacks.TorchPredictionCallback import TorchPredictionCallback # Example usage (assuming model and data are available) # callback = TorchPredictionCallback() # model.predict(data, callbacks=[callback]) # results = callback.get_result() ``` -------------------------------- ### PandasPredictionCallback Source: https://github.com/sb-ai-lab/replay/blob/main/docs/pages/modules/models.rst A callback to get recommendations in Pandas DataFrame format. It handles the inference process and result retrieval. ```Python from replay.models.nn.sequential.callbacks.PandasPredictionCallback import PandasPredictionCallback # Example usage (assuming model and data are available) # callback = PandasPredictionCallback() # model.predict(data, callbacks=[callback]) # results = callback.get_result() ``` -------------------------------- ### Import RePlay and Dependencies Source: https://github.com/sb-ai-lab/replay/blob/main/examples/14_hierarchical_recommender.ipynb Imports necessary libraries and modules from RePlay, PySpark, and Scikit-learn for building and evaluating recommender systems. This includes data loading, preprocessing, models, metrics, and utility functions. ```python import logging import time from pyspark.sql import functions as sf, types as st from pyspark.sql.types import IntegerType from rs_datasets import MovieLens from sklearn.cluster import KMeans from replay.experimental.models import ULinUCB, HierarchicalRecommender from replay.experimental.models.base_rec import HybridRecommender from replay.experimental.preprocessing.data_preparator import DataPreparator, Indexer from replay.metrics import Experiment from replay.metrics import Coverage, HitRate, MRR, MAP, NDCG, Surprisal from replay.models import PopRec, RandomRec, UCB, Wilson from replay.utils.session_handler import State from replay.splitters import TimeSplitter from replay.utils.spark_utils import get_log_info ``` -------------------------------- ### SparkPredictionCallback Source: https://github.com/sb-ai-lab/replay/blob/main/docs/pages/modules/models.rst A callback to get recommendations in PySpark DataFrame format. It handles the inference process and result retrieval. ```Python from replay.models.nn.sequential.callbacks.SparkPredictionCallback import SparkPredictionCallback # Example usage (assuming model and spark session are available) # callback = SparkPredictionCallback() # model.predict(data, callbacks=[callback]) # results = callback.get_result() ``` -------------------------------- ### Initialize Data Preparator Source: https://github.com/sb-ai-lab/replay/blob/main/examples/14_hierarchical_recommender.ipynb Initializes the DataPreparator class from RePlay, which will be used to preprocess the raw dataset. ```python preparator = DataPreparator() ``` -------------------------------- ### Display Inverse Transformed Recommendations Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Shows the results after applying the inverse transformation to the PySpark recommendations. ```Python recommendations.show() ``` -------------------------------- ### Import Bert4RecCompiled Model Source: https://github.com/sb-ai-lab/replay/blob/main/examples/10_bert4rec_example.ipynb Imports the Bert4RecCompiled class from the replay.models.nn.sequential.compiled module, which is necessary for optimized inference. ```Python from replay.models.nn.sequential.compiled import Bert4RecCompiled ``` -------------------------------- ### Load MovieLens Dataset Source: https://github.com/sb-ai-lab/replay/blob/main/examples/12_neural_ts_exp.ipynb Loads the MovieLens 1m dataset using the 'rs-datasets' library and displays its basic information. ```python data = MovieLens("1m") data.info() ``` -------------------------------- ### Initialize TwoStageSplitter Source: https://github.com/sb-ai-lab/replay/blob/main/examples/01_replay_basics.ipynb Initializes a TwoStageSplitter for data splitting in recommender systems. Configures parameters for user and item columns, cold-start handling, split sizes, and shuffling. ```Python splitter = TwoStageSplitter( query_column="user_id", item_column="item_id", first_divide_column="user_id", second_divide_column="item_id", drop_cold_items=True, drop_cold_users=True, second_divide_size=K, first_divide_size=500, seed=SEED, shuffle=True, ) ``` -------------------------------- ### Set Spark Log Level Source: https://github.com/sb-ai-lab/replay/blob/main/examples/08_recommending_for_categories.ipynb Sets the logging level for the Spark context to 'ERROR' to reduce verbosity during execution. ```python spark.sparkContext.setLogLevel('ERROR') ``` -------------------------------- ### Initialize Word2Vec Model Source: https://github.com/sb-ai-lab/replay/blob/main/examples/06_item2item_recommendations.ipynb Initializes an instance of the Word2Vec recommendation model. ```Python model_w2v = Word2VecRec() ``` -------------------------------- ### Get Log Information Before and After Rating Filtering Source: https://github.com/sb-ai-lab/replay/blob/main/examples/07_filters.ipynb Prints the user and item information for the log before and after applying the `LowRatingFilter`. ```python print("Before filtering\n", get_log_info(log_spark, user_col="user_id", item_col="item_id")) print("After filtering\n", get_log_info(log_filter, user_col="user_id", item_col="item_id")) ``` -------------------------------- ### Lock Dependencies with Poetry Source: https://github.com/sb-ai-lab/replay/blob/main/CONTRIBUTING.md Updates the poetry.lock file to reflect changes in pyproject.toml, ensuring consistent dependency versions across installations. ```bash ./poetry_wrapper.sh lock ``` -------------------------------- ### Show Sample Transformed Log Data Source: https://github.com/sb-ai-lab/replay/blob/main/examples/14_hierarchical_recommender.ipynb Displays the first two rows of the transformed log data to verify the preprocessing steps. ```python log.show(2) ``` -------------------------------- ### Create Dataset for Word2Vec Source: https://github.com/sb-ai-lab/replay/blob/main/examples/06_item2item_recommendations.ipynb Creates a Dataset object for the Word2Vec model using the defined feature schema and the training interactions including timestamps. ```Python train_dataset_w2 = Dataset( feature_schema=feature_schema, interactions=train_w2v, ) ``` -------------------------------- ### Display Interaction Data Head Source: https://github.com/sb-ai-lab/replay/blob/main/examples/09_sasrec_example.ipynb Displays the first few rows of the interactions DataFrame to inspect the loaded data. ```Python interactions.head() ``` -------------------------------- ### Check if Train Data is Cached Source: https://github.com/sb-ai-lab/replay/blob/main/examples/08_recommending_for_categories.ipynb Checks and returns a boolean indicating whether the training DataFrame is currently cached in memory. ```python train.is_cached ``` -------------------------------- ### Define Experiment Parameters Source: https://github.com/sb-ai-lab/replay/blob/main/examples/13_personalized_bandit_comparison.ipynb Sets up key parameters for the experiment, including the number of recommendations (K), a list of K values for metrics, budget parameters, and a random seed for reproducibility. ```Python K = 10 K_list_metrics = [1, 5, 10] BUDGET = 5 BUDGET_NN = 2 SEED = 12345 ``` -------------------------------- ### Create Training Dataset Source: https://github.com/sb-ai-lab/replay/blob/main/examples/features_for_sequential_models.ipynb Instantiates a Dataset object for the training stage. It utilizes the prepared feature schema and raw training events, along with user and item features, to configure the training dataset. ```Python train_dataset = Dataset( feature_schema=prepare_feature_schema(is_ground_truth=False), interactions=raw_train_events, query_features=user_features, item_features=item_features, check_consistency=True, categorical_encoded=False, ) ``` -------------------------------- ### Get Log Information Before and After Item Filtering Source: https://github.com/sb-ai-lab/replay/blob/main/examples/07_filters.ipynb Prints the user and item information for the log before and after applying the `MinCountFilter` for items. ```python print("Before filtering\n", get_log_info(log_spark, user_col="user_id", item_col="item_id")) print("After filtering\n", get_log_info(log_filter, user_col="user_id", item_col="item_id")) ``` -------------------------------- ### Create Custom SparkSession and Set RePlay State Source: https://github.com/sb-ai-lab/replay/blob/main/examples/01_replay_basics.ipynb Stops the current Spark session, creates a new SparkSession with specific configurations (driver memory, shuffle partitions, bind address, host), sets the master to local, enables Hive support, and then initializes the RePlay State with this custom session. Finally, it prints the shuffle partitions. ```python spark.stop() session = ( SparkSession.builder.config("spark.driver.memory", "8g") .config("spark.sql.shuffle.partitions", "50") .config("spark.driver.bindAddress", "127.0.0.1") .config("spark.driver.host", "localhost") .master("local[*]") .enableHiveSupport() .getOrCreate() ) spark = State(session).session print_config_param(spark, "spark.sql.shuffle.partitions") ``` -------------------------------- ### Load MovieLens Dataset Source: https://github.com/sb-ai-lab/replay/blob/main/examples/02_models_comparison.ipynb Loads the MovieLens 1m dataset using the rs_datasets library and displays its information. ```Python data = MovieLens("1m") data.info() ``` -------------------------------- ### Get Log Information Before and After User Filtering Source: https://github.com/sb-ai-lab/replay/blob/main/examples/07_filters.ipynb Prints the user and item information for the log before and after applying the `MinCountFilter` for users. ```python print("Before filtering\n", get_log_info(log_spark, user_col="user_id", item_col="item_id")) print("After filtering\n", get_log_info(log_filter, user_col="user_id", item_col="item_id")) ``` -------------------------------- ### Fit ItemKNN Model Source: https://github.com/sb-ai-lab/replay/blob/main/examples/06_item2item_recommendations.ipynb Trains the ItemKNN model using the prepared training dataset. ```Python model_knn.fit(train_dataset) ```