{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Copyright (c) Recommenders contributors.\n", "\n", "Licensed under the MIT License." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Sequential Recommender Quick Start\n", "\n", "### Example: A2SVD : Adaptive User Modeling with Long and Short-Term Preferences for Personailzed Recommendation\n", "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).\n", "\n", "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\\], and SUM \\[5\\]. Without loss of generality, this notebook takes the [A2SVD model](https://www.microsoft.com/en-us/research/uploads/prod/2019/07/IJCAI19-ready_v1.pdf) for example.\n", "A2SVD \\[1\\] is a deep learning-based model that captures long-term user preferences: it adopts an attentive \"Asymmetric-SVD\" paradigm, summarizing the user's whole behavior history into a long-term representation with an attention mechanism.\n", "\n", "In this notebook, we test A2SVD on a subset of the public dataset: [Amazon_reviews](http://snap.stanford.edu/data/amazon/productGraph/categoryFiles/reviews_Movies_and_TV_5.json.gz) and [Amazon_metadata](http://snap.stanford.edu/data/amazon/productGraph/categoryFiles/meta_Movies_and_TV.json.gz)\n", "\n", "This notebook is tested under TF 2.6. **Note:** SLi_Rec \\[1\\], the time-aware model from the same paper, has moved to its own PyTorch notebook: [slirec_amazon.ipynb](slirec_amazon.ipynb). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 0. Global Settings and Imports" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2026-07-21T14:56:44.734809Z", "iopub.status.busy": "2026-07-21T14:56:44.734033Z", "iopub.status.idle": "2026-07-21T14:56:52.405362Z", "shell.execute_reply": "2026-07-21T14:56:52.401172Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2026-07-21 16:56:45.348592: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n", "2026-07-21 16:56:45.448916: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", "2026-07-21 16:56:45.448993: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", "2026-07-21 16:56:45.451479: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", "2026-07-21 16:56:45.466224: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", "To enable the following instructions: AVX2 AVX512F AVX512_VNNI AVX512_BF16 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2026-07-21 16:56:46.896794: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "System version: 3.11.14 (main, Jan 14 2026, 19:35:32) [Clang 21.1.4 ]\n", "Tensorflow version: 2.15.1\n" ] } ], "source": [ "import os\n", "import sys\n", "import tensorflow.compat.v1 as tf\n", "tf.get_logger().setLevel('ERROR') # only show error messages\n", "\n", "from recommenders.utils.timer import Timer\n", "from recommenders.utils.constants import SEED\n", "from recommenders.models.deeprec.deeprec_utils import (\n", " prepare_hparams\n", ")\n", "from recommenders.datasets.amazon_reviews import download_and_extract, data_preprocessing\n", "from recommenders.models.deeprec.models.sequential.asvd import A2SVDModel as SeqModel\n", "#### SLi-Rec now has its own PyTorch notebook (slirec_amazon.ipynb); to use another model, use one of these:\n", "# from recommenders.models.deeprec.models.sequential.caser import CaserModel as SeqModel\n", "# from recommenders.models.deeprec.models.sequential.gru import GRUModel as SeqModel\n", "# from recommenders.models.deeprec.models.sequential.sum import SUMModel as SeqModel\n", "#from recommenders.models.deeprec.models.sequential.nextitnet import NextItNetModel\n", "from recommenders.models.deeprec.io.sequential_iterator import SequentialIterator\n", "#from recommenders.models.deeprec.io.nextitnet_iterator import NextItNetIterator\n", "from recommenders.utils.notebook_utils import store_metadata\n", "\n", "print(f\"System version: {sys.version}\")\n", "print(f\"Tensorflow version: {tf.__version__}\")\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Parameters" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2026-07-21T14:56:52.410074Z", "iopub.status.busy": "2026-07-21T14:56:52.409402Z", "iopub.status.idle": "2026-07-21T14:56:52.418906Z", "shell.execute_reply": "2026-07-21T14:56:52.415532Z" }, "tags": [ "parameters" ] }, "outputs": [], "source": [ "EPOCHS = 10\n", "BATCH_SIZE = 400\n", "RANDOM_SEED = SEED # Set None for non-deterministic result\n", "\n", "data_path = os.path.join(\"..\", \"..\", \"tests\", \"resources\", \"deeprec\", \"slirec\")\n", "\n", "## ATTENTION: change to the corresponding config file, e.g., caser.yaml for CaserModel, sum.yaml for SUMModel\n", "yaml_file = '../../recommenders/models/deeprec/config/asvd.yaml' " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Input data format\n", "The input data contains 8 columns, i.e., `