TL;DR
Matrix factorization writes a large table, such as users rating films, as the product of two smaller tables: one short vector per user and one per item. Each entry of the big table is then approximated by a dot product of two short vectors. Because the model works from the whole pattern of ratings, it can fill in ratings that nobody has given yet.
Builds on: Matrix Multiplication, Gradient Descent · Leads to: recommender systems, latent factor models
Where it's used
Film recommendations. The Netflix Prize, run from 2006 to 2009, asked teams to predict how users would rate films they had not yet seen. The competition data had about 480,000 users and about 17,800 films, with roughly 100 million known ratings out of a table of about 8.5 billion cells. Matrix factorization models were central to the winning methods, and they are still a standard baseline for recommendation.
Music and shopping. A store sees which items each customer buys or clicks, and most of the table is empty. A factorization gives each customer and each item a short profile, and a high dot product suggests the item will suit the customer. The same idea works for songs, news articles and advertisements.
Topic models for text. A table of documents by words can be factored into documents by topics and topics by words. The rows and columns then have a meaning a reader can inspect. Latent semantic analysis, from the late 1980s and early 1990s, is an early version of this idea, built on the Singular Value Decomposition (SVD).
How it works
Take one tiny instance: one user and one item, each described by a single number, so the "short vectors" have length one. The user's rating for the item is 4. We start from a poor guess and improve it with one gradient step.
- Set up the guesses. Let the user's factor be and the item's factor be . The prediction is the product . Check: the guess is far from the true rating of 4.
- Measure the error. The error is the true rating minus the prediction: . Check: a positive error means the prediction is too low.
- Move each factor toward the rating. A larger error should push the factors harder. Using a learning rate of 0.1, the user factor becomes , and the item factor becomes . Check: both factors grew, so their product will grow.
- Predict again. The new prediction is . The error has fallen from 3 to . Check: one step helped, and more steps would help further.
- Repeat over all known ratings. In a real system the same update runs over every known rating, many times over. The factors settle to values whose dot products match the known ratings and fill in the missing ones.
The idea scales because a user's factor vector is shared across every rating that user gives, and an item's vector is shared across every rating that item receives. Each rating then teaches both vectors something.
The math
The model
Let there be users and items. Write the rating table as , an matrix in which most entries are unknown. Choose a small number of latent factors, much smaller than or . Then find an matrix and an matrix so that
Row of is the user vector , and row of is the item vector . The predicted rating is the dot product
The sum runs over the latent factors. Each factor can be thought of as a hidden quality, such as how much a film is a comedy or how much a user cares about comedy, but the model never needs those labels.
Step 2 in symbols: the error
For one known rating , the error is . In the walkthrough, .
Step 3 in symbols: the gradient step
We minimise the squared error, the loss, over the known ratings, with a penalty that keeps the factors small:
The penalty term with coefficient is L2 Regularization. For one known rating, the Stochastic Gradient Descent update is
The factor 2 that appears when the square is differentiated has been absorbed into the learning rate . In the walkthrough , so each update is , which is the step we took.
Note that both updates use the old values of and . In the walkthrough, the item factor was updated with rather than the new , and that is the correct order.
Why the answer is not unique
If and are a solution, so are and for any invertible matrix , because their product is unchanged. The model is only defined up to this rotation of the latent space. Practical methods pick one solution by the choice of starting point, the penalty and the training order, and they do not interpret the individual factors as fixed meanings.
The link to the SVD
If every entry of were known, the best rank- approximation in the sum-of-squares sense is given by the truncated Singular Value Decomposition (SVD). Matrix factorization with missing entries is harder: the SVD cannot be computed on a table with holes, so the factors are fitted by gradient steps that use only the known entries. When the table is complete and , the two approaches describe the same kind of approximation.
Worked examples
- Set , and the learning rate . The true rating is .
- The prediction is . The error is .
- Update the user factor: .
- Update the item factor with the old user value: .
- The new prediction is , so the error has changed from to .
- Interpretation: the step was too large, and the prediction overshot the rating. With the same step gives and a prediction of , which is below the rating but does not overshoot.
- Take three users and three items, each with factors. The user factors are , and . The item factors are , and .
- Compute the first row of : , , .
- The second row is , , , and the third row is , , .
- Suppose the entry for user 2 and item 3 is hidden. Predict it: .
- Check: the prediction matches the value the full table would have had, which is 4.
- Interpretation: this table has rank 2, so its missing entry is fixed by the factors that generate the rest of the table. Training works in reverse: it starts from the known entries and searches for factors that reproduce them, and the same factors then fill in the gaps.
- Use the sizes quoted for the Netflix Prize: about 480,000 users and about 17,800 films. So and .
- The full table has cells, about 8.5 billion.
- With latent factors, the two factor tables hold numbers, about 50 million.
- The ratio is . The factored model is about 170 times smaller than the full table.
- Interpretation: the model stores far fewer numbers than the table, and it can produce a prediction for any user-film pair, including the many pairs nobody has rated.
Try it: a user has factor and a film has factor . What rating does the model predict? Answer: .
Common mistakes and tradeoffs
It is tempting to think that the factors have fixed, interpretable meanings. In fact the model is defined only up to a rotation of the latent space, as shown above. A factor may look like "comedy" in one run and mix several genres in another. Read the factors as a summary, not as labels.
It is tempting to think that the more factors, the better. In fact a large lets the model fit the known ratings too closely, so its predictions for new pairs get worse. The penalty and a held-out test set, checked by Cross-Validation, are used to choose and .
It is tempting to think that missing entries are zeros. In fact a missing rating means the user has not rated the item, not that the rating is zero. Treating them as zeros teaches the model that users dislike everything they have not seen. The loss is therefore summed only over the known entries.
When to use it. Use matrix factorization when the data is a large, sparse table of interactions, such as ratings, clicks or purchases, and the pattern is roughly low-rank.
When not to use it. Do not use it when there is little data for each user or item, because the vectors cannot be estimated reliably. It also ignores side information such as a film's genre or a user's age, which richer models can include.
Alternatives. The SVD works when the table is complete. Neural recommenders replace the dot product with a learned function, which is more flexible but harder to train and to inspect.
History
Before the mid-2000s, collaborative filtering for recommendations mostly compared users or items directly, using neighbourhoods of similar ratings. Those methods struggled when the table was sparse and large. Matrix factorization was known in linear algebra for much longer, but its use on incomplete rating tables became popular during the Netflix Prize. Simon Funk published a widely read account in 2006 of training such a model by gradient steps over the known ratings, and many competitors built on that approach.
The Netflix Prize ended in 2009 with a team that combined many models, and matrix factorization was a core part of the ensemble. Yehuda Koren, Robert Bell and Chris Volinsky then wrote a 2009 survey that set out the standard form and its regularised training. The method caught on because it scaled to millions of users, handled missing entries directly, and was simple to implement with Stochastic Gradient Descent.
Recap
- A large, sparse rating table is approximated by , one short vector per user and per item.
- The predicted rating is the dot product , and the error is trained by gradient steps.
- The loss is summed only over known entries, and regularisation keeps the factors small.
- The factors are defined only up to rotation, so read them as summaries, not labels.
- Read next: Recommender System for how the predictions become rankings, and Singular Value Decomposition (SVD) for the complete-table case.