Search

Search papers and lessons

Matrix Factorization

Level 3 · 11 min read

Matrix factorization approximates a big table of ratings as the product of two small tables, one row per user and one per item, so missing entries can be filled in by a dot product.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Probability
  • Losses & regularization
  • Optimizers
  • ReadReady to readNot yet

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.

  1. Set up the guesses. Let the user's factor be p=1p = 1 and the item's factor be q=1q = 1. The prediction is the product p×q=1p \times q = 1. Check: the guess is far from the true rating of 4.
  2. Measure the error. The error is the true rating minus the prediction: 4−1=34 - 1 = 3. Check: a positive error means the prediction is too low.
  3. 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 p=1+0.1×3×1=1.3p = 1 + 0.1 \times 3 \times 1 = 1.3, and the item factor becomes q=1+0.1×3×1=1.3q = 1 + 0.1 \times 3 \times 1 = 1.3. Check: both factors grew, so their product will grow.
  4. Predict again. The new prediction is 1.3×1.3=1.691.3 \times 1.3 = 1.69. The error has fallen from 3 to 4−1.69=2.314 - 1.69 = 2.31. Check: one step helped, and more steps would help further.
  5. 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 mm users and nn items. Write the rating table as RR, an m×nm \times n matrix in which most entries are unknown. Choose a small number kk of latent factors, much smaller than mm or nn. Then find an m×km \times k matrix PP and an n×kn \times k matrix QQ so that

R≈PQT.R \approx P Q^{\mathsf{T}}.

Row uu of PP is the user vector pu\mathbf{p}_u, and row ii of QQ is the item vector qi\mathbf{q}_i. The predicted rating is the dot product

r^ui=pu⋅qi=∑f=1kpuf qif.\hat{r}_{ui} = \mathbf{p}_u \cdot \mathbf{q}_i = \sum_{f=1}^{k} p_{uf}\, q_{if}.

The sum runs over the kk 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 ruir_{ui}, the error is eui=rui−pu⋅qie_{ui} = r_{ui} - \mathbf{p}_u \cdot \mathbf{q}_i. In the walkthrough, e=4−1=3e = 4 - 1 = 3.

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:

min⁡P,Q ∑(u,i) known(rui−pu⋅qi)2+λ(∥pu∥2+∥qi∥2).\min_{P, Q}\ \sum_{(u,i)\ \text{known}} \left(r_{ui} - \mathbf{p}_u \cdot \mathbf{q}_i\right)^2 + \lambda\left(\lVert \mathbf{p}_u \rVert^2 + \lVert \mathbf{q}_i \rVert^2\right).

The penalty term with coefficient λ\lambda is L2 Regularization. For one known rating, the Stochastic Gradient Descent update is

pu←pu+η(eui qi−λ pu),qi←qi+η(eui pu−λ qi).\mathbf{p}_u \leftarrow \mathbf{p}_u + \eta\left(e_{ui}\, \mathbf{q}_i - \lambda\, \mathbf{p}_u\right), \qquad \mathbf{q}_i \leftarrow \mathbf{q}_i + \eta\left(e_{ui}\, \mathbf{p}_u - \lambda\, \mathbf{q}_i\right).

The factor 2 that appears when the square is differentiated has been absorbed into the learning rate η\eta. In the walkthrough λ=0\lambda = 0, so each update is p←p+η e qp \leftarrow p + \eta\, e\, q, which is the step we took.

Note that both updates use the old values of pu\mathbf{p}_u and qi\mathbf{q}_i. In the walkthrough, the item factor was updated with p=1p = 1 rather than the new p=1.3p = 1.3, and that is the correct order.

Why the answer is not unique

If PP and QQ are a solution, so are PAPA and QA−TQA^{-\mathsf{T}} for any invertible k×kk \times k matrix AA, 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.

If every entry of RR were known, the best rank-kk 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 λ=0\lambda = 0, the two approaches describe the same kind of approximation.

Worked examples

Example
1. One rating, one factor
  1. Set p=1p = 1, q=1q = 1 and the learning rate η=0.5\eta = 0.5. The true rating is r=2r = 2.
  2. The prediction is r^=pq=1\hat{r} = pq = 1. The error is e=2−1=1e = 2 - 1 = 1.
  3. Update the user factor: p←1+0.5×1×1=1.5p \leftarrow 1 + 0.5 \times 1 \times 1 = 1.5.
  4. Update the item factor with the old user value: q←1+0.5×1×1=1.5q \leftarrow 1 + 0.5 \times 1 \times 1 = 1.5.
  5. The new prediction is 1.5×1.5=2.251.5 \times 1.5 = 2.25, so the error has changed from +1+1 to −0.25-0.25.
  6. Interpretation: the step was too large, and the prediction overshot the rating. With η=0.1\eta = 0.1 the same step gives p=q=1.1p = q = 1.1 and a prediction of 1.211.21, which is below the rating but does not overshoot.
