The following was compiled by the model 4 starter team,
The originalReleased in
Medium,The author
Steeve Huang.The content reproduced is only used for learning and communication, and the copyright belongs to the original author.



1. The background

In the previous article “Want to know about recommendation Systems? Look here! (1) – Collaborative Filtering and Singular value Decomposition”, we talked about how collaborative filtering (CF) and singular value decomposition (SVD) can be used to build recommendation systems. With the rise of neural network, how to use this technology to build recommendation system has attracted a lot of attention. This post introduces Spotlight, a recommendation system framework powered by PyTorch.

2. Spotlight

Spotlight is a well-implemented Python framework for building recommendation systems. It contains two main types of models, decomposition models and sequence models.

The decomposition model utilizes the ideas behind SVD to decompose the utility matrix (the matrix that records the interaction between the user and the project) into two potential representations of the user and the project matrix and feed them back into the network.

Series models are constructed using time series models, such as long-term short-term memory (LSTM) and one-dimensional convolutional neural network (CNN). Since Spotlight’s back end is PyTorch, make sure you have the correct version of PyTorch installed before using it.

interaction

In Spotlight, utility matrices are called interactions. To create an implicit interaction, we specify a separate ID for each user-project interaction pair. Additional rating information transforms the implicit interaction into an explicit one.

Decomposition model

The decomposition model uses either implicit or explicit interaction. The rest of this article provides a brief explanation of implicit interactions.

The idea of implicit interaction is very similar to SVD, where users and objects are mapped to potential Spaces and can be directly compared. In general, we use two embedding layers to represent the user and the project.

The target is the interaction we pass in (the utility matrix). To calculate the score of the user-item pair, we take the dot product of the potential representations of the user and the item and pass it to the SigmoID activation function.

By calculating the losses of all user-project pairs for real interactions, we can back-propagate and optimize the embedding layer, as shown in the figure below.

The model takes just a few lines of code to train in Spotlight, and it looks very similar to the SciKit-Learn toolkit:

Order model

The sequential model treats the recommendation problem as the sequential prediction problem. Given the user’s previous interaction data, we want to know which items the user is most likely to like in the next time step.

For example, suppose user A interacts with the items in the sequence [2,4,17,3,5]. Next we will make the following extended window predictions.

[2] - > 4 [2, 4] - > [17] 2, 4, 17 - > 3 [2, 4, 17, 3] - > 5Copy the code

The array on the left stores the user’s previous interactions, while the integers on the right represent the items with which user A will interact next.

To train such a model, we simply transform the original interaction object into a sequential interaction object. The same applies to other objects.

Note that in order to ensure that each Sequence has the same length, the Sequence function will fill in zeros before any Sequence that is short of length.

Therefore, to ensure that this function works, the item with ID 0 should be changed to any other ID number that is not in use.

Selection loss function

We have the flexibility to change the loss function when specifying the model. Models with different loss functions may differ significantly in performance. This article will briefly cover the two main types of loss functions defined in Spotlight.

  • ‘pointwise’ :

This is the simplest form of the loss function compared to other forms. Due to the sparsity of the sample (there are multiple zeros in the utility matrix), it is impossible to calculate if all items are considered. Therefore, we consider only a randomly selected subset of the negative sample (items with which the user did not interact) and all of the positive samples.

  • ‘BPR’ :

Bayesian personalized sorting (BPR) provides each user with a ranking of each item. BPR uses the following calculation formula to ensure that the grade of the positive sample is higher than that of the negative sample.

ranking loss

Conclusion 5.

This article discusses how to build a recommendation system with Spotlight. This approach is simple and flexible enough to meet most requirements. Although sequence models are superior to decomposition models for most problems, training sequence models takes longer. In addition, applying a sequence model is of little use if there is no significant sequential correlation between the data.


Related reading:

Want to learn about recommendation systems? Look here! (1) — Collaborative filtering and singular value decomposition

How to realize automatic online, operation and maintenance of intelligent recommendation system?

How to implement ai-enabled new media technology landing?

Getting started with recommendation systems, a list of knowledge you shouldn’t miss

For more information, please search and follow the official weibo @Xianjian and wechat official account (ID: DSFSXJ).