├── .gitignore ├── LICENSE ├── Probability Matrix Factorization.ipynb ├── README.md └── matrix_factorization.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Mat Leonard 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Probability Matrix Factorization.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Implementing Probabilistic Matrix Factorization in PyTorch\n", 8 | "\n", 9 | "From this paper: https://papers.nips.cc/paper/3208-probabilistic-matrix-factorization.pdf.\n", 10 | "\n", 11 | "Data from here: https://grouplens.org/datasets/movielens/\n", 12 | "\n", 13 | "Our goal here is to find feature vectors for our users and movies that allow us to compare them. We start with a big matrix $R$ of movie ratings, with users as rows and movies as columns. Since users don't rate all movies, this is a pretty sparse matrix with a lot of missing values. \n", 14 | "\n", 15 | "What we can do is factor $R$ into two matrices $U$ and $V$ representing latent feature vectors for users and movies. With these latent features, we can compare users with each other, movies with each other, and predict ratings for movies users haven't seen.\n", 16 | "\n", 17 | "![matrix factorization](matrix_factorization.png)\n", 18 | "\n", 19 | "The big idea behind this paper is that we're going to treat the latent vectors as parameters in a Bayesian model. As a reminder, Bayes theorem:\n", 20 | "\n", 21 | "$$\n", 22 | "\\large P(\\theta \\mid D) \\propto P(D \\mid \\theta) P(\\theta)\n", 23 | "$$\n", 24 | "\n", 25 | "In our model we try to predict the rating matrix $R$ with $U$ and $V$\n", 26 | "\n", 27 | "$$ \\large \\hat{R} = U^T V$$\n", 28 | "\n", 29 | "In our model we assume the ratings are drawn from a normal distribution with mean $\\hat{R}$. What's really cool is that we can place priors on our latent features. \n", 30 | "\n", 31 | "$$\n", 32 | "\\begin{aligned}\n", 33 | "R &\\sim \\mathrm{Normal}(U^T V, \\sigma^2) \\\\\n", 34 | "U &\\sim \\mathrm{Normal}(0, \\sigma_U^2) \\\\\n", 35 | "V &\\sim \\mathrm{Normal}(0, \\sigma_V^2)\n", 36 | "\\end{aligned}\n", 37 | "$$\n", 38 | "\n", 39 | "The authors of the paper go further and build a hierachical structure for the user vectors\n", 40 | "\n", 41 | "$$\n", 42 | "\\begin{aligned}\n", 43 | "U_i &= Y_i + \\frac{\\sum_k^M I_{ik}W_k}{\\sum_k^M I_{ik}} \\\\\n", 44 | "Y &\\sim \\mathrm{Normal}(0, \\sigma_U^2) \\\\\n", 45 | "W &\\sim \\mathrm{Normal}(0, \\sigma_W^2)\n", 46 | "\\end{aligned}\n", 47 | "$$\n", 48 | "\n", 49 | "With this model, we can maximize the posterior probability with respect to $U$ and $V$, or $V$, $Y$, and $W$ for the hierarchical model. In effect this is just a linear model with fancy regularization.\n", 50 | "\n", 51 | "The authors also do things like converting the ratings to be between 0 and 1, then taking the sigmoid of $U^T V$. For empirical reasons." 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 1, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "import pandas as pd\n", 61 | "import torch" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 3, 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "ratings = pd.read_csv('ml-latest-small/ratings.csv')" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 4, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/html": [ 81 | "
\n", 82 | "\n", 95 | "\n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | " \n", 115 | " \n", 116 | " \n", 117 | " \n", 118 | " \n", 119 | " \n", 120 | " \n", 121 | " \n", 122 | " \n", 123 | " \n", 124 | " \n", 125 | " \n", 126 | " \n", 127 | " \n", 128 | " \n", 129 | " \n", 130 | " \n", 131 | " \n", 132 | " \n", 133 | " \n", 134 | " \n", 135 | " \n", 136 | " \n", 137 | " \n", 138 | " \n", 139 | " \n", 140 | " \n", 141 | " \n", 142 | " \n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | " \n", 148 | " \n", 149 | " \n", 150 | " \n", 151 | " \n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | " \n", 157 | " \n", 158 | " \n", 159 | " \n", 160 | " \n", 161 | " \n", 162 | " \n", 163 | "
userIdmovieIdratingtimestamp
count100836.000000100836.000000100836.0000001.008360e+05
mean326.12756419435.2957183.5015571.205946e+09
std182.61849135530.9871991.0425292.162610e+08
min1.0000001.0000000.5000008.281246e+08
25%177.0000001199.0000003.0000001.019124e+09
50%325.0000002991.0000003.5000001.186087e+09
75%477.0000008122.0000004.0000001.435994e+09
max610.000000193609.0000005.0000001.537799e+09
\n", 164 | "
" 165 | ], 166 | "text/plain": [ 167 | " userId movieId rating timestamp\n", 168 | "count 100836.000000 100836.000000 100836.000000 1.008360e+05\n", 169 | "mean 326.127564 19435.295718 3.501557 1.205946e+09\n", 170 | "std 182.618491 35530.987199 1.042529 2.162610e+08\n", 171 | "min 1.000000 1.000000 0.500000 8.281246e+08\n", 172 | "25% 177.000000 1199.000000 3.000000 1.019124e+09\n", 173 | "50% 325.000000 2991.000000 3.500000 1.186087e+09\n", 174 | "75% 477.000000 8122.000000 4.000000 1.435994e+09\n", 175 | "max 610.000000 193609.000000 5.000000 1.537799e+09" 176 | ] 177 | }, 178 | "execution_count": 4, 179 | "metadata": {}, 180 | "output_type": "execute_result" 181 | } 182 | ], 183 | "source": [ 184 | "ratings.describe()" 185 | ] 186 | }, 187 | { 188 | "cell_type": "code", 189 | "execution_count": 5, 190 | "metadata": {}, 191 | "outputs": [], 192 | "source": [ 193 | "rating_matrix = ratings.pivot(index='userId', columns='movieId', values='rating')\n", 194 | "n_users, n_movies = rating_matrix.shape\n", 195 | "# Scaling ratings to between 0 and 1, this helps our model by constraining predictions\n", 196 | "min_rating, max_rating = ratings['rating'].min(), ratings['rating'].max()\n", 197 | "rating_matrix = (rating_matrix - min_rating) / (max_rating - min_rating)" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 6, 203 | "metadata": {}, 204 | "outputs": [], 205 | "source": [ 206 | "# Replacing missing ratings with -1 so we can filter them out later\n", 207 | "rating_matrix[rating_matrix.isnull()] = -1\n", 208 | "rating_matrix = torch.FloatTensor(rating_matrix.values)" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 7, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": [ 219 | "tensor([[ 0.0067, -0.0105, 0.0074, 0.0099, 0.0122],\n", 220 | " [ 0.0039, -0.0055, -0.0020, -0.0106, 0.0109],\n", 221 | " [ 0.0120, -0.0112, -0.0107, 0.0054, -0.0020],\n", 222 | " ...,\n", 223 | " [-0.0019, -0.0210, 0.0027, 0.0274, -0.0096],\n", 224 | " [ 0.0152, -0.0083, 0.0015, -0.0007, -0.0005],\n", 225 | " [-0.0048, 0.0103, -0.0027, 0.0072, 0.0056]])" 226 | ] 227 | }, 228 | "execution_count": 7, 229 | "metadata": {}, 230 | "output_type": "execute_result" 231 | } 232 | ], 233 | "source": [ 234 | "# This is how we can define our feature matrices\n", 235 | "# We're going to be training these, so we'll need gradients\n", 236 | "latent_vectors = 5\n", 237 | "user_features = torch.randn(n_users, latent_vectors, requires_grad=True)\n", 238 | "user_features.data.mul_(0.01)\n", 239 | "movie_features = torch.randn(n_movies, latent_vectors, requires_grad=True)\n", 240 | "movie_features.data.mul_(0.01)" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 8, 246 | "metadata": {}, 247 | "outputs": [], 248 | "source": [ 249 | "class PMFLoss(torch.nn.Module):\n", 250 | " def __init__(self, lam_u=0.3, lam_v=0.3):\n", 251 | " super().__init__()\n", 252 | " self.lam_u = lam_u\n", 253 | " self.lam_v = lam_v\n", 254 | " \n", 255 | " def forward(self, matrix, u_features, v_features):\n", 256 | " non_zero_mask = (matrix != -1).type(torch.FloatTensor)\n", 257 | " predicted = torch.sigmoid(torch.mm(u_features, v_features.t()))\n", 258 | " \n", 259 | " diff = (matrix - predicted)**2\n", 260 | " prediction_error = torch.sum(diff*non_zero_mask)\n", 261 | "\n", 262 | " u_regularization = self.lam_u * torch.sum(u_features.norm(dim=1))\n", 263 | " v_regularization = self.lam_v * torch.sum(v_features.norm(dim=1))\n", 264 | " \n", 265 | " return prediction_error + u_regularization + v_regularization" 266 | ] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "execution_count": 9, 271 | "metadata": {}, 272 | "outputs": [], 273 | "source": [ 274 | "criterion = PMFLoss()\n", 275 | "loss = criterion(rating_matrix, user_features, movie_features)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": null, 281 | "metadata": {}, 282 | "outputs": [ 283 | { 284 | "name": "stdout", 285 | "output_type": "stream", 286 | "text": [ 287 | "Step 0, 8252.830\n", 288 | "Step 50, 2594.168\n", 289 | "Step 100, 1554.992\n", 290 | "Step 150, 1133.337\n", 291 | "Step 200, 949.591\n", 292 | "Step 250, 854.038\n", 293 | "Step 300, 793.739\n", 294 | "Step 350, 751.326\n", 295 | "Step 400, 719.344\n", 296 | "Step 450, 695.155\n", 297 | "Step 500, 676.232\n", 298 | "Step 550, 660.853\n", 299 | "Step 600, 648.111\n", 300 | "Step 650, 637.387\n" 301 | ] 302 | } 303 | ], 304 | "source": [ 305 | "# Actual training loop now\n", 306 | "\n", 307 | "latent_vectors = 30\n", 308 | "user_features = torch.randn(n_users, latent_vectors, requires_grad=True)\n", 309 | "user_features.data.mul_(0.01)\n", 310 | "movie_features = torch.randn(n_movies, latent_vectors, requires_grad=True)\n", 311 | "movie_features.data.mul_(0.01)\n", 312 | "\n", 313 | "pmferror = PMFLoss(lam_u=0.05, lam_v=0.05)\n", 314 | "optimizer = torch.optim.Adam([user_features, movie_features], lr=0.01)\n", 315 | "for step, epoch in enumerate(range(1000)):\n", 316 | " optimizer.zero_grad()\n", 317 | " loss = pmferror(rating_matrix, user_features, movie_features)\n", 318 | " loss.backward()\n", 319 | " optimizer.step()\n", 320 | " if step % 50 == 0:\n", 321 | " print(f\"Step {step}, {loss:.3f}\")" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": 34, 327 | "metadata": {}, 328 | "outputs": [ 329 | { 330 | "name": "stdout", 331 | "output_type": "stream", 332 | "text": [ 333 | "Predictions: \n", 334 | " tensor([4., 2., 4., 4., 3., 5., 3., 4., 5., 3., 3., 4., 2., 4., 4., 3., 4., 3.,\n", 335 | " 3., 3., 5., 3., 3., 4., 3., 5., 3., 3., 5., 5., 3., 4., 4., 1., 3., 4.,\n", 336 | " 3., 4., 2., 5., 3., 3., 3., 5., 3., 4., 3.], grad_fn=)\n", 337 | "Truth: \n", 338 | " tensor([4., 2., 4., 4., 3., 5., 3., 4., 5., 3., 3., 4., 2., 5., 4., 3., 4., 3.,\n", 339 | " 3., 3., 5., 3., 3., 4., 3., 5., 3., 3., 5., 5., 3., 4., 5., 1., 3., 4.,\n", 340 | " 3., 4., 2., 5., 3., 3., 3., 5., 3., 4., 3.])\n" 341 | ] 342 | } 343 | ], 344 | "source": [ 345 | "# Checking if our model can reproduce the true user ratings\n", 346 | "user_idx = 7\n", 347 | "user_ratings = rating_matrix[user_idx, :]\n", 348 | "true_ratings = user_ratings != -1\n", 349 | "predictions = torch.sigmoid(torch.mm(user_features[user_idx, :].view(1, -1), movie_features.t()))\n", 350 | "predicted_ratings = (predictions.squeeze()[true_ratings]*(max_rating - min_rating) + min_rating).round()\n", 351 | "actual_ratings = (user_ratings[true_ratings]*(max_rating - min_rating) + min_rating).round()\n", 352 | "\n", 353 | "print(\"Predictions: \\n\", predicted_ratings)\n", 354 | "print(\"Truth: \\n\", actual_ratings)" 355 | ] 356 | } 357 | ], 358 | "metadata": { 359 | "kernelspec": { 360 | "display_name": "Python 3", 361 | "language": "python", 362 | "name": "python3" 363 | }, 364 | "language_info": { 365 | "codemirror_mode": { 366 | "name": "ipython", 367 | "version": 3 368 | }, 369 | "file_extension": ".py", 370 | "mimetype": "text/x-python", 371 | "name": "python", 372 | "nbconvert_exporter": "python", 373 | "pygments_lexer": "ipython3", 374 | "version": "3.6.8" 375 | } 376 | }, 377 | "nbformat": 4, 378 | "nbformat_minor": 2 379 | } 380 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pmf-pytorch 2 | Probabilistic Matrix Factorization in PyTorch 3 | -------------------------------------------------------------------------------- /matrix_factorization.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcleonard/pmf-pytorch/9640e701807f85e008e5716096285c122037bde5/matrix_factorization.png --------------------------------------------------------------------------------