{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Copyright (c) Recommenders contributors.\n", "\n", "Licensed under the MIT License." ] }, { "cell_type": "markdown", "metadata": {}, "source": "# SLi_Rec: Adaptive User Modeling with Long and Short-Term Preferences for Personalized Recommendation\n\nThis notebook gives a quick example of how to train and evaluate the [SLi_Rec model](https://www.microsoft.com/en-us/research/uploads/prod/2019/07/IJCAI19-ready_v1.pdf) \\[1\\].\nSLi_Rec \\[1\\] is a deep learning-based sequential recommendation model that captures both long and short-term user preferences: it takes the sequence of the user behaviors as context and predicts the items that the user will interact in a short time (in an extreme case, the item that the user will interact next). To summarize, SLi_Rec has the following key properties:\n\n* It adopts the attentive \"Asymmetric-SVD\" paradigm for long-term modeling;\n* It takes both time irregularity and semantic irregularity into consideration by modifying the gating logic in LSTM.\n* It uses an attention mechanism to dynamic fuse the long-term component and short-term component.\n\nIn this notebook, we test SLi_Rec 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)" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 0. Global Settings and Imports" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2026-07-21T14:42:59.217961Z", "iopub.status.busy": "2026-07-21T14:42:59.217407Z", "iopub.status.idle": "2026-07-21T14:43:06.729279Z", "shell.execute_reply": "2026-07-21T14:43:06.724860Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "System version: 3.11.14 (main, Jan 14 2026, 19:35:32) [Clang 21.1.4 ]\n", "PyTorch version: 2.13.0.dev20260521+cu132\n" ] } ], "source": [ "import os\n", "import sys\n", "import torch\n", "\n", "from recommenders.utils.timer import Timer\n", "from recommenders.utils.constants import SEED\n", "from recommenders.datasets.amazon_reviews import download_and_extract, data_preprocessing\n", "from recommenders.models.deeprec.models.sequential.pytorch.sli_rec import SLiRecModel as SeqModel\n", "from recommenders.utils.notebook_utils import store_metadata\n", "\n", "print(f\"System version: {sys.version}\")\n", "print(f\"PyTorch version: {torch.__version__}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Parameters" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2026-07-21T14:43:06.787079Z", "iopub.status.busy": "2026-07-21T14:43:06.786331Z", "iopub.status.idle": "2026-07-21T14:43:06.795261Z", "shell.execute_reply": "2026-07-21T14:43:06.791967Z" }, "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\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Input data format\n", "The input data contains 8 columns, i.e., `