Example
2. A small table built from known factors
  1. Take three users and three items, each with k=2k = 2 factors. The user factors are p1=(2,1)\mathbf{p}_1 = (2, 1), p2=(1,2)\mathbf{p}_2 = (1, 2) and p3=(0,1)\mathbf{p}_3 = (0, 1). The item factors are q1=(2,0)\mathbf{q}_1 = (2, 0), q2=(1,1)\mathbf{q}_2 = (1, 1) and q3=(0,2)\mathbf{q}_3 = (0, 2).
  2. Compute the first row of RR: p1⋅q1=4\mathbf{p}_1 \cdot \mathbf{q}_1 = 4, p1⋅q2=3\mathbf{p}_1 \cdot \mathbf{q}_2 = 3, p1⋅q3=2\mathbf{p}_1 \cdot \mathbf{q}_3 = 2.
  3. The second row is 22, 33, 44, and the third row is 00, 11, 22.
  4. Suppose the entry for user 2 and item 3 is hidden. Predict it: p2⋅q3=(1)(0)+(2)(2)=4\mathbf{p}_2 \cdot \mathbf{q}_3 = (1)(0) + (2)(2) = 4.
  5. Check: the prediction matches the value the full table would have had, which is 4.
  6. 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.
Example
3. The storage saving at Netflix scale
  1. Use the sizes quoted for the Netflix Prize: about 480,000 users and about 17,800 films. So m=480,000m = 480{,}000 and n=17,800n = 17{,}800.
  2. The full table has mn=480,000×17,800=8,544,000,000mn = 480{,}000 \times 17{,}800 = 8{,}544{,}000{,}000 cells, about 8.5 billion.
  3. With k=100k = 100 latent factors, the two factor tables hold k(m+n)=100×497,800=49,780,000k(m + n) = 100 \times 497{,}800 = 49{,}780{,}000 numbers, about 50 million.
  4. The ratio is 8.5×109/5.0×107≈1718.5 \times 10^{9} / 5.0 \times 10^{7} \approx 171. The factored model is about 170 times smaller than the full table.
  5. 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 (1,3)(1, 3) and a film has factor (2,1)(2, 1). What rating does the model predict? Answer: 1×2+3×1=51 \times 2 + 3 \times 1 = 5.

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 kk lets the model fit the known ratings too closely, so its predictions for new pairs get worse. The penalty λ\lambda and a held-out test set, checked by Cross-Validation, are used to choose kk and λ\lambda.

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 R≈PQTR \approx PQ^{\mathsf{T}}, one short vector per user and per item.
  • The predicted rating is the dot product pu⋅qi\mathbf{p}_u \cdot \mathbf{q}_i, 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.

Check yourself

QuestionWhat is the predicted rating when a user's factor is (3,0)(3, 0) and an item's factor is (1,4)(1, 4)?Show answer
The dot product is 3×1+0×4=33 \times 1 + 0 \times 4 = 3.
QuestionA user factor is p=2p = 2, an item factor is q=1q = 1, the true rating is r=5r = 5 and the learning rate is η=0.1\eta = 0.1. What is the new user factor after one step without regularisation?Show answer
The error is e=5−2×1=3e = 5 - 2 \times 1 = 3. The update is p←2+0.1×3×1=2.3p \leftarrow 2 + 0.1 \times 3 \times 1 = 2.3.
QuestionWhy are missing entries not treated as zeros when training a factorization?Show answer
A missing entry means no rating was given, not a rating of zero. Treating it as zero would teach the model that users dislike unseen items, so the loss sums only over the known entries.
QuestionA company has 1 million users and 10,000 items, and wants 50 latent factors. Roughly how many numbers does the factored model store, and how does that compare with the full table?Show answer
The factors hold 50×(1,000,000+10,000)=50,500,00050 \times (1{,}000{,}000 + 10{,}000) = 50{,}500{,}000 numbers, about 50 million. The full table has 101010^{10} cells, about 10 billion, so the factored model is roughly 200 times smaller.
QuestionA team finds that its factors change completely between two training runs, and one run's factor looks like comedy while the other's does not. Is the model broken?Show answer
Not necessarily. The model is defined only up to a rotation of the latent space, so the factors can differ between runs while giving the same predictions. The team should judge the model by its predictions on held-out ratings, not by the meaning of individual factors.

Sources