{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# default_exp models.bases.common"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install -q pytorch-lightning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "from typing import Any, Iterable, List, Optional, Tuple, Union, Callable\n",
    "\n",
    "import torch\n",
    "from torch import nn\n",
    "\n",
    "from pytorch_lightning import LightningModule\n",
    "\n",
    "from recohut.evaluation.metrics import get_eval_metrics\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "class PointModel(LightningModule):\n",
    "    def __init__(self, n_neg=4, k=10, loss_type='CL', optim_type='adamw', lr=0.005):\n",
    "        \"\"\"\n",
    "        Args:\n",
    "            n_neg: number of negative samples\n",
    "            k: top-k recommendations\n",
    "            loss_type: CL/SL\n",
    "            optim_type: adam/adamw/sgd\n",
    "            lr: learning rate\n",
    "        \"\"\"\n",
    "        super().__init__()\n",
    "        self.n_neg = n_neg\n",
    "        self.k = k\n",
    "        self.loss_type = loss_type\n",
    "        self.optim_type = optim_type\n",
    "        self.lr = lr\n",
    "\n",
    "    def forward(self, users, items):\n",
    "        raise NotImplementedError\n",
    "\n",
    "    def training_step(self, batch, batch_idx):\n",
    "        pos, score = batch\n",
    "        users, pos_items = pos[:, 0], pos[:, 1]\n",
    "\n",
    "        # negative sampling\n",
    "        neg_items = torch.multinomial(score, self.n_neg)\n",
    "        items = torch.cat((pos_items.view(-1, 1), neg_items), dim=1)\n",
    "\n",
    "        labels = torch.zeros(items.shape)\n",
    "        labels[:, 0] += 1\n",
    "        users = users.view(-1, 1).repeat(1, items.shape[1])\n",
    "\n",
    "        users = users.view(-1, 1).squeeze()\n",
    "        items = items.view(-1, 1).squeeze()\n",
    "        labels = labels.view(-1, 1).squeeze()\n",
    "\n",
    "        logits = self(users, items)\n",
    "        loss = self.loss_fn(logits, labels)\n",
    "\n",
    "        return {\n",
    "            \"loss\": loss,\n",
    "            \"logits\": logits.detach(),\n",
    "        }\n",
    "\n",
    "    def training_epoch_end(self, outputs):\n",
    "        # This function recevies as parameters the output from \"training_step()\"\n",
    "        # Outputs is a list which contains a dictionary like:\n",
    "        # [{'pred':x,'target':x,'loss':x}, {'pred':x,'target':x,'loss':x}, ...]\n",
    "        pass\n",
    "\n",
    "    def validation_step(self, batch, batch_idx):\n",
    "        pos, items, labels = batch\n",
    "        n_items = items.shape[1]\n",
    "        users = pos[:, 0].view(-1, 1).repeat(1, n_items)\n",
    "\n",
    "        users = users.view(-1, 1).squeeze()\n",
    "        items = items.view(-1, 1).squeeze()\n",
    "        labels = labels.view(-1, 1).squeeze()\n",
    "\n",
    "        logits = self(users, items)\n",
    "        loss = self.loss_fn(logits, labels)\n",
    "\n",
    "        items = items.view(-1, n_items)\n",
    "        logits = logits.view(-1, n_items)\n",
    "        item_true = pos[:, 1].view(-1, 1)\n",
    "        item_scores = [dict(zip(item.tolist(), score.tolist())) for item, score in zip(items, logits)]\n",
    "        ncdg, apak, hr = get_eval_metrics(item_scores, item_true, self.k)\n",
    "        metrics = {\n",
    "            'loss': loss.item(),\n",
    "            'ncdg': ncdg,\n",
    "            'apak': apak,\n",
    "            'hr': hr,\n",
    "        }\n",
    "        self.log(\"Val Metrics\", metrics, prog_bar=True)\n",
    "\n",
    "        return {\n",
    "            \"loss\": loss.item(),\n",
    "            \"logits\": logits,\n",
    "        }\n",
    "\n",
    "    def validation_epoch_end(self, outputs):\n",
    "        pass\n",
    "\n",
    "    def test_step(self, batch, batch_idx):\n",
    "        pos, items, labels = batch\n",
    "        n_items = items.shape[1]\n",
    "        users = pos[:, 0].view(-1, 1).repeat(1, n_items)\n",
    "\n",
    "        users = users.view(-1, 1).squeeze()\n",
    "        items = items.view(-1, 1).squeeze()\n",
    "        labels = labels.view(-1, 1).squeeze()\n",
    "\n",
    "        logits = self(users, items)\n",
    "        loss = self.loss_fn(logits, labels)\n",
    "\n",
    "        items = items.view(-1, n_items)\n",
    "        logits = logits.view(-1, n_items)\n",
    "        item_true = pos[:, 1].view(-1, 1)\n",
    "        item_scores = [dict(zip(item.tolist(), score.tolist())) for item, score in zip(items, logits)]\n",
    "        ncdg, apak, hr = get_eval_metrics(item_scores, item_true, self.k)\n",
    "        metrics = {\n",
    "            'loss': loss.item(),\n",
    "            'ncdg': ncdg,\n",
    "            'apak': apak,\n",
    "            'hr': hr,\n",
    "        }\n",
    "        self.log(\"Test Metrics\", metrics, prog_bar=True)\n",
    "\n",
    "        return {\n",
    "            \"loss\": loss.item(),\n",
    "            \"logits\": logits,\n",
    "        }\n",
    "\n",
    "    def test_epoch_end(self, outputs):\n",
    "        pass\n",
    "\n",
    "    def configure_optimizers(self):\n",
    "        if self.optim_type == 'adamw':\n",
    "            return torch.optim.AdamW(self.parameters(), lr=self.lr)\n",
    "        elif self.optim_type == 'adam':\n",
    "            return torch.optim.Adam(self.parameters(), lr=self.lr)\n",
    "        elif self.optim_type == 'sgd':\n",
    "            return torch.optim.SGD(self.parameters(), lr=self.lr)\n",
    "        else:\n",
    "            raise ValueError(f'Invalid optimizer type: {self.optim_type}')\n",
    "\n",
    "        # optimizer = torch.optim.Adam(self.parameters(), lr=self.lr)\n",
    "        # scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(\n",
    "        #     optimizer, patience=10, factor=0.1\n",
    "        # )\n",
    "        # return {\n",
    "        #     \"optimizer\": optimizer,\n",
    "        #     \"lr_scheduler\": scheduler,\n",
    "        #     \"monitor\": \"valid_loss\",\n",
    "        # }\n",
    "\n",
    "    def loss_fn(self, logits, labels):\n",
    "        if self.loss_type == 'CL':\n",
    "            return nn.BCEWithLogitsLoss(reduction='sum')(logits, labels)\n",
    "        elif self.loss_type == 'SL':\n",
    "            return nn.MSELoss(reduction='sum')(logits, labels)\n",
    "        else:\n",
    "            raise ValueError(f'Invalid loss type: {self.loss_type}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Author: Sparsh A.\n",
      "\n",
      "Last updated: 2022-01-10 08:47:12\n",
      "\n",
      "recohut          : 0.0.10\n",
      "pytorch_lightning: 1.5.8\n",
      "\n",
      "Compiler    : GCC 7.5.0\n",
      "OS          : Linux\n",
      "Release     : 5.4.144+\n",
      "Machine     : x86_64\n",
      "Processor   : x86_64\n",
      "CPU cores   : 2\n",
      "Architecture: 64bit\n",
      "\n",
      "torch  : 1.10.0+cu111\n",
      "IPython: 5.5.0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#hide\n",
    "%reload_ext watermark\n",
    "%watermark -a \"Sparsh A.\" -m -iv -u -t -d -p recohut,pytorch_lightning"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
