{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Importing the libraries\n", "import numpy as np\n", "import pandas as pd\n", "import torch\n", "import torch.nn as nn\n", "import torch.nn.parallel\n", "import torch.optim as optim\n", "import torch.utils.data\n", "from torch.autograd import Variable" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Importing the dataset\n", "movies = pd.read_csv('ml-1m/movies.dat', sep = '::', header = None, engine = 'python', encoding = 'latin-1')\n", "users = pd.read_csv('ml-1m/users.dat', sep = '::', header = None, engine = 'python', encoding = 'latin-1')\n", "ratings = pd.read_csv('ml-1m/ratings.dat', sep = '::', header = None, engine = 'python', encoding = 'latin-1')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Preparing the training set and the test set\n", "training_set = pd.read_csv('ml-100k/u1.base', delimiter = '\\t')\n", "training_set = np.array(training_set, dtype = 'int')\n", "test_set = pd.read_csv('ml-100k/u1.test', delimiter = '\\t')\n", "test_set = np.array(test_set, dtype = 'int')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Getting the number of users and movies\n", "nb_users = int(max(max(training_set[:,0]), max(test_set[:,0])))\n", "nb_movies = int(max(max(training_set[:,1]), max(test_set[:,1])))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Converting the data into an array with users in lines and movies in columns\n", "def convert(data):\n", " new_data = []\n", " for id_users in range(1, nb_users + 1):\n", " id_movies = data[:,1][data[:,0] == id_users]\n", " id_ratings = data[:,2][data[:,0] == id_users]\n", " ratings = np.zeros(nb_movies)\n", " ratings[id_movies - 1] = id_ratings\n", " new_data.append(list(ratings))\n", " return new_data\n", "training_set = convert(training_set)\n", "test_set = convert(test_set)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Converting the data into Torch tensors\n", "training_set = torch.FloatTensor(training_set)\n", "test_set = torch.FloatTensor(test_set)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Creating the architecture of the Neural Network\n", "class SAE(nn.Module):\n", " def __init__(self, ):\n", " super(SAE, self).__init__()\n", " self.fc1 = nn.Linear(nb_movies, 20)\n", " self.fc2 = nn.Linear(20, 10)\n", " self.fc3 = nn.Linear(10, 20)\n", " self.fc4 = nn.Linear(20, nb_movies)\n", " self.activation = nn.Sigmoid()\n", " def forward(self, x):\n", " x = self.activation(self.fc1(x))\n", " x = self.activation(self.fc2(x))\n", " x = self.activation(self.fc3(x))\n", " x = self.fc4(x)\n", " return x\n", "sae = SAE()\n", "criterion = nn.MSELoss()\n", "optimizer = optim.RMSprop(sae.parameters(), lr = 0.01, weight_decay = 0.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Training the SAE\n", "nb_epoch = 200\n", "for epoch in range(1, nb_epoch + 1):\n", " train_loss = 0\n", " s = 0.\n", " for id_user in range(nb_users):\n", " input = Variable(training_set[id_user]).unsqueeze(0)\n", " target = input.clone()\n", " if torch.sum(target.data > 0) > 0:\n", " output = sae(input)\n", " target.require_grad = False\n", " output[target == 0] = 0\n", " loss = criterion(output, target)\n", " mean_corrector = nb_movies/float(torch.sum(target.data > 0) + 1e-10)\n", " loss.backward()\n", " train_loss += np.sqrt(loss.item()*mean_corrector)\n", " s += 1.\n", " optimizer.step()\n", " print('epoch: '+str(epoch)+' loss: '+str(train_loss/s))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Testing the SAE\n", "test_loss = 0\n", "s = 0.\n", "for id_user in range(nb_users):\n", " input = Variable(training_set[id_user]).unsqueeze(0)\n", " target = Variable(test_set[id_user])\n", " if torch.sum(target.data > 0) > 0:\n", " output = sae(input)\n", " target.require_grad = False\n", " output[0, target == 0] = 0\n", " loss = criterion(output, target)\n", " mean_corrector = nb_movies/float(torch.sum(target.data > 0) + 1e-10)\n", " test_loss += np.sqrt(loss.item()*mean_corrector)\n", " s += 1.\n", "print('test loss: '+str(test_loss/s))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.10" } }, "nbformat": 4, "nbformat_minor": 4 }