{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Copyright (c) Recommenders contributors.\n", "\n", "Licensed under the MIT License." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Neural Collaborative Filtering on MovieLens dataset.\n", "\n", "Neural Collaborative Filtering (NCF) is a well known recommendation algorithm that generalizes the matrix factorization problem with multi-layer perceptron. \n", "\n", "This notebook provides an example of how to utilize and evaluate NCF implementation in the `recommenders`. We use a smaller dataset in this example to run NCF efficiently with GPU acceleration on a [Data Science Virtual Machine](https://azure.microsoft.com/en-gb/services/virtual-machines/data-science-virtual-machines/)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "System version: 3.11.15 (main, Mar 11 2026, 17:20:07) [GCC 14.3.0]\n", "Pandas version: 2.3.3\n", "PyTorch version: 2.11.0+cu130\n" ] } ], "source": [ "import sys\n", "import pandas as pd\n", "import torch\n", "\n", "from recommenders.utils.timer import Timer\n", "from recommenders.models.ncf.ncf_singlenode import NCF\n", "from recommenders.models.ncf.dataset import Dataset as NCFDataset\n", "from recommenders.datasets import movielens\n", "from recommenders.datasets.python_splitters import python_chrono_split\n", "from recommenders.evaluation.python_evaluation import (\n", " map_at_k, ndcg_at_k, precision_at_k, recall_at_k\n", ")\n", "from recommenders.utils.notebook_utils import store_metadata\n", "\n", "print(\"System version: {}\".format(sys.version))\n", "print(\"Pandas version: {}\".format(pd.__version__))\n", "print(\"PyTorch version: {}\".format(torch.__version__))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set the default parameters." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [ "parameters" ] }, "outputs": [], "source": [ "# top k items to recommend\n", "TOP_K = 10\n", "\n", "# Select MovieLens data size: 100k, 1m, 10m, or 20m\n", "MOVIELENS_DATA_SIZE = '100k'\n", "\n", "# Model parameters\n", "EPOCHS = 50\n", "BATCH_SIZE = 256\n", "\n", "SEED = 42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. Download the MovieLens dataset" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:recommenders.datasets.download_utils:Downloading http://files.grouplens.org/datasets/movielens/ml-100k.zip\n", "100%|██████████| 4.81k/4.81k [00:00<00:00, 5.10kKB/s]\n" ] } ], "source": [ "df = movielens.load_pandas_df(\n", " size=MOVIELENS_DATA_SIZE,\n", " header=[\"userID\", \"itemID\", \"rating\", \"timestamp\"]\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Split the data using the Spark chronological splitter provided in utilities" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "train, test = python_chrono_split(df, 0.75)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Filter out any users or items in the test set that do not appear in the training set." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "test = test[test[\"userID\"].isin(train[\"userID\"].unique())]\n", "test = test[test[\"itemID\"].isin(train[\"itemID\"].unique())]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write datasets to csv files." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "train_file = \"./train.csv\"\n", "test_file = \"./test.csv\"\n", "train.to_csv(train_file, index=False)\n", "test.to_csv(test_file, index=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate an NCF dataset object from the data subsets." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:recommenders.models.ncf.dataset:Indexing ./train.csv ...\n", "INFO:recommenders.models.ncf.dataset:Indexing ./test.csv ...\n", "INFO:recommenders.models.ncf.dataset:Creating full leave-one-out test file ./test_full.csv ...\n", " 0%| | 0/943 [00:00