{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/RecoHut-Projects/recohut/blob/US632593/tutorials/quick_tutorial.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install pytorch-lightning\n",
    "%cd /content\n",
    "!rm -rf recohut\n",
    "!git clone --branch US632593 https://github.com/RecoHut-Projects/recohut.git\n",
    "%cd recohut\n",
    "!pip install -U .\n",
    "!apt-get -qq install tree\n",
    "!pip install -q watermark"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Downloading http://files.grouplens.org/datasets/movielens/ml-1m.zip\n",
      "Extracting /content/data/raw/ml-1m.zip\n",
      "Processing...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Turning into implicit ratings\n",
      "Filtering triplets\n",
      "Densifying index\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Done!\n",
      "GPU available: False, used: False\n",
      "TPU available: False, using: 0 TPU cores\n",
      "IPU available: False, using: 0 IPUs\n",
      "Missing logger folder: /content/recohut/default\n",
      "\n",
      "  | Name               | Type      | Params\n",
      "-------------------------------------------------\n",
      "0 | user_embedding     | Embedding | 120 K \n",
      "1 | item_embedding     | Embedding | 62.5 K\n",
      "2 | user_embedding_gmf | Embedding | 120 K \n",
      "3 | item_embedding_gmf | Embedding | 62.5 K\n",
      "4 | gmf                | Linear    | 210   \n",
      "5 | fc1                | Linear    | 820   \n",
      "6 | fc2                | Linear    | 420   \n",
      "7 | fc3                | Linear    | 210   \n",
      "8 | fc_final           | Linear    | 21    \n",
      "9 | dropout            | Dropout   | 0     \n",
      "-------------------------------------------------\n",
      "368 K     Trainable params\n",
      "0         Non-trainable params\n",
      "368 K     Total params\n",
      "1.472     Total estimated model params size (MB)\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5701c24fd50c49fca433b3bc4e9ca95c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Training: 0it [00:00, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5e27582d21f542b8b3bb9778ec24b3da",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Validating: 0it [00:00, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b97236d25b7341f1a288e801da63c100",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Testing: 0it [00:00, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------------------------------------\n",
      "DATALOADER:0 TEST RESULTS\n",
      "{'Test Metrics': {'apak': tensor(0.0456),\n",
      "                  'hr': tensor(0.1448),\n",
      "                  'loss': tensor(0.2072),\n",
      "                  'ncdg': tensor(0.0684)}}\n",
      "--------------------------------------------------------------------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'Test Metrics': {'apak': tensor(0.0456),\n",
       "   'hr': tensor(0.1448),\n",
       "   'loss': tensor(0.2072),\n",
       "   'ncdg': tensor(0.0684)}}]"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# prepare dataset\n",
    "from recohut.datasets.movielens import ML1mDataModule\n",
    "\n",
    "class Args:\n",
    "    def __init__(self):\n",
    "        self.data_dir = '/content/data'\n",
    "        self.min_rating = 4\n",
    "        self.num_negative_samples = 99\n",
    "        self.min_uc = 5\n",
    "        self.min_sc = 5\n",
    "        self.val_p = 0.2\n",
    "        self.test_p = 0.2\n",
    "        self.num_workers = 2\n",
    "        self.normalize = False\n",
    "        self.batch_size = 32\n",
    "        self.seed = 42\n",
    "        self.shuffle = True\n",
    "        self.pin_memory = True\n",
    "        self.drop_last = False\n",
    "        self.split_type = 'stratified'\n",
    "\n",
    "args = Args()\n",
    "\n",
    "ds = ML1mDataModule(**args.__dict__)\n",
    "\n",
    "ds.prepare_data()\n",
    "\n",
    "# build model\n",
    "from recohut.models.nmf import NMF\n",
    "\n",
    "model = NMF(n_items=ds.data.num_items, n_users=ds.data.num_users, embedding_dim=20)\n",
    "\n",
    "# train\n",
    "from recohut.trainers.pl_trainer import pl_trainer\n",
    "\n",
    "pl_trainer(model, ds, max_epochs=5)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
