In this notebook, I'll build a deep learning model Ads recommendations system on the Squad Digital dataset.

Similar to what I did for the previous notebooks, I loaded the 3 datasets into 3 dataframes: *frequency*, *users*, and *interests*. Additionally, to make it easy to use series from the *frequency* dataframe as training inputs and output to the Keras model, I set *max_userid* as the max value of user_id in the frequency and *max_ineterestid* as the max value of ineterest_id in the frequency.

Loading output library...

Loading output library...

Loading output library...

I definitely would want to create a training and validation set and optimize the number of embeddings by minimizing the RMSE. Intuitively, the RMSE will decrease on the training set as the number of embeddings increases (because I'm approximating the original ratings matrix with a higher rank matrix). Here I create a training set by shuffling randomly the values from the original ratings dataset.

We want our model to learn the values of embedding matrix itself. The user latent features and ineterests latent features are looked up from the embedding matrices for specific interest-user combination. These are the input values for further linear and non-linear layers. We can pass this input to multiple relu, linear or sigmoid layers and learn the corresponding weights by any optimization algorithm (Adam, SGD, etc.).

I created a sparse matrix factoring algorithm in Keras for my model in CFModel.py Here are the main components:

- A left embedding layer that creates a Users by Latent Factors matrix.
- A right embedding layer that creates a interest by Latent Factors matrix.
- When the input to these layers are (i) a user id and (ii) a interest id, they'll return the latent factor vectors for the user and the interest, respectively.
- A merge layer that takes the dot product of these two latent vectors to return the predicted rating.

I then compile the model using Mean Squared Error (MSE) as the loss function and the AdaMax learning algorithm.

Now I need to train the model. This step will be the most-time consuming one.

During the training process above, I saved the model weights each time the validation loss has improved. Thus, I can use that value to calculate the best validation Root Mean Square Error.

The best validation loss is *0.8512* at epoch 5. Taking the square root of that number, I got the RMSE value of *0.9226*.

The next step is to actually predict the frequency a random user will go to a random interest. Below I apply the freshly trained deep learning model for all the users and all the interests, using 100 dimensional embeddings for each of them. I also load pre-trained weights from *weights.h5

As mentioned above, my random test user is has ID 6.

Loading output library...

Here I define the function to predict user's rating of unfrequented interests, using the *rate* function inside the CFModel class in *CFModel.py

Here I show the top 20 interests that user 6 has already frequented, including the *predictions* column showing the values that user 6 would have frequented based on the newly defined *predict_rating* function.

Loading output library...

Here I make a recommendation list of unfrequented 20 interests sorted by prediction value for user 6. Let's see it.

Loading output library...