CSE-41XX
CS-4125 ML

Lecture 16: Recommender Systems

An introduction to recommender systems, including content-based recommendation methods, collaborative filtering algorithms, low-rank matrix factorization, and mean normalization.

Recommender systems - introduction

Two motivations for talking about recommender systems

  • Important application of ML systems
    • Many technology companies find recommender systems to be absolutely key.
    • Think about websites (Amazon, eBay, iTunes Genius):
      • Try and recommend new content for you based on past purchases.
      • Substantial part of Amazon's revenue generation.
    • Improvement in recommender system performance can bring in more income.
    • Kind of a funny problem:
      • In academic learning, recommender systems receive a small amount of attention.
      • But in industry, it's an absolutely crucial tool.
  • Talk about the big ideas in machine learning
    • Not so much a technique, but an idea.
    • As seen, features are really important:
      • There's a big idea in machine learning that for some problems you can learn what a good set of features are (so not select those features, but learn them).
    • Recommender systems do this — try and identify the crucial and relevant features.

Example - predict movie ratings

  • You're a company who sells movies:
    • You let users rate movies using a 1–5 star rating (to make the example nicer, allow 0–5; makes math easier).
  • You have five movies and four users.
  • Admittedly, business isn't going well, but you're optimistic about the future as a result of your truly outstanding (if limited) inventory.

Movie ratings dataset matrix showing 5 movies and 4 users with missing ratings

  • To introduce some notation:
    • nun_u — Number of users (called nun_u occasionally as we can't subscript in superscript)
    • nmn_m — Number of movies
    • r(i,j)=1r(i, j) = 1 if user jj has rated movie ii (i.e. bitmap)
    • y(i,j)y^{(i,j)} — rating given by user jj to movie ii (defined only if r(i,j)=1r(i,j) = 1)
  • So for this example:
    • nu=4n_u = 4
    • nm=5n_m = 5
  • Summary of scoring:
    • Alice and Bob gave good ratings to rom coms, but low scores to action films.
    • Carol and Dave gave good ratings for action films, but low ratings for rom coms.
  • We have the data given above.
  • The problem is as follows:
    • Given r(i,j)r(i,j) and y(i,j)y^{(i,j)}, go through and try and predict missing values (?s).
    • Come up with a learning algorithm that can fill in these missing values.

Content based recommendation

Using our example above, how do we predict?

  • For each movie we have a feature vector which measures the degree to which a movie is a romantic movie (x1x_1) or an action movie (x2x_2).

Movie features table measuring romance and action degrees

  • If we have features like these, each film can be represented by a feature vector.
  • Add an extra feature x0=1x_0 = 1 (bias unit):
    • So for each film we have a 3×13 \times 1 vector, which for film number 1 ("Love at Last") would be:

Feature vector x^(1) for Love at Last

  • We'll explain later how we derived these values, but for now just take it that we have a feature vector x(i)x^{(i)} associated with movie ii:
    • For movie 1: x(1)=[10.90]x^{(1)} = \begin{bmatrix} 1 \\ 0.9 \\ 0 \end{bmatrix}
    • For movie 2: x(2)=[11.00.01]x^{(2)} = \begin{bmatrix} 1 \\ 1.0 \\ 0.01 \end{bmatrix}
    • For movie 3: x(3)=[10.990]x^{(3)} = \begin{bmatrix} 1 \\ 0.99 \\ 0 \end{bmatrix}
    • For movie 4: x(4)=[10.11.0]x^{(4)} = \begin{bmatrix} 1 \\ 0.1 \\ 1.0 \end{bmatrix}
    • For movie 5: x(5)=[100.9]x^{(5)} = \begin{bmatrix} 1 \\ 0 \\ 0.9 \end{bmatrix}

Feature vectors for all 5 movies

  • So here n=2n = 2 (number of features, not counting x0x_0).
  • For each user jj, we can learn a parameter vector θ(j)Rn+1\theta^{(j)} \in \mathbb{R}^{n+1}.
  • Predict user jj's rating for movie ii as:

Linear regression formula for predicted rating

  • Predicted rating = (θ(j))Tx(i)(\theta^{(j)})^T x^{(i)} stars.

  • Detailed Example: User 1 (Alice)

    • Suppose Alice has parameter vector θ(1)=[050]\theta^{(1)} = \begin{bmatrix} 0 \\ 5 \\ 0 \end{bmatrix}.
    • For Movie 3 ("Cute Puppies of Love"), x(3)=[10.990]x^{(3)} = \begin{bmatrix} 1 \\ 0.99 \\ 0 \end{bmatrix}.
    • Predicted rating: (θ(1))Tx(3)=0(1)+5(0.99)+0(0)=4.95(\theta^{(1)})^T x^{(3)} = 0(1) + 5(0.99) + 0(0) = 4.95 stars.
    • This fits with Alice's known preferences!

How do we learn (θ(j))(\theta^{(j)})?

  • To learn θ(j)\theta^{(j)} (parameter for user jj):
    • Create parameters which give values as close as those seen in the data when applied.
    • Sum over all movies rated by user jj:

User parameter optimization cost function

minθ(j)12m(j)i:r(i,j)=1((θ(j))Tx(i)y(i,j))2+λ2m(j)k=1n(θk(j))2\min_{\theta^{(j)}} \frac{1}{2 m^{(j)}} \sum_{i:r(i,j)=1} \left( (\theta^{(j)})^T x^{(i)} - y^{(i,j)} \right)^2 + \frac{\lambda}{2 m^{(j)}} \sum_{k=1}^n (\theta^{(j)}_k)^2

  • Where m(j)m^{(j)} is the number of movies rated by user jj.
  • To make this a little bit clearer, you can get rid of the m(j)m^{(j)} term (it's just a constant so shouldn't affect the optimal θ(j)\theta^{(j)} value):

Simplified parameter optimization objective

minθ(j)12i:r(i,j)=1((θ(j))Tx(i)y(i,j))2+λ2k=1n(θk(j))2\min_{\theta^{(j)}} \frac{1}{2} \sum_{i:r(i,j)=1} \left( (\theta^{(j)})^T x^{(i)} - y^{(i,j)} \right)^2 + \frac{\lambda}{2} \sum_{k=1}^n (\theta^{(j)}_k)^2

  • So to learn θ(j)\theta^{(j)} for a single user jj:

Single user cost function

  • To learn parameter vectors for all users (θ(1),θ(2),,θ(nu)\theta^{(1)}, \theta^{(2)}, \dots, \theta^{(n_u)}):

All users cost function optimization objective

J(θ(1),,θ(nu))=12j=1nui:r(i,j)=1((θ(j))Tx(i)y(i,j))2+λ2j=1nuk=1n(θk(j))2J(\theta^{(1)}, \dots, \theta^{(n_u)}) = \frac{1}{2} \sum_{j=1}^{n_u} \sum_{i:r(i,j)=1} \left( (\theta^{(j)})^T x^{(i)} - y^{(i,j)} \right)^2 + \frac{\lambda}{2} \sum_{j=1}^{n_u} \sum_{k=1}^n (\theta^{(j)}_k)^2

  • Gradient Descent Update Rules
    • In order to do the minimization, we use the following gradient descent updates:

Gradient descent updates for theta_k

  • For k=0k = 0 (bias term, no regularization): θ0(j):=θ0(j)αi:r(i,j)=1((θ(j))Tx(i)y(i,j))x0(i)\theta_0^{(j)} := \theta_0^{(j)} - \alpha \sum_{i:r(i,j)=1} \left( (\theta^{(j)})^T x^{(i)} - y^{(i,j)} \right) x_0^{(i)}

  • For k0k \neq 0 (with regularization): θk(j):=θk(j)α(i:r(i,j)=1((θ(j))Tx(i)y(i,j))xk(i)+λθk(j))\theta_k^{(j)} := \theta_k^{(j)} - \alpha \left( \sum_{i:r(i,j)=1} \left( (\theta^{(j)})^T x^{(i)} - y^{(i,j)} \right) x_k^{(i)} + \lambda \theta_k^{(j)} \right)

  • We can define the error derivative term as:

Gradient descent derivative term definition

  • This algorithm is called content-based recommendation because we assume we have features available for our content (movies) that describe their features (e.g. romance, action).

Collaborative filtering - overview

  • The collaborative filtering algorithm has a very interesting property — it does feature learning, i.e. it can learn what features to use by itself!

Example table where feature values are unknown

  • So — let's change the problem and pretend we have a dataset where we don't know any of the features associated with the movies (i.e. x1x_1 and x2x_2 are unknown).
  • BUT suppose we have collected parameters θ(1),θ(2),θ(3),θ(4)\theta^{(1)}, \theta^{(2)}, \theta^{(3)}, \theta^{(4)} from our users:

User parameter values provided

  • θ(1)=[050]\theta^{(1)} = \begin{bmatrix} 0 \\ 5 \\ 0 \end{bmatrix} (Alice likes romance)

  • θ(2)=[050]\theta^{(2)} = \begin{bmatrix} 0 \\ 5 \\ 0 \end{bmatrix} (Bob likes romance)

  • θ(3)=[005]\theta^{(3)} = \begin{bmatrix} 0 \\ 0 \\ 5 \end{bmatrix} (Carol likes action)

  • θ(4)=[005]\theta^{(4)} = \begin{bmatrix} 0 \\ 0 \\ 5 \end{bmatrix} (Dave likes action)

  • Given these user parameters, can we infer the feature vector x(i)x^{(i)} for movie ii?

  • Consider Movie 1 ("Love at Last"):

    • Alice and Bob gave it 5 stars.
    • Carol and Dave gave it 0 stars.
    • We want (θ(1))Tx(1)5(\theta^{(1)})^T x^{(1)} \approx 5, (θ(2))Tx(1)5(\theta^{(2)})^T x^{(1)} \approx 5, (θ(3))Tx(1)0(\theta^{(3)})^T x^{(1)} \approx 0, (θ(4))Tx(1)0(\theta^{(4)})^T x^{(1)} \approx 0.
    • From this, we can infer x(1)=[11.00.0]x^{(1)} = \begin{bmatrix} 1 \\ 1.0 \\ 0.0 \end{bmatrix}, meaning Movie 1 is a strong romantic movie with zero action content!

Inferred feature vector x^(1)

Formalizing the collaborative filtering problem

  • Given (θ(1),,θ(nu))(\theta^{(1)}, \dots, \theta^{(n_u)}), we want to learn features x(i)x^{(i)} for movie ii:

Cost function for learning features x^(i) for movie i

minx(i)12j:r(i,j)=1((θ(j))Tx(i)y(i,j))2+λ2k=1n(xk(i))2\min_{x^{(i)}} \frac{1}{2} \sum_{j:r(i,j)=1} \left( (\theta^{(j)})^T x^{(i)} - y^{(i,j)} \right)^2 + \frac{\lambda}{2} \sum_{k=1}^n (x_k^{(i)})^2

  • To learn all features (x(1),,x(nm))(x^{(1)}, \dots, x^{(n_m)}): minx(1),,x(nm)12i=1nmj:r(i,j)=1((θ(j))Tx(i)y(i,j))2+λ2i=1nmk=1n(xk(i))2\min_{x^{(1)}, \dots, x^{(n_m)}} \frac{1}{2} \sum_{i=1}^{n_m} \sum_{j:r(i,j)=1} \left( (\theta^{(j)})^T x^{(i)} - y^{(i,j)} \right)^2 + \frac{\lambda}{2} \sum_{i=1}^{n_m} \sum_{k=1}^n (x_k^{(i)})^2

How does this work with the previous recommendation system?

  • Content-based recommendation systems:
    • Given movie features x(1),,x(nm)x^{(1)}, \dots, x^{(n_m)}, we can learn user parameters θ(1),,θ(nu)\theta^{(1)}, \dots, \theta^{(n_u)}.
  • Feature learning (given parameters):
    • Given user parameters θ(1),,θ(nu)\theta^{(1)}, \dots, \theta^{(n_u)}, we can learn movie features x(1),,x(nm)x^{(1)}, \dots, x^{(n_m)}.
  • This is a chicken-and-egg problem:
    • Which comes first, θ\theta or xx?
    • We can actually iterate back and forth!
    • Randomly initialize θ\theta \rightarrow Learn xx \rightarrow Learn θ\theta \rightarrow Learn xx \rightarrow \dots
    • This converges to a good set of features and parameters!

Collaborative filtering Algorithm

  • Here we combine both ideas into a single joint optimization objective to solve for θ\theta and xx simultaneously.

Given x learn theta cost function

Given theta learn x cost function

  • We can write a single joint cost function J(x(1),,x(nm),θ(1),,θ(nu))J(x^{(1)}, \dots, x^{(n_m)}, \theta^{(1)}, \dots, \theta^{(n_u)}):

Joint collaborative filtering cost function

J(x,θ)=12(i,j):r(i,j)=1((θ(j))Tx(i)y(i,j))2+λ2i=1nmk=1n(xk(i))2+λ2j=1nuk=1n(θk(j))2J(x, \theta) = \frac{1}{2} \sum_{(i,j):r(i,j)=1} \left( (\theta^{(j)})^T x^{(i)} - y^{(i,j)} \right)^2 + \frac{\lambda}{2} \sum_{i=1}^{n_m} \sum_{k=1}^n (x_k^{(i)})^2 + \frac{\lambda}{2} \sum_{j=1}^{n_u} \sum_{k=1}^n (\theta_k^{(j)})^2

Understanding this optimization objective

  • The squared error term sums over all pairs (i,j)(i,j) for which r(i,j)=1r(i,j) = 1 (all user-movie pairs with ratings).

Summation notation over rated pairs (i,j)

  • Notice that in collaborative filtering, we eliminate the bias term x0=1x_0 = 1 and θ0=0\theta_0 = 0. Thus xRnx \in \mathbb{R}^n and θRn\theta \in \mathbb{R}^n. If the algorithm needs a constant feature, it can learn x1=1x_1 = 1 automatically!

Algorithm Structure

  1. Initialize x(1),,x(nm)x^{(1)}, \dots, x^{(n_m)} and θ(1),,θ(nu)\theta^{(1)}, \dots, \theta^{(n_u)} to small random values (similar to neural network weight initialization; breaks symmetry).
  2. Minimize J(x,θ)J(x, \theta) using gradient descent (or advanced optimization algorithms like Conjugate Gradient, L-BFGS):

Gradient descent update formula for x_k^(i)

xk(i):=xk(i)α(j:r(i,j)=1((θ(j))Tx(i)y(i,j))θk(j)+λxk(i))x_k^{(i)} := x_k^{(i)} - \alpha \left( \sum_{j:r(i,j)=1} \left( (\theta^{(j)})^T x^{(i)} - y^{(i,j)} \right) \theta_k^{(j)} + \lambda x_k^{(i)} \right)

Gradient descent update formula for theta_k^(j)

θk(j):=θk(j)α(i:r(i,j)=1((θ(j))Tx(i)y(i,j))xk(i)+λθk(j))\theta_k^{(j)} := \theta_k^{(j)} - \alpha \left( \sum_{i:r(i,j)=1} \left( (\theta^{(j)})^T x^{(i)} - y^{(i,j)} \right) x_k^{(i)} + \lambda \theta_k^{(j)} \right)

  1. For a user jj with parameters θ(j)\theta^{(j)} and a movie ii with learned features x(i)x^{(i)}, predict rating as: Predicted Rating=(θ(j))Tx(i)\text{Predicted Rating} = (\theta^{(j)})^T x^{(i)}

Vectorization: Low rank matrix factorization

  • Now let's look at how to vectorize collaborative filtering calculations.

Matrix Y of ratings

  • Group all ratings into a rating matrix YY of dimension nm×nun_m \times n_u (5×45 \times 4 in our example):
Y=[55005?00?40000540050]Y = \begin{bmatrix} 5 & 5 & 0 & 0 \\ 5 & \text{?} & 0 & 0 \\ \text{?} & 4 & 0 & 0 \\ 0 & 0 & 5 & 4 \\ 0 & 0 & 5 & 0 \end{bmatrix}
  • We can vectorize the predicted ratings matrix as:

Matrix of predicted ratings formula

  • Define movie feature matrix XX:

Movie feature matrix X

X=[(x(1))T(x(2))T(x(nm))T](nm×n)X = \begin{bmatrix} \text{---} & (x^{(1)})^T & \text{---} \\ \text{---} & (x^{(2)})^T & \text{---} \\ & \vdots & \\ \text{---} & (x^{(n_m)})^T & \text{---} \end{bmatrix} \quad (n_m \times n)
  • Define user parameter matrix Θ\Theta:

User parameter matrix Theta

Θ=[(θ(1))T(θ(2))T(θ(nu))T](nu×n)\Theta = \begin{bmatrix} \text{---} & (\theta^{(1)})^T & \text{---} \\ \text{---} & (\theta^{(2)})^T & \text{---} \\ & \vdots & \\ \text{---} & (\theta^{(n_u)})^T & \text{---} \end{bmatrix} \quad (n_u \times n)
  • Then the matrix of predicted ratings is given by: Predicted Ratings=XΘT\text{Predicted Ratings} = X \Theta^T

  • This vectorized algorithm is also called Low Rank Matrix Factorization because XΘTX \Theta^T is a low-rank matrix approximation of YY.

Recommending new movies to a user

  • How can we find movies related to a movie ii?
  • For each movie ii, we learn a feature vector x(i)Rnx^{(i)} \in \mathbb{R}^n.
  • If a user is looking at movie ii, how do we find a movie jj that is most similar to movie ii?
  • Find movie jj such that the distance x(i)x(j)\|x^{(i)} - x^{(j)}\| is small!

Implementation detail: Mean Normalization

  • Let's look at one final implementation detail to make the collaborative filtering algorithm work much better for users with no ratings.

User Eve with no rated movies

  • Problem Case: Consider user 5 ("Eve") who has not rated any movies.
    • What does the algorithm predict for Eve?
    • Let's look at Eve's parameter vector θ(5)\theta^{(5)} (for n=2n=2):

Eve cost function with zero rated items

  • For Eve, r(i,5)=0r(i, 5) = 0 for all movies ii.
  • Thus, the first term in J(x,θ)J(x, \theta) has no terms involving θ(5)\theta^{(5)}!
  • The only term involving θ(5)\theta^{(5)} is the regularization term:

Regularization term for Eve

minθ(5)λ2k=1n(θk(5))2\min_{\theta^{(5)}} \frac{\lambda}{2} \sum_{k=1}^n (\theta_k^{(5)})^2

  • The value of θ(5)\theta^{(5)} that minimizes this is θ(5)=[00]\theta^{(5)} = \begin{bmatrix} 0 \\ 0 \end{bmatrix}.
  • If θ(5)=[00]\theta^{(5)} = \begin{bmatrix} 0 \\ 0 \end{bmatrix}, then for any movie ii, predicted rating is: (θ(5))Tx(i)=0(\theta^{(5)})^T x^{(i)} = 0
  • We predict 0 stars for every movie for Eve! That is useless — we can't make any meaningful recommendations to her.

How does mean normalization work?

  • Group all ratings into matrix YY (with a column of ?s for Eve):

Matrix Y with missing column for Eve

  • Compute average rating for each movie ii into vector μRnm\mu \in \mathbb{R}^{n_m}:

Mean vector mu computation

μ=[2.52.52.02.251.25]\mu = \begin{bmatrix} 2.5 \\ 2.5 \\ 2.0 \\ 2.25 \\ 1.25 \end{bmatrix}
  • Subtract off the mean ratings from YY to get normalized matrix YY:

Subtracting mean vector from rating matrix Y

Y:=YμY := Y - \mu

  • Now every movie has an average rating of 0 in the normalized matrix.

  • Train collaborative filtering on normalized matrix YY to learn x(i)x^{(i)} and θ(j)\theta^{(j)}.

  • For user jj and movie ii, predict rating as: Predicted Rating=(θ(j))Tx(i)+μi\text{Predicted Rating} = (\theta^{(j)})^T x^{(i)} + \mu_i

  • For user 5 (Eve), θ(5)=[00]\theta^{(5)} = \begin{bmatrix} 0 \\ 0 \end{bmatrix} (since no ratings):

Predicted rating for Eve using mean normalization

Predicted Rating for Eve on Movie i=(θ(5))Tx(i)+μi=0+μi=μi\text{Predicted Rating for Eve on Movie } i = (\theta^{(5)})^T x^{(i)} + \mu_i = 0 + \mu_i = \mu_i

  • Now, for a user who has rated no movies, we predict the average rating μi\mu_i for movie ii! This is logical and provides a reasonable baseline for recommendations.

  • Summary / Aside:

    • Mean normalization allows the system to handle new users with zero ratings gracefully by predicting average item ratings.
    • (Similarly, column-wise normalization can handle brand new unrated movies, though usually unrated movies shouldn't be recommended until evaluated).

On this page