Book

Personalized Recommendation

The source code from this tutorial is at here. For instructions to run it, please refer to this guide.

Background

The recommender system is a component of e-commerce, online videos, and online reading services. There are several different approaches for recommender systems to learn from user behavior and product properties and to understand users' interests.

User behavior-based approach. A well-known method of this approach is collaborative filtering, which assumes that if two users made similar purchases, they share common interests and would likely go on making the same decision. Some variants of collaborative filtering are user-based[3], item-based [4], social network based[5], and model-based.

Content-based approach[1]. This approach represents product properties and user interests as feature vectors of the same space so that it could measure how much a user is interested in a product by the distance between two feature vectors.

Hybrid approach[2]: This one combines above two to help with each other about the data sparsity problem[6].

This tutorial explains a deep learning based hybrid approach and its implement in PaddlePaddle. We are going to train a model using a dataset that includes user information, movie information, and ratings. Once we train the model, we will be able to get a predicted rating given a pair of user and movie IDs.

Model Overview

To know more about deep learning based recommendation, let us start from going over the Youtube recommender system[7] before introducing our hybrid model.

YouTube's Deep Learning Recommendation Model

YouTube is a video-sharing Web site with one of the largest user base in the world. Its recommender system serves more than a billion users. This system is composed of two major parts: candidate generation and ranking. The former selects few hundreds of candidates from millions of videos, and the latter ranks and outputs the top 10.

Figure 1. YouTube recommender system overview.

Candidate Generation Network

YouTube models candidate generation as a multi-class classification problem with a huge number of classes equal to the number of videos. The architecture of the model is as follows:

Figure 2. Deep candidate generation model.

The first stage of this model maps watching history and search queries into fixed-length representative features. Then, an MLP (multi-layer Perceptron, as described in the Recognize Digits tutorial) takes the concatenation of all representative vectors. The output of the MLP represents the user' intrinsic interests. At training time, it is used together with a softmax output layer for minimizing the classification error. At serving time, it is used to compute the relevance of the user with all movies.

For a user $U$, the predicted watching probability of video $i$ is

$$P(\omega=i|u)=\frac{e^{v_{i}u}}{\sum_{j \in V}e^{v_{j}u}}$$

where $u$ is the representative vector of user $U$, $V$ is the corpus of all videos, $v_i$ is the representative vector of the $i$-th video. $u$ and $v_i$ are vectors of the same length, so we can compute their dot product using a fully connected layer.

This model could have a performance issue as the softmax output covers millions of classification labels. To optimize performance, at the training time, the authors down-sample negative samples, so the actual number of classes is reduced to thousands. At serving time, the authors ignore the normalization of the softmax outputs, because the results are just for ranking.

Ranking Network

The architecture of the ranking network is similar to that of the candidate generation network. Similar to ranking models widely used in online advertising, it uses rich features like video ID, last watching time, etc. The output layer of the ranking network is a weighted logistic regression, which rates all candidate videos.

Hybrid Model

In the section, let us introduce our movie recommendation system. Especially, we feed moives titles into a text convolution network to get a fixed-length representative feature vector. Accordingly we will introduce the convolutional neural network for texts and the hybrid recommendation model respectively.

Convolutional Neural Networks for Texts (CNN)

Convolutional Neural Networks are frequently applied to data with grid-like topology such as two-dimensional images and one-dimensional texts. A CNN can extract multiple local features, combine them, and produce high-level abstractions, which correspond to semantic understanding. Empirically, CNN is shown to be efficient for image and text modeling.

CNN mainly contains convolution and pooling operation, with versatile combinations in various applications. Here, we briefly describe a CNN as shown in Figure 3.

Figure 3. CNN for text modeling.

Let $n$ be the length of the sentence to process, and the $i$-th word has embedding as $x_i\in\mathbb{R}^k$，where $k$ is the embedding dimensionality.

First, we concatenate the words by piecing together every $h$ words, each as a window of length $h$. This window is denoted as $x_{i:i+h-1}$, consisting of $x_{i},x_{i+1},\ldots,x_{i+h-1}$, where $x_i$ is the first word in the window and $i$ takes value ranging from $1$ to $n-h+1$: $x_{i:i+h-1}\in\mathbb{R}^{hk}$.

Next, we apply the convolution operation: we apply the kernel $w\in\mathbb{R}^{hk}$ in each window, extracting features $c_i=f(w\cdot x_{i:i+h-1}+b)$, where $b\in\mathbb{R}$ is the bias and $f$ is a non-linear activation function such as $sigmoid$. Convolving by the kernel at every window ${x_{1:h},x_{2:h+1},\ldots,x_{n-h+1:n}}$ produces a feature map in the following form:

$$c=[c_1,c_2,\ldots,c_{n-h+1}], c \in \mathbb{R}^{n-h+1}$$

Next, we apply max pooling over time to represent the whole sentence $\hat c$, which is the maximum element across the feature map:

$$\hat c=max(c)$$

Model Structure Of The Hybrid Model

In our network, the input includes features of users and movies. The user feature includes four properties: user ID, gender, occupation, and age. Movie features include their IDs, genres, and titles.

We use fully-connected layers to map user features into representative feature vectors and concatenate them. The process of movie features is similar, except that for movie titles -- we feed titles into a text convolution network as described in the above section to get a fixed-length representative feature vector.

Given the feature vectors of users and movies, we compute the relevance using cosine similarity. We minimize the squared error at training time.

Figure 4. A hybrid recommendation model.

Dataset

We use the MovieLens ml-1m to train our model. This dataset includes 10,000 ratings of 4,000 movies from 6,000 users to 4,000 movies. Each rate is in the range of 1~5. Thanks to GroupLens Research for collecting, processing and publishing the dataset.

paddle.datasets package encapsulates multiple public datasets, including cifar, imdb, mnist, movielens and wmt14, etc. There's no need for us to manually download and preprocess MovieLens dataset.

The raw MoiveLens contains movie ratings, relevant features from both movies and users.
For instance, one movie's feature could be:

As shown in the above code, the input is four dimension integers for each user, that is user_id,gender_id, age_id and job_id. In order to deal with these features conveniently, we use the language model in NLP to transform these discrete values into embedding vaules usr_emb, usr_gender_emb, usr_age_emb and usr_job_emb.

Then we can use user features as input, directly connecting to a fully-connected layer, which is used to reduce dimension to 200.

Furthermore, we do a similar transformation for each movie feature. The model configuration is:

Movie title, which is a sequence of words represented by an integer word index sequence, will be fed into a sequence_conv_pool layer, which will apply convolution and pooling on time dimension. Because pooling is done on time dimension, the output will be a fixed-length vector regardless the length of the input sequence.

Finally, we can define a inference_program that uses cosine similarity to calculate the similarity between user characteristics and movie features.

Model Training

Specify training environment

Specify your training environment, you should specify if the training is on CPU or GPU.

use_cuda=Falseplace=fluid.CUDAPlace(0)ifuse_cudaelsefluid.CPUPlace()

Datafeeder Configuration

Next we define data feeders for test and train. The feeder reads a buf_size of data each time and feed them to the training/testing process.
paddle.dataset.movielens.train will yield records during each pass, after shuffling, a batch input of BATCH_SIZE is generated for training.

Create Trainer

Feeding Data

feed_order is devoted to specifying the correspondence between each yield record and paddle.layer.data. For instance, the first column of data generated by movielens.train corresponds to user_id feature.

Create Inferencer

Generate input data for testing

Use create_lod_tensor(data, lod, place) API to generate LoD Tensor, where data is a list of sequences of index numbers, lod is the level of detail (lod) info associated with data.
For example, data = [[10, 2, 3], [2, 3]] means that it contains two sequences of indices, of length 3 and 2, respectively.
Correspondingly, lod = [[3, 2]] contains one level of detail info, indicating that data consists of two sequences of length 3 and 2.

In this infer example, we try to predict rating of movie 'Hunchback of Notre Dame' from the info of user id 1.

Conclusion

This tutorial goes over traditional approaches in recommender system and a deep learning based approach. We also show that how to train and use the model with PaddlePaddle. Deep learning has been well used in computer vision and NLP, we look forward to its new successes in recommender systems.