Back to Recommenders

Sequential Recommender Quick Start

examples/00_quick_start/sequential_recsys_amazondataset.ipynb

1.2.116.9 KB
Original Source

<i>Copyright (c) Recommenders contributors.</i>

<i>Licensed under the MIT License.</i>

Sequential Recommender Quick Start

Example: SLi_Rec : Adaptive User Modeling with Long and Short-Term Preferences for Personailzed Recommendation

Unlike a general recommender such as Matrix Factorization or xDeepFM (in the repo) which doesn't consider the order of the user's activities, sequential recommender systems take the sequence of the user behaviors as context and the goal is to predict the items that the user will interact in a short time (in an extreme case, the item that the user will interact next).

This notebook aims to give you a quick example of how to train a sequential model based on a public Amazon dataset. Currently, we can support NextItNet [4], GRU [2], Caser [3], A2SVD [1], SLi_Rec [1], and SUM [5]. Without loss of generality, this notebook takes SLi_Rec model for example. SLi_Rec [1] is a deep learning-based model aims at capturing both long and short-term user preferences for precise recommender systems. To summarize, SLi_Rec has the following key properties:

  • It adopts the attentive "Asymmetric-SVD" paradigm for long-term modeling;
  • It takes both time irregularity and semantic irregularity into consideration by modifying the gating logic in LSTM.
  • It uses an attention mechanism to dynamic fuse the long-term component and short-term component.

In this notebook, we test SLi_Rec on a subset of the public dataset: Amazon_reviews and Amazon_metadata

This notebook is tested under TF 2.6.

0. Global Settings and Imports

python
import os
import sys
import tensorflow.compat.v1 as tf
tf.get_logger().setLevel('ERROR') # only show error messages

from recommenders.utils.timer import Timer
from recommenders.utils.constants import SEED
from recommenders.models.deeprec.deeprec_utils import (
    prepare_hparams
)
from recommenders.datasets.amazon_reviews import download_and_extract, data_preprocessing
from recommenders.models.deeprec.models.sequential.sli_rec import SLI_RECModel as SeqModel
####  to use the other model, use one of the following lines:
# from recommenders.models.deeprec.models.sequential.asvd import A2SVDModel as SeqModel
# from recommenders.models.deeprec.models.sequential.caser import CaserModel as SeqModel
# from recommenders.models.deeprec.models.sequential.gru import GRUModel as SeqModel
# from recommenders.models.deeprec.models.sequential.sum import SUMModel as SeqModel
#from recommenders.models.deeprec.models.sequential.nextitnet import NextItNetModel
from recommenders.models.deeprec.io.sequential_iterator import SequentialIterator
#from recommenders.models.deeprec.io.nextitnet_iterator import NextItNetIterator
from recommenders.utils.notebook_utils import store_metadata

print(f"System version: {sys.version}")
print(f"Tensorflow version: {tf.__version__}")


Parameters

python
EPOCHS = 10
BATCH_SIZE = 400
RANDOM_SEED = SEED  # Set None for non-deterministic result

data_path = os.path.join("..", "..", "tests", "resources", "deeprec", "slirec")

##  ATTENTION: change to the corresponding config file, e.g., caser.yaml for CaserModel, sum.yaml for SUMModel
yaml_file = '../../recommenders/models/deeprec/config/sli_rec.yaml'  

1. Input data format

The input data contains 8 columns, i.e., <label> <user_id> <item_id> <category_id> <timestamp> <history_item_ids> <history_cateory_ids> <hitory_timestamp> columns are seperated by "\t". item_id and category_id denote the target item and category, which means that for this instance, we want to guess whether user user_id will interact with item_id at timestamp. <history_*> columns record the user behavior list up to <timestamp>, elements are separated by commas. <label> is a binary value with 1 for positive instances and 0 for negative instances. One example for an instance is:

1 A1QQ86H5M2LVW2 B0059XTU1S Movies 1377561600 B002ZG97WE,B004IK30PA,B000BNX3AU,B0017ANB08,B005LAIHW2 Movies,Movies,Movies,Movies,Movies 1304294400,1304812800,1315785600,1316304000,1356998400

In data preprocessing stage, we have a script to generate some ID mapping dictionaries, so user_id, item_id and category_id will be mapped into interager index starting from 1. And you need to tell the input iterator where is the ID mapping files are. (For example, in the next section, we have some mapping files like user_vocab, item_vocab, and cate_vocab). The data preprocessing script is at recommenders/dataset/amazon_reviews.py, you need to call the _create_vocab(train_file, user_vocab, item_vocab, cate_vocab) function. Note that ID vocabulary only creates from the train_file, so the new IDs in valid_file or test_file will be regarded as unknown IDs and assigned with a defualt 0 index.

Only the SLi_Rec model is time-aware. For the other models, you can just pad some meaningless timestamp in the data files to fill up the format, the models will ignore these columns.

We use Softmax to the loss function. In training and evalution stage, we group 1 positive instance with num_ngs negative instances. Pair-wise ranking can be regarded as a special case of softmax ranking, where num_ngs is set to 1.

More specifically, for training and evalation, you need to organize the data file such that each one positive instance is followed by num_ngs negative instances. Our program will take 1+num_ngs lines as a unit for Softmax calculation. num_ngs is a parameter you need to pass to the prepare_hparams, fit and run_eval function. train_num_ngs in prepare_hparams denotes the number of negative instances for training, where a recommended number is 4. valid_num_ngs and num_ngs in fit and run_eval denote the number in evalution. In evaluation, the model calculates metrics among the 1+num_ngs instances. For the predict function, since we only need to calcuate a score for each individual instance, there is no need for num_ngs setting. More details and examples will be provided in the following sections.

For training stage, if you don't want to prepare negative instances, you can just provide positive instances and set the parameter need_sample=True, train_num_ngs=train_num_ngs for function prepare_hparams, our model will dynamicly sample train_num_ngs instances as negative samples in each mini batch.

Amazon dataset

Now let's start with a public dataset containing product reviews and metadata from Amazon, which is widely used as a benchmark dataset in recommemdation systems field.

python

# for test
train_file = os.path.join(data_path, r'train_data')
valid_file = os.path.join(data_path, r'valid_data')
test_file = os.path.join(data_path, r'test_data')
user_vocab = os.path.join(data_path, r'user_vocab.pkl')
item_vocab = os.path.join(data_path, r'item_vocab.pkl')
cate_vocab = os.path.join(data_path, r'category_vocab.pkl')
output_file = os.path.join(data_path, r'output.txt')

reviews_name = 'reviews_Movies_and_TV_5.json'
meta_name = 'meta_Movies_and_TV.json'
reviews_file = os.path.join(data_path, reviews_name)
meta_file = os.path.join(data_path, meta_name)
train_num_ngs = 4 # number of negative instances with a positive instance for training
valid_num_ngs = 4 # number of negative instances with a positive instance for validation
test_num_ngs = 9 # number of negative instances with a positive instance for testing
sample_rate = 0.01 # sample a small item set for training and testing here for fast example

input_files = [reviews_file, meta_file, train_file, valid_file, test_file, user_vocab, item_vocab, cate_vocab]

if not os.path.exists(train_file):
    download_and_extract(reviews_name, reviews_file)
    download_and_extract(meta_name, meta_file)
    data_preprocessing(*input_files, sample_rate=sample_rate, valid_num_ngs=valid_num_ngs, test_num_ngs=test_num_ngs)
    #### uncomment this for the NextItNet model, because it does not need to unfold the user history
    # data_preprocessing(*input_files, sample_rate=sample_rate, valid_num_ngs=valid_num_ngs, test_num_ngs=test_num_ngs, is_history_expanding=False)

1.1 Prepare hyper-parameters

prepare_hparams() will create a full set of hyper-parameters for model training, such as learning rate, feature number, and dropout ratio. We can put those parameters in a yaml file (a complete list of parameters can be found under our config folder) , or pass parameters as the function's parameters (which will overwrite yaml settings).

Parameters hints:

need_sample controls whether to perform dynamic negative sampling in mini-batch. train_num_ngs indicates how many negative instances followed by one positive instances.

Examples:

(1) need_sample=True and train_num_ngs=4: There are only positive instances in your training file. Our model will dynamically sample 4 negative instances for each positive instances in mini-batch. Note that if need_sample is set to True, train_num_ngs should be greater than zero.

(2) need_sample=False and train_num_ngs=4: In your training file, each one positive line is followed by 4 negative lines. Note that if need_sample is set to False, you must provide a traiing file with negative instances, and train_num_ngs should match the number of negative number in your training file.

python
### NOTE:  
### remember to use `_create_vocab(train_file, user_vocab, item_vocab, cate_vocab)` to generate the user_vocab, item_vocab and cate_vocab files, if you are using your own dataset rather than using our demo Amazon dataset.
hparams = prepare_hparams(yaml_file, 
                          embed_l2=0., 
                          layer_l2=0., 
                          learning_rate=0.001,  # set to 0.01 if batch normalization is disable
                          epochs=EPOCHS,
                          batch_size=BATCH_SIZE,
                          show_step=20,
                          MODEL_DIR=os.path.join(data_path, "model/"),
                          SUMMARIES_DIR=os.path.join(data_path, "summary/"),
                          user_vocab=user_vocab,
                          item_vocab=item_vocab,
                          cate_vocab=cate_vocab,
                          need_sample=True,
                          train_num_ngs=train_num_ngs, # provides the number of negative instances for each positive instance for loss computation.
            )

1.2 Create data loader

Designate a data iterator for the model. All our sequential models use SequentialIterator. data format is introduced aboved.

Validation and testing data are files after negative sampling offline with the number of <num_ngs> and <test_num_ngs>.

python
input_creator = SequentialIterator
#### uncomment this for the NextItNet model, because it needs a special data iterator for training
#input_creator = NextItNetIterator

2. Create model

When both hyper-parameters and data iterator are ready, we can create a model:

python
model = SeqModel(hparams, input_creator, seed=RANDOM_SEED)

## sometimes we don't want to train a model from scratch
## then we can load a pre-trained model like this: 
#model.load_model(r'your_model_path')

Now let's see what is the model's performance at this point (without starting training):

python
# test_num_ngs is the number of negative lines after each positive line in your test_file
print(model.run_eval(test_file, num_ngs=test_num_ngs)) 

AUC=0.5 is a state of random guess. We can see that before training, the model behaves like random guessing.

2.1 Train model

Next we want to train the model on a training set, and check the performance on a validation dataset. Training the model is as simple as a function call:

python
with Timer() as train_time:
    model = model.fit(train_file, valid_file, valid_num_ngs=valid_num_ngs) 

# valid_num_ngs is the number of negative lines after each positive line in your valid_file 
# we will evaluate the performance of model on valid_file every epoch
print('Time cost for training is {0:.2f} mins'.format(train_time.interval/60.0))

2.2 Evaluate model

Again, let's see what is the model's performance now (after training):

python
res_syn = model.run_eval(test_file, num_ngs=test_num_ngs)
print(res_syn)

python
# Record results for tests - ignore this cell
store_metadata("auc", res_syn["auc"])
store_metadata("logloss", res_syn["logloss"])
store_metadata("mean_mrr", res_syn["mean_mrr"])
store_metadata("ndcg@2", res_syn["ndcg@2"])
store_metadata("ndcg@4", res_syn["ndcg@4"])
store_metadata("ndcg@6", res_syn["ndcg@6"])
store_metadata("group_auc", res_syn["group_auc"])

If we want to get the full prediction scores rather than evaluation metrics, we can do this:

python
model = model.predict(test_file, output_file)
python
# The data was downloaded in tmpdir folder. You can delete them manually if you do not need them any more.

2.3 Running models with large dataset

Here are performances using the whole amazon dataset among popular sequential models with 1,697,533 positive instances.

Settings for reproducing the results:

learning_rate=0.001, dropout=0.3, item_embedding_dim=32, cate_embedding_dim=8, l2_norm=0, batch_size=400, train_num_ngs=4, valid_num_ngs=4, test_num_ngs=49

We compare the running time with CPU only and with GPU on the larger dataset. It appears that GPU can significantly accelerate the training. Hardware specification for running the large dataset:

GPU: Tesla P100-PCIE-16GB

CPU: 6 cores Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz

ModelsAUCg-AUCNDCG@2NDCG@10seconds per epoch on GPUseconds per epoch on CPUconfig
A2SVD0.82510.81780.29220.4264249.5440.0N/A
GRU0.84110.83320.32130.4547439.04285.0max_seq_length=50, hidden_size=40
Caser0.82440.81710.2830.4194314.35369.9T=1, n_v=128, n_h=128, L=3, min_seq_length=5
SLi_Rec0.86310.85190.34910.4842549.65014.0attention_size=40, max_seq_length=50, hidden_size=40
NextItNet*0.67930.67690.06020.1733112.0214.5min_seq_length=3, dilations=[1,2,4,1,2,4], kernel_size=3
SUM0.84810.84060.33940.47741005.09427.0hidden_size=40, slots=4, dropout=0

Note 1: The five models are grid searched with a coarse granularity and the results are for reference only.

Note 2: NextItNet model requires a dataset with strong sequence property, but the Amazon dataset used in this notebook does not meet that requirement, so NextItNet Model may not performance good. If you wish to use other datasets with strong sequence property, NextItNet is recommended.

Note 3: Time cost of NextItNet Model is significantly shorter than other models because it doesn't need a history expanding of training data.

3. Loading Trained Models

In this section, we provide a simple example to illustrate how we can use the trained model to serve for production demand.

Suppose we are in a new session. First let's load a previous trained model:

python
model_best_trained = SeqModel(hparams, input_creator, seed=RANDOM_SEED)
path_best_trained = os.path.join(hparams.MODEL_DIR, "best_model")
print('loading saved model in {0}'.format(path_best_trained))
model_best_trained.load_model(path_best_trained)

Let's see if we load the model correctly. The testing metrics should be close to the numbers we have in the training stage.

python
model_best_trained.run_eval(test_file, num_ngs=test_num_ngs)

And we make predictions using this model. In the next step, we will make predictions using a serving model. Then we can check if the two result files are consistent.

python
model_best_trained.predict(test_file, output_file)

References

[1] Zeping Yu, Jianxun Lian, Ahmad Mahmoody, Gongshen Liu, Xing Xie. Adaptive User Modeling with Long and Short-Term Preferences for Personailzed Recommendation. In Proceedings of the 28th International Joint Conferences on Artificial Intelligence, IJCAI’19, Pages 4213-4219. AAAI Press, 2019.

[2] Kyunghyun Cho, Bart van Merrienboer, Caglar Gulcehre, Dzmitry Bahdanau, Fethi Bougares, Holger Schwenk, and Yoshua Bengio. Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine Translation. arXiv preprint arXiv:1406.1078. 2014.

[3] Tang, Jiaxi, and Ke Wang. Personalized top-n sequential recommendation via convolutional sequence embedding. Proceedings of the Eleventh ACM International Conference on Web Search and Data Mining. ACM, 2018.

[4] Yuan, F., Karatzoglou, A., Arapakis, I., Jose, J. M., & He, X. A Simple Convolutional Generative Network for Next Item Recommendation. WSDM, 2019.

[5] Lian, J., Batal, I., Liu, Z., Soni, A., Kang, E. Y., Wang, Y., & Xie, X. Multi-Interest-Aware User Modeling for Large-Scale Sequential Recommendations. arXiv preprint arXiv:2102.09211. 2021.