# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Copyright 2018 Google LLC.
Recommendation Systems with TensorFlow
This Colab notebook complements the course on Recommendation Systems. Specifically, we’ll be using matrix factorization to learn user and movie embeddings.
Introduction
We will create a movie recommendation system based on the MovieLens dataset available here. The data consists of movies ratings (on a scale of 1 to 5).
Outline
- Exploring the MovieLens Data (10 minutes)
- Preliminaries (25 minutes)
- Training a matrix factorization model (15 minutes)
- Inspecting the Embeddings (15 minutes)
- Regularization in matrix factorization (15 minutes)
- Softmax model training (30 minutes)
Setup
Let’s get started by importing the required packages.
# @title Imports (run this cell)
from __future__ import print_function
import numpy as np
import pandas as pd
import collections
from mpl_toolkits.mplot3d import Axes3D
from IPython import display
from matplotlib import pyplot as plt
import sklearn
import sklearn.manifold
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tf.logging.set_verbosity(tf.logging.ERROR)
# Add some convenience functions to Pandas DataFrame.
= 10
pd.options.display.max_rows = '{:.3f}'.format
pd.options.display.float_format def mask(df, key, function):
"""Returns a filtered dataframe, by applying function to key"""
return df[function(df[key])]
def flatten_cols(df):
= [' '.join(col).strip() for col in df.columns.values]
df.columns return df
= mask
pd.DataFrame.mask = flatten_cols
pd.DataFrame.flatten_cols
# Install Altair and activate its colab renderer.
print("Installing Altair...")
%pip install altair
# !pip install git+git://github.com/altair-viz/altair.git
import altair as alt
'default', max_rows=None)
alt.data_transformers.enable(#alt.renderers.enable('colab')
print("Done installing Altair.")
# Install spreadsheets and import authentication module.
= False
USER_RATINGS %pip install --upgrade -q gspread
# from google.colab import auth
import gspread
# from oauth2client.client import GoogleCredentials
2023-10-10 17:11:37.171582: I tensorflow/core/platform/cpu_feature_guard.cc:194] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: SSE3 SSE4.1 SSE4.2 AVX
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-10-10 17:11:37.269854: I tensorflow/core/util/port.cc:104] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
WARNING:tensorflow:From /usr/local/lib/python3.8/dist-packages/tensorflow/python/compat/v2_compat.py:107: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
Installing Altair...
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: altair in /home/vscode/.local/lib/python3.8/site-packages (5.1.2)
Requirement already satisfied: typing-extensions>=4.0.1 in /usr/local/lib/python3.8/dist-packages (from altair) (4.4.0)
Requirement already satisfied: numpy in /usr/local/lib/python3.8/dist-packages (from altair) (1.22.2)
Requirement already satisfied: jsonschema>=3.0 in /usr/local/lib/python3.8/dist-packages (from altair) (4.17.3)
Requirement already satisfied: pandas>=0.25 in /usr/local/lib/python3.8/dist-packages (from altair) (1.5.2)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.8/dist-packages (from altair) (3.1.2)
Requirement already satisfied: toolz in /usr/local/lib/python3.8/dist-packages (from altair) (0.12.0)
Requirement already satisfied: packaging in /usr/local/lib/python3.8/dist-packages (from altair) (22.0)
Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /usr/local/lib/python3.8/dist-packages (from jsonschema>=3.0->altair) (0.19.3)
Requirement already satisfied: attrs>=17.4.0 in /usr/local/lib/python3.8/dist-packages (from jsonschema>=3.0->altair) (22.2.0)
Requirement already satisfied: pkgutil-resolve-name>=1.3.10 in /usr/local/lib/python3.8/dist-packages (from jsonschema>=3.0->altair) (1.3.10)
Requirement already satisfied: importlib-resources>=1.4.0 in /usr/local/lib/python3.8/dist-packages (from jsonschema>=3.0->altair) (5.10.2)
Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.8/dist-packages (from pandas>=0.25->altair) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.8/dist-packages (from pandas>=0.25->altair) (2022.6)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.8/dist-packages (from jinja2->altair) (2.1.1)
Requirement already satisfied: zipp>=3.1.0 in /usr/local/lib/python3.8/dist-packages (from importlib-resources>=1.4.0->jsonschema>=3.0->altair) (3.11.0)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.8/dist-packages (from python-dateutil>=2.8.1->pandas>=0.25->altair) (1.16.0)
Note: you may need to restart the kernel to use updated packages.
Done installing Altair.
Note: you may need to restart the kernel to use updated packages.
We then download the MovieLens Data, and create DataFrames containing movies, users, and ratings.
# @title Load the MovieLens data (run this cell).
# Download MovieLens data.
print("Downloading movielens data...")
from urllib.request import urlretrieve
import zipfile
"http://files.grouplens.org/datasets/movielens/ml-100k.zip", "movielens.zip")
urlretrieve(= zipfile.ZipFile('movielens.zip', "r")
zip_ref
zip_ref.extractall()print("Done. Dataset contains:")
print(zip_ref.read('ml-100k/u.info'))
# Load each data set (users, movies, and ratings).
= ['user_id', 'age', 'sex', 'occupation', 'zip_code']
users_cols = pd.read_csv(
users 'ml-100k/u.user', sep='|', names=users_cols, encoding='latin-1')
= ['user_id', 'movie_id', 'rating', 'unix_timestamp']
ratings_cols = pd.read_csv(
ratings 'ml-100k/u.data', sep='\t', names=ratings_cols, encoding='latin-1')
# The movies file contains a binary feature for each genre.
= [
genre_cols "genre_unknown", "Action", "Adventure", "Animation", "Children", "Comedy",
"Crime", "Documentary", "Drama", "Fantasy", "Film-Noir", "Horror",
"Musical", "Mystery", "Romance", "Sci-Fi", "Thriller", "War", "Western"
]= [
movies_cols 'movie_id', 'title', 'release_date', "video_release_date", "imdb_url"
+ genre_cols
] = pd.read_csv(
movies 'ml-100k/u.item', sep='|', names=movies_cols, encoding='latin-1')
# Since the ids start at 1, we shift them to start at 0.
"user_id"] = users["user_id"].apply(lambda x: str(x-1))
users["movie_id"] = movies["movie_id"].apply(lambda x: str(x-1))
movies["year"] = movies['release_date'].apply(lambda x: str(x).split('-')[-1])
movies["movie_id"] = ratings["movie_id"].apply(lambda x: str(x-1))
ratings["user_id"] = ratings["user_id"].apply(lambda x: str(x-1))
ratings["rating"] = ratings["rating"].apply(lambda x: float(x))
ratings[
# Compute the number of movies to which a genre is assigned.
= movies[genre_cols].sum().to_dict()
genre_occurences
# Since some movies can belong to more than one genre, we create different
# 'genre' columns as follows:
# - all_genres: all the active genres of the movie.
# - genre: randomly sampled from the active genres.
def mark_genres(movies, genres):
def get_random_genre(gs):
= [genre for genre, g in zip(genres, gs) if g==1]
active if len(active) == 0:
return 'Other'
return np.random.choice(active)
def get_all_genres(gs):
= [genre for genre, g in zip(genres, gs) if g==1]
active if len(active) == 0:
return 'Other'
return '-'.join(active)
'genre'] = [
movies[for gs in zip(*[movies[genre] for genre in genres])]
get_random_genre(gs) 'all_genres'] = [
movies[for gs in zip(*[movies[genre] for genre in genres])]
get_all_genres(gs)
mark_genres(movies, genre_cols)
# Create one merged DataFrame containing all the movielens data.
= ratings.merge(movies, on='movie_id').merge(users, on='user_id')
movielens
# Utility to split the data into training and test sets.
def split_dataframe(df, holdout_fraction=0.1):
"""Splits a DataFrame into training and test sets.
Args:
df: a dataframe.
holdout_fraction: fraction of dataframe rows to use in the test set.
Returns:
train: dataframe for training
test: dataframe for testing
"""
= df.sample(frac=holdout_fraction, replace=False)
test = df[~df.index.isin(test.index)]
train return train, test
Downloading movielens data...
Done. Dataset contains:
b'943 users\n1682 items\n100000 ratings\n'
I. Exploring the Movielens Data
Before we dive into model building, let’s inspect our MovieLens dataset. It is usually helpful to understand the statistics of the dataset.
Users
We start by printing some basic statistics describing the numeric user features.
users.describe()
age | |
---|---|
count | 943.000 |
mean | 34.052 |
std | 12.193 |
min | 7.000 |
25% | 25.000 |
50% | 31.000 |
75% | 43.000 |
max | 73.000 |
We can also print some basic statistics describing the categorical user features
=[np.object]) users.describe(include
/tmp/ipykernel_326140/2959809221.py:1: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
users.describe(include=[np.object])
user_id | sex | occupation | zip_code | |
---|---|---|---|---|
count | 943 | 943 | 943 | 943 |
unique | 943 | 2 | 21 | 795 |
top | 0 | M | student | 55414 |
freq | 1 | 670 | 196 | 9 |
We can also create histograms to further understand the distribution of the users. We use Altair to create an interactive chart.
# @title Altair visualization code (run this cell)
# The following functions are used to generate interactive Altair charts.
# We will display histograms of the data, sliced by a given attribute.
# Create filters to be used to slice the data.
= alt.selection_multi(fields=["occupation"])
occupation_filter = alt.Chart().mark_bar().encode(
occupation_chart ="count()",
x=alt.Y("occupation:N"),
y=alt.condition(
color
occupation_filter,"occupation:N", scale=alt.Scale(scheme='category20')),
alt.Color("lightgray")),
alt.value(=300, height=300, selection=occupation_filter)
).properties(width
# A function that generates a histogram of filtered data.
def filtered_hist(field, label, filter):
"""Creates a layered chart of histograms.
The first layer (light gray) contains the histogram of the full data, and the
second contains the histogram of the filtered data.
Args:
field: the field for which to generate the histogram.
label: String label of the histogram.
filter: an alt.Selection object to be used to filter the data.
"""
= alt.Chart().mark_bar().encode(
base =alt.X(field, bin=alt.Bin(maxbins=10), title=label),
x="count()",
y
).properties(=300,
width
)return alt.layer(
filter),
base.transform_filter(=alt.value('lightgray'), opacity=alt.value(.7)),
base.encode(color='independent') ).resolve_scale(y
Next, we look at the distribution of ratings per user. Clicking on an occupation in the right chart will filter the data by that occupation. The corresponding histogram is shown in blue, and superimposed with the histogram for the whole data (in light gray). You can use SHIFT+click to select multiple subsets.
What do you observe, and how might this affect the recommendations?
= (
users_ratings
ratings'user_id', as_index=False)
.groupby('rating': ['count', 'mean']})
.agg({
.flatten_cols()='user_id')
.merge(users, on
)
# Create a chart for the count, and one for the mean.
alt.hconcat('rating count', '# ratings / user', occupation_filter),
filtered_hist('rating mean', 'mean user rating', occupation_filter),
filtered_hist(
occupation_chart,=users_ratings) data
--------------------------------------------------------------------------- SchemaValidationError Traceback (most recent call last) File ~/.local/lib/python3.8/site-packages/altair/vegalite/v5/api.py:921, in TopLevelMixin.to_dict(self, validate, format, ignore, context) 916 context["top_level"] = False 918 # TopLevelMixin instance does not necessarily have to_dict defined 919 # but due to how Altair is set up this should hold. 920 # Too complex to type hint right now --> 921 vegalite_spec = super(TopLevelMixin, copy).to_dict( # type: ignore[misc] 922 validate=validate, ignore=ignore, context=dict(context, pre_transform=False) 923 ) 925 # TODO: following entries are added after validation. Should they be validated? 926 if is_top_level: 927 # since this is top-level we add $schema if it's missing File ~/.local/lib/python3.8/site-packages/altair/utils/schemapi.py:983, in SchemaBase.to_dict(self, validate, ignore, context) 976 self.validate(result) 977 except jsonschema.ValidationError as err: 978 # We do not raise `from err` as else the resulting 979 # traceback is very long as it contains part 980 # of the Vega-Lite schema. It would also first 981 # show the less helpful ValidationError instead of 982 # the more user friendly SchemaValidationError --> 983 raise SchemaValidationError(self, err) from None 984 return result SchemaValidationError: `HConcatChart` has no parameter named 'selection' Existing parameter names are: hconcat center description params title autosize config name resolve transform background data padding spacing usermeta bounds datasets See the help for `HConcatChart` to read the full description of these parameters
alt.HConcatChart(...)
Movies
It is also useful to look at information about the movies and their ratings.
= movies.merge(
movies_ratings
ratings'movie_id', as_index=False)
.groupby('rating': ['count', 'mean']})
.agg({
.flatten_cols(),='movie_id')
on
= alt.selection_multi(fields=['genre'])
genre_filter = alt.Chart().mark_bar().encode(
genre_chart ="count()",
x=alt.Y('genre'),
y=alt.condition(
color
genre_filter,"genre:N"),
alt.Color('lightgray'))
alt.value(=300, selection=genre_filter) ).properties(height
/home/vscode/.local/lib/python3.8/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'selection_multi' is deprecated. Use 'selection_point'
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
'title', 'rating count', 'rating mean']]
(movies_ratings[['rating count', ascending=False)
.sort_values(10)) .head(
title | rating count | rating mean | |
---|---|---|---|
49 | Star Wars (1977) | 583 | 4.358 |
257 | Contact (1997) | 509 | 3.804 |
99 | Fargo (1996) | 508 | 4.156 |
180 | Return of the Jedi (1983) | 507 | 4.008 |
293 | Liar Liar (1997) | 485 | 3.157 |
285 | English Patient, The (1996) | 481 | 3.657 |
287 | Scream (1996) | 478 | 3.441 |
0 | Toy Story (1995) | 452 | 3.878 |
299 | Air Force One (1997) | 431 | 3.631 |
120 | Independence Day (ID4) (1996) | 429 | 3.438 |
'title', 'rating count', 'rating mean']]
(movies_ratings[['rating count', lambda x: x > 20)
.mask('rating mean', ascending=False)
.sort_values(10)) .head(
title | rating count | rating mean | |
---|---|---|---|
407 | Close Shave, A (1995) | 112 | 4.491 |
317 | Schindler's List (1993) | 298 | 4.466 |
168 | Wrong Trousers, The (1993) | 118 | 4.466 |
482 | Casablanca (1942) | 243 | 4.457 |
113 | Wallace & Gromit: The Best of Aardman Animatio... | 67 | 4.448 |
63 | Shawshank Redemption, The (1994) | 283 | 4.445 |
602 | Rear Window (1954) | 209 | 4.388 |
11 | Usual Suspects, The (1995) | 267 | 4.386 |
49 | Star Wars (1977) | 583 | 4.358 |
177 | 12 Angry Men (1957) | 125 | 4.344 |
Finally, the last chart shows the distribution of the number of ratings and average rating.
# Display the number of ratings and average rating per movie.
alt.hconcat('rating count', '# ratings / movie', genre_filter),
filtered_hist('rating mean', 'mean movie rating', genre_filter),
filtered_hist(
genre_chart,=movies_ratings) data
--------------------------------------------------------------------------- SchemaValidationError Traceback (most recent call last) File ~/.local/lib/python3.8/site-packages/altair/vegalite/v5/api.py:921, in TopLevelMixin.to_dict(self, validate, format, ignore, context) 916 context["top_level"] = False 918 # TopLevelMixin instance does not necessarily have to_dict defined 919 # but due to how Altair is set up this should hold. 920 # Too complex to type hint right now --> 921 vegalite_spec = super(TopLevelMixin, copy).to_dict( # type: ignore[misc] 922 validate=validate, ignore=ignore, context=dict(context, pre_transform=False) 923 ) 925 # TODO: following entries are added after validation. Should they be validated? 926 if is_top_level: 927 # since this is top-level we add $schema if it's missing File ~/.local/lib/python3.8/site-packages/altair/utils/schemapi.py:983, in SchemaBase.to_dict(self, validate, ignore, context) 976 self.validate(result) 977 except jsonschema.ValidationError as err: 978 # We do not raise `from err` as else the resulting 979 # traceback is very long as it contains part 980 # of the Vega-Lite schema. It would also first 981 # show the less helpful ValidationError instead of 982 # the more user friendly SchemaValidationError --> 983 raise SchemaValidationError(self, err) from None 984 return result SchemaValidationError: `HConcatChart` has no parameter named 'selection' Existing parameter names are: hconcat center description params title autosize config name resolve transform background data padding spacing usermeta bounds datasets See the help for `HConcatChart` to read the full description of these parameters
alt.HConcatChart(...)
II. Preliminaries
Our goal is to factorize the ratings matrix \(A\) into the product of a user embedding matrix \(U\) and movie embedding matrix \(V\), such that \(A \approx UV^\top\) with \(U = \begin{bmatrix} u_{1} \\ \hline \vdots \\ \hline u_{N} \end{bmatrix}\) and \(V = \begin{bmatrix} v_{1} \\ \hline \vdots \\ \hline v_{M} \end{bmatrix}\).
Here - \(N\) is the number of users, - \(M\) is the number of movies, - \(A_{ij}\) is the rating of the \(j\)th movies by the \(i\)th user, - each row \(U_i\) is a \(d\)-dimensional vector (embedding) representing user \(i\), - each row \(V_j\) is a \(d\)-dimensional vector (embedding) representing movie \(j\), - the prediction of the model for the \((i, j)\) pair is the dot product \(\langle U_i, V_j \rangle\).
Sparse Representation of the Rating Matrix
The rating matrix could be very large and, in general, most of the entries are unobserved, since a given user will only rate a small subset of movies. For effcient representation, we will use a tf.SparseTensor. A SparseTensor
uses three tensors to represent the matrix: tf.SparseTensor(indices, values, dense_shape)
represents a tensor, where a value \(A_{ij} = a\) is encoded by setting indices[k] = [i, j]
and values[k] = a
. The last tensor dense_shape
is used to specify the shape of the full underlying matrix.
Toy example
Assume we have \(2\) users and \(4\) movies. Our toy ratings dataframe has three ratings,
user_id | movie_id | rating |
---|---|---|
0 | 0 | 5.0 |
0 | 1 | 3.0 |
1 | 3 | 1.0 |
The corresponding rating matrix is
\[ A = \begin{bmatrix} 5.0 & 3.0 & 0 & 0 \\ 0 & 0 & 0 & 1.0 \end{bmatrix} \]
And the SparseTensor representation is,
SparseTensor(=[[0, 0], [0, 1], [1,3]],
indices=[5.0, 3.0, 1.0],
values=[2, 4]) dense_shape
Exercise 1: Build a tf.SparseTensor representation of the Rating Matrix.
In this exercise, we’ll write a function that maps from our ratings
DataFrame to a tf.SparseTensor
.
Hint: you can select the values of a given column of a Dataframe df
using df['column_name'].values
.
def build_rating_sparse_tensor(ratings_df):
"""
Args:
ratings_df: a pd.DataFrame with `user_id`, `movie_id` and `rating` columns.
Returns:
A tf.SparseTensor representing the ratings matrix.
"""
# ========================= Complete this section ============================
# indices =
# values =
# ============================================================================
return tf.SparseTensor(
=indices,
indices=values,
values=[users.shape[0], movies.shape[0]]) dense_shape
#@title Solution
def build_rating_sparse_tensor(ratings_df):
"""
Args:
ratings_df: a pd.DataFrame with `user_id`, `movie_id` and `rating` columns.
Returns:
a tf.SparseTensor representing the ratings matrix.
"""
= ratings_df[['user_id', 'movie_id']].values
indices = ratings_df['rating'].values
values return tf.SparseTensor(
=indices,
indices=values,
values=[users.shape[0], movies.shape[0]]) dense_shape
Calculating the error
The model approximates the ratings matrix \(A\) by a low-rank product \(UV^\top\). We need a way to measure the approximation error. We’ll start by using the Mean Squared Error of observed entries only (we will revisit this later). It is defined as
\[ \begin{align*} \text{MSE}(A, UV^\top) &= \frac{1}{|\Omega|}\sum_{(i, j) \in\Omega}{( A_{ij} - (UV^\top)_{ij})^2} \\ &= \frac{1}{|\Omega|}\sum_{(i, j) \in\Omega}{( A_{ij} - \langle U_i, V_j\rangle)^2} \end{align*} \] where \(\Omega\) is the set of observed ratings, and \(|\Omega|\) is the cardinality of \(\Omega\).
Exercise 2: Mean Squared Error
Write a TensorFlow function that takes a sparse rating matrix \(A\) and the two embedding matrices \(U, V\) and returns the mean squared error \(\text{MSE}(A, UV^\top)\).
Hints: * in this section, we only consider observed entries when calculating the loss. * a SparseTensor
sp_x
is a tuple of three Tensors: sp_x.indices
, sp_x.values
and sp_x.dense_shape
. * you may find tf.gather_nd
and tf.losses.mean_squared_error
helpful.
def sparse_mean_square_error(sparse_ratings, user_embeddings, movie_embeddings):
"""
Args:
sparse_ratings: A SparseTensor rating matrix, of dense_shape [N, M]
user_embeddings: A dense Tensor U of shape [N, k] where k is the embedding
dimension, such that U_i is the embedding of user i.
movie_embeddings: A dense Tensor V of shape [M, k] where k is the embedding
dimension, such that V_j is the embedding of movie j.
Returns:
A scalar Tensor representing the MSE between the true ratings and the
model's predictions.
"""
# ========================= Complete this section ============================
# loss =
# ============================================================================
return loss
#@title Solution
def sparse_mean_square_error(sparse_ratings, user_embeddings, movie_embeddings):
"""
Args:
sparse_ratings: A SparseTensor rating matrix, of dense_shape [N, M]
user_embeddings: A dense Tensor U of shape [N, k] where k is the embedding
dimension, such that U_i is the embedding of user i.
movie_embeddings: A dense Tensor V of shape [M, k] where k is the embedding
dimension, such that V_j is the embedding of movie j.
Returns:
A scalar Tensor representing the MSE between the true ratings and the
model's predictions.
"""
= tf.gather_nd(
predictions =True),
tf.matmul(user_embeddings, movie_embeddings, transpose_b
sparse_ratings.indices)= tf.losses.mean_squared_error(sparse_ratings.values, predictions)
loss return loss
Note: One approach is to compute the full prediction matrix \(UV^\top\), then gather the entries corresponding to the observed pairs. The memory cost of this approach is \(O(NM)\). For the MovieLens dataset, this is fine, as the dense \(N \times M\) matrix is small enough to fit in memory (\(N = 943\), \(M = 1682\)).
Another approach (given in the alternate solution below) is to only gather the embeddings of the observed pairs, then compute their dot products. The memory cost is \(O(|\Omega| d)\) where \(d\) is the embedding dimension. In our case, \(|\Omega| = 10^5\), and the embedding dimension is on the order of \(10\), so the memory cost of both methods is comparable. But when the number of users or movies is much larger, the first approach becomes infeasible.
#@title Alternate Solution
def sparse_mean_square_error(sparse_ratings, user_embeddings, movie_embeddings):
"""
Args:
sparse_ratings: A SparseTensor rating matrix, of dense_shape [N, M]
user_embeddings: A dense Tensor U of shape [N, k] where k is the embedding
dimension, such that U_i is the embedding of user i.
movie_embeddings: A dense Tensor V of shape [M, k] where k is the embedding
dimension, such that V_j is the embedding of movie j.
Returns:
A scalar Tensor representing the MSE between the true ratings and the
model's predictions.
"""
= tf.reduce_sum(
predictions 0]) *
tf.gather(user_embeddings, sparse_ratings.indices[:, 1]),
tf.gather(movie_embeddings, sparse_ratings.indices[:, =1)
axis= tf.losses.mean_squared_error(sparse_ratings.values, predictions)
loss return loss
Exercise 3 (Optional): adding your own ratings to the data set
You have the option to add your own ratings to the data set. If you choose to do so, you will be able to see recommendations for yourself.
Start by checking the box below. Running the next cell will authenticate you to your google Drive account, and create a spreadsheet, that contains all movie titles in column ‘A’. Follow the link to the spreadsheet and take 3 minutes to rate some of the movies. Your ratings should be entered in column ‘B’.
= True #@param {type:"boolean"} USER_RATINGS
# @title Run to create a spreadsheet, then use it to enter your ratings.
# Authenticate user.
if USER_RATINGS:
auth.authenticate_user()= gspread.authorize(GoogleCredentials.get_application_default())
gc # Create the spreadsheet and print a link to it.
try:
= gc.open('MovieLens-test')
sh except(gspread.SpreadsheetNotFound):
= gc.create('MovieLens-test')
sh
= sh.sheet1
worksheet = movies['title'].values
titles = worksheet.range(1, 1, len(titles), 1)
cell_list for cell, title in zip(cell_list, titles):
= title
cell.value
worksheet.update_cells(cell_list)print("Link to the spreadsheet: "
"https://docs.google.com/spreadsheets/d/{}/edit".format(sh.id))
Run the next cell to load your ratings and add them to the main ratings
DataFrame.
# @title Run to load your ratings.
# Load the ratings from the spreadsheet and create a DataFrame.
if USER_RATINGS:
= pd.DataFrame.from_records(worksheet.get_all_values()).reset_index()
my_ratings = my_ratings[my_ratings[1] != '']
my_ratings = pd.DataFrame({
my_ratings 'user_id': "943",
'movie_id': list(map(str, my_ratings['index'])),
'rating': list(map(float, my_ratings[1])),
})# Remove previous ratings.
= ratings[ratings.user_id != "943"]
ratings # Add new ratings.
= ratings.append(my_ratings, ignore_index=True)
ratings # Add new user to the users DataFrame.
if users.shape[0] == 943:
= users.append(users.iloc[942], ignore_index=True)
users "user_id"][943] = "943"
users[print("Added your %d ratings; you have great taste!" % len(my_ratings))
=="943"].merge(movies[['movie_id', 'title']]) ratings[ratings.user_id
III. Training a Matrix Factorization model
CFModel (Collaborative Filtering Model) helper class
This is a simple class to train a matrix factorization model using stochastic gradient descent.
The class constructor takes - the user embeddings U (a tf.Variable
). - the movie embeddings V, (a tf.Variable
). - a loss to optimize (a tf.Tensor
). - an optional list of metrics dictionaries, each mapping a string (the name of the metric) to a tensor. These are evaluated and plotted during training (e.g. training error and test error).
After training, one can access the trained embeddings using the model.embeddings
dictionary.
Example usage:
U_var = ...
V_var = ...
loss = ...
model = CFModel(U_var, V_var, loss)
model.train(iterations=100, learning_rate=1.0)
user_embeddings = model.embeddings['user_id']
movie_embeddings = model.embeddings['movie_id']
# @title CFModel helper class (run this cell)
class CFModel(object):
"""Simple class that represents a collaborative filtering model"""
def __init__(self, embedding_vars, loss, metrics=None):
"""Initializes a CFModel.
Args:
embedding_vars: A dictionary of tf.Variables.
loss: A float Tensor. The loss to optimize.
metrics: optional list of dictionaries of Tensors. The metrics in each
dictionary will be plotted in a separate figure during training.
"""
self._embedding_vars = embedding_vars
self._loss = loss
self._metrics = metrics
self._embeddings = {k: None for k in embedding_vars}
self._session = None
@property
def embeddings(self):
"""The embeddings dictionary."""
return self._embeddings
def train(self, num_iterations=100, learning_rate=1.0, plot_results=True,
=tf.train.GradientDescentOptimizer):
optimizer"""Trains the model.
Args:
iterations: number of iterations to run.
learning_rate: optimizer learning rate.
plot_results: whether to plot the results at the end of training.
optimizer: the optimizer to use. Default to GradientDescentOptimizer.
Returns:
The metrics dictionary evaluated at the last iteration.
"""
with self._loss.graph.as_default():
= optimizer(learning_rate)
opt = opt.minimize(self._loss)
train_op = tf.group(
local_init_op
tf.variables_initializer(opt.variables()),
tf.local_variables_initializer())if self._session is None:
self._session = tf.Session()
with self._session.as_default():
self._session.run(tf.global_variables_initializer())
self._session.run(tf.tables_initializer())
tf.train.start_queue_runners()
with self._session.as_default():
local_init_op.run()= []
iterations = self._metrics or ({},)
metrics = [collections.defaultdict(list) for _ in self._metrics]
metrics_vals
# Train and append results.
for i in range(num_iterations + 1):
= self._session.run((train_op, metrics))
_, results if (i % 10 == 0) or i == num_iterations:
print("\r iteration %d: " % i + ", ".join(
"%s=%f" % (k, v) for r in results for k, v in r.items()]),
[='')
end
iterations.append(i)for metric_val, result in zip(metrics_vals, results):
for k, v in result.items():
metric_val[k].append(v)
for k, v in self._embedding_vars.items():
self._embeddings[k] = v.eval()
if plot_results:
# Plot the metrics.
= len(metrics)+1
num_subplots = plt.figure()
fig *10, 8)
fig.set_size_inches(num_subplotsfor i, metric_vals in enumerate(metrics_vals):
= fig.add_subplot(1, num_subplots, i+1)
ax for k, v in metric_vals.items():
=k)
ax.plot(iterations, v, label1, num_iterations])
ax.set_xlim([
ax.legend()return results
Exercise 4: Build a Matrix Factorization model and train it
Using your sparse_mean_square_error
function, write a function that builds a CFModel
by creating the embedding variables and the train and test losses.
def build_model(ratings, embedding_dim=3, init_stddev=1.):
"""
Args:
ratings: a DataFrame of the ratings
embedding_dim: the dimension of the embedding vectors.
init_stddev: float, the standard deviation of the random initial embeddings.
Returns:
model: a CFModel.
"""
# Split the ratings DataFrame into train and test.
= split_dataframe(ratings)
train_ratings, test_ratings # SparseTensor representation of the train and test datasets.
# ========================= Complete this section ============================
# A_train =
# A_test =
# ============================================================================
# Initialize the embeddings using a normal distribution.
= tf.Variable(tf.random_normal(
U 0], embedding_dim], stddev=init_stddev))
[A_train.dense_shape[= tf.Variable(tf.random_normal(
V 1], embedding_dim], stddev=init_stddev))
[A_train.dense_shape[# ========================= Complete this section ============================
# train_loss =
# test_loss =
# ============================================================================
= {
metrics 'train_error': train_loss,
'test_error': test_loss
}= {
embeddings "user_id": U,
"movie_id": V
}return CFModel(embeddings, train_loss, [metrics])
#@title Solution
def build_model(ratings, embedding_dim=3, init_stddev=1.):
"""
Args:
ratings: a DataFrame of the ratings
embedding_dim: the dimension of the embedding vectors.
init_stddev: float, the standard deviation of the random initial embeddings.
Returns:
model: a CFModel.
"""
# Split the ratings DataFrame into train and test.
= split_dataframe(ratings)
train_ratings, test_ratings # SparseTensor representation of the train and test datasets.
= build_rating_sparse_tensor(train_ratings)
A_train = build_rating_sparse_tensor(test_ratings)
A_test # Initialize the embeddings using a normal distribution.
= tf.Variable(tf.random_normal(
U 0], embedding_dim], stddev=init_stddev))
[A_train.dense_shape[= tf.Variable(tf.random_normal(
V 1], embedding_dim], stddev=init_stddev))
[A_train.dense_shape[= sparse_mean_square_error(A_train, U, V)
train_loss = sparse_mean_square_error(A_test, U, V)
test_loss = {
metrics 'train_error': train_loss,
'test_error': test_loss
}= {
embeddings "user_id": U,
"movie_id": V
}return CFModel(embeddings, train_loss, [metrics])
Great, now it’s time to train the model!
Go ahead and run the next cell, trying different parameters (embedding dimension, learning rate, iterations). The training and test errors are plotted at the end of training. You can inspect these values to validate the hyper-parameters.
Note: by calling model.train
again, the model will continue training starting from the current values of the embeddings.
# Build the CF model and train it.
= build_model(ratings, embedding_dim=30, init_stddev=0.5)
model =1000, learning_rate=10.) model.train(num_iterations
The movie and user embeddings are also displayed in the right figure. When the embedding dimension is greater than 3, the embeddings are projected on the first 3 dimensions. The next section will have a more detailed look at the embeddings.
IV. Inspecting the Embeddings
In this section, we take a closer look at the learned embeddings, by - computing your recommendations - looking at the nearest neighbors of some movies, - looking at the norms of the movie embeddings, - visualizing the embedding in a projected embedding space.
Exercise 5: Write a function that computes the scores of the candidates
We start by writing a function that, given a query embedding \(u \in \mathbb R^d\) and item embeddings \(V \in \mathbb R^{N \times d}\), computes the item scores.
As discussed in the lecture, there are different similarity measures we can use, and these can yield different results. We will compare the following: - dot product: the score of item j is \(\langle u, V_j \rangle\). - cosine: the score of item j is \(\frac{\langle u, V_j \rangle}{\|u\|\|V_j\|}\).
Hints: - you can use np.dot
to compute the product of two np.Arrays. - you can use np.linalg.norm
to compute the norm of a np.Array.
= 'dot'
DOT = 'cosine'
COSINE def compute_scores(query_embedding, item_embeddings, measure=DOT):
"""Computes the scores of the candidates given a query.
Args:
query_embedding: a vector of shape [k], representing the query embedding.
item_embeddings: a matrix of shape [N, k], such that row i is the embedding
of item i.
measure: a string specifying the similarity measure to be used. Can be
either DOT or COSINE.
Returns:
scores: a vector of shape [N], such that scores[i] is the score of item i.
"""
# ========================= Complete this section ============================
# scores =
# ============================================================================
return scores
#@title Solution
= 'dot'
DOT = 'cosine'
COSINE def compute_scores(query_embedding, item_embeddings, measure=DOT):
"""Computes the scores of the candidates given a query.
Args:
query_embedding: a vector of shape [k], representing the query embedding.
item_embeddings: a matrix of shape [N, k], such that row i is the embedding
of item i.
measure: a string specifying the similarity measure to be used. Can be
either DOT or COSINE.
Returns:
scores: a vector of shape [N], such that scores[i] is the score of item i.
"""
= query_embedding
u = item_embeddings
V if measure == COSINE:
= V / np.linalg.norm(V, axis=1, keepdims=True)
V = u / np.linalg.norm(u)
u = u.dot(V.T)
scores return scores
Equipped with this function, we can compute recommendations, where the query embedding can be either a user embedding or a movie embedding.
# @title User recommendations and nearest neighbors (run this cell)
def user_recommendations(model, measure=DOT, exclude_rated=False, k=6):
if USER_RATINGS:
= compute_scores(
scores "user_id"][943], model.embeddings["movie_id"], measure)
model.embeddings[= measure + ' score'
score_key = pd.DataFrame({
df list(scores),
score_key: 'movie_id': movies['movie_id'],
'titles': movies['title'],
'genres': movies['all_genres'],
})if exclude_rated:
# remove movies that are already rated
= ratings[ratings.user_id == "943"]["movie_id"].values
rated_movies = df[df.movie_id.apply(lambda movie_id: movie_id not in rated_movies)]
df =False).head(k))
display.display(df.sort_values([score_key], ascending
def movie_neighbors(model, title_substring, measure=DOT, k=6):
# Search for movie ids that match the given substring.
= movies[movies['title'].str.contains(title_substring)].index.values
ids = movies.iloc[ids]['title'].values
titles if len(titles) == 0:
raise ValueError("Found no movies with title %s" % title_substring)
print("Nearest neighbors of : %s." % titles[0])
if len(titles) > 1:
print("[Found more than one matching movie. Other candidates: {}]".format(
", ".join(titles[1:])))
= ids[0]
movie_id = compute_scores(
scores "movie_id"][movie_id], model.embeddings["movie_id"],
model.embeddings[
measure)= measure + ' score'
score_key = pd.DataFrame({
df list(scores),
score_key: 'titles': movies['title'],
'genres': movies['all_genres']
})=False).head(k)) display.display(df.sort_values([score_key], ascending
Your recommendations
If you chose to input your recommendations, you can run the next cell to generate recommendations for you.
=COSINE, k=5) user_recommendations(model, measure
How do the recommendations look?
Movie Nearest neighbors
Let’s look at the neareast neighbors for some of the movies.
"Aladdin", DOT)
movie_neighbors(model, "Aladdin", COSINE) movie_neighbors(model,
It seems that the quality of learned embeddings may not be very good. This will be addressed in Section V by adding several regularization techniques. First, we will further inspect the embeddings.
Movie Embedding Norm
We can also observe that the recommendations with dot-product and cosine are different: with dot-product, the model tends to recommend popular movies. This can be explained by the fact that in matrix factorization models, the norm of the embedding is often correlated with popularity (popular movies have a larger norm), which makes it more likely to recommend more popular items. We can confirm this hypothesis by sorting the movies by their embedding norm, as done in the next cell.
# @title Embedding Visualization code (run this cell)
def movie_embedding_norm(models):
"""Visualizes the norm and number of ratings of the movie embeddings.
Args:
model: A MFModel object.
"""
if not isinstance(models, list):
= [models]
models = pd.DataFrame({
df 'title': movies['title'],
'genre': movies['genre'],
'num_ratings': movies_ratings['rating count'],
})= []
charts = alt.selection_interval()
brush for i, model in enumerate(models):
= 'norm'+str(i)
norm_key = np.linalg.norm(model.embeddings["movie_id"], axis=1)
df[norm_key] = alt.selection(
nearest type='single', encodings=['x', 'y'], on='mouseover', nearest=True,
='none')
empty= alt.Chart().mark_circle().encode(
base ='num_ratings',
x=norm_key,
y=alt.condition(brush, alt.value('#4c78a8'), alt.value('lightgray'))
color
).properties(=nearest).add_selection(brush)
selection= alt.Chart().mark_text(align='center', dx=5, dy=-5).encode(
text ='num_ratings', y=norm_key,
x=alt.condition(nearest, 'title', alt.value('')))
text
charts.append(alt.layer(base, text))return alt.hconcat(*charts, data=df)
def visualize_movie_embeddings(data, x, y):
= alt.selection(
nearest type='single', encodings=['x', 'y'], on='mouseover', nearest=True,
='none')
empty= alt.Chart().mark_circle().encode(
base =x,
x=y,
y=alt.condition(genre_filter, "genre", alt.value("whitesmoke")),
color
).properties(=600,
width=600,
height=nearest)
selection= alt.Chart().mark_text(align='left', dx=5, dy=-5).encode(
text =x,
x=y,
y=alt.condition(nearest, 'title', alt.value('')))
textreturn alt.hconcat(alt.layer(base, text), genre_chart, data=data)
def tsne_movie_embeddings(model):
"""Visualizes the movie embeddings, projected using t-SNE with Cosine measure.
Args:
model: A MFModel object.
"""
= sklearn.manifold.TSNE(
tsne =2, perplexity=40, metric='cosine', early_exaggeration=10.0,
n_components='pca', verbose=True, n_iter=400)
init
print('Running t-SNE...')
= tsne.fit_transform(model.embeddings["movie_id"])
V_proj 'x'] = V_proj[:, 0]
movies.loc[:,'y'] = V_proj[:, 1]
movies.loc[:,return visualize_movie_embeddings(movies, 'x', 'y')
movie_embedding_norm(model)
Note: Depending on how the model is initialized, you may observe that some niche movies (ones with few ratings) have a high norm, leading to spurious recommendations. This can happen if the embedding of that movie happens to be initialized with a high norm. Then, because the movie has few ratings, it is infrequently updated, and can keep its high norm. This will be alleviated by using regularization.
Try changing the value of the hyper-parameter init_stddev
. One quantity that can be helpful is that the expected norm of a \(d\)-dimensional vector with entries \(\sim \mathcal N(0, \sigma^2)\) is approximatley \(\sigma \sqrt d\).
How does this affect the embedding norm distribution, and the ranking of the top-norm movies?
#@title Solution
= build_model(ratings, embedding_dim=30, init_stddev=0.05)
model_lowinit =1000, learning_rate=10.)
model_lowinit.train(num_iterations"Aladdin", DOT)
movie_neighbors(model_lowinit, "Aladdin", COSINE)
movie_neighbors(model_lowinit, movie_embedding_norm([model, model_lowinit])
Embedding visualization
Since it is hard to visualize embeddings in a higher-dimensional space (when the embedding dimension \(k > 3\)), one approach is to project the embeddings to a lower dimensional space. T-SNE (T-distributed Stochastic Neighbor Embedding) is an algorithm that projects the embeddings while attempting to preserve their pariwise distances. It can be useful for visualization, but one should use it with care. For more information on using t-SNE, see How to Use t-SNE Effectively.
tsne_movie_embeddings(model_lowinit)
You can highlight the embeddings of a given genre by clicking on the genres panel (SHIFT+click to select multiple genres).
We can observe that the embeddings do not seem to have any notable structure, and the embeddings of a given genre are located all over the embedding space. This confirms the poor quality of the learned embeddings. One of the main reasons, which we will address in the next section, is that we only trained the model on observed pairs, and without regularization.
V. Regularization In Matrix Factorization
In the previous section, our loss was defined as the mean squared error on the observed part of the rating matrix. As discussed in the lecture, this can be problematic as the model does not learn how to place the embeddings of irrelevant movies. This phenomenon is known as folding.
We will add regularization terms that will address this issue. We will use two types of regularization: - Regularization of the model parameters. This is a common \(\ell_2\) regularization term on the embedding matrices, given by \(r(U, V) = \frac{1}{N} \sum_i \|U_i\|^2 + \frac{1}{M}\sum_j \|V_j\|^2\). - A global prior that pushes the prediction of any pair towards zero, called the gravity term. This is given by \(g(U, V) = \frac{1}{MN} \sum_{i = 1}^N \sum_{j = 1}^M \langle U_i, V_j \rangle^2\).
The total loss is then given by \[ \frac{1}{|\Omega|}\sum_{(i, j) \in \Omega} (A_{ij} - \langle U_i, V_j\rangle)^2 + \lambda _r r(U, V) + \lambda_g g(U, V) \] where \(\lambda_r\) and \(\lambda_g\) are two regularization coefficients (hyper-parameters).
Exercise 6: Build a regularized Matrix Factorization model and train it
Write a function that builds a regularized model. You are given a function gravity(U, V)
that computes the gravity term given the two embedding matrices \(U\) and \(V\).
def gravity(U, V):
"""Creates a gravity loss given two embedding matrices."""
return 1. / (U.shape[0].value*V.shape[0].value) * tf.reduce_sum(
=True) * tf.matmul(V, V, transpose_a=True))
tf.matmul(U, U, transpose_a
def build_regularized_model(
=3, regularization_coeff=.1, gravity_coeff=1.,
ratings, embedding_dim=0.1):
init_stddev"""
Args:
ratings: the DataFrame of movie ratings.
embedding_dim: The dimension of the embedding space.
regularization_coeff: The regularization coefficient lambda.
gravity_coeff: The gravity regularization coefficient lambda_g.
Returns:
A CFModel object that uses a regularized loss.
"""
# Split the ratings DataFrame into train and test.
= split_dataframe(ratings)
train_ratings, test_ratings # SparseTensor representation of the train and test datasets.
= build_rating_sparse_tensor(train_ratings)
A_train = build_rating_sparse_tensor(test_ratings)
A_test = tf.Variable(tf.random_normal(
U 0], embedding_dim], stddev=init_stddev))
[A_train.dense_shape[= tf.Variable(tf.random_normal(
V 1], embedding_dim], stddev=init_stddev))
[A_train.dense_shape[
# ========================= Complete this section ============================
# error_train =
# error_test =
# gravity_loss =
# regularization_loss =
# ============================================================================
= error_train + regularization_loss + gravity_loss
total_loss = {
losses 'train_error': error_train,
'test_error': error_test,
}= {
loss_components 'observed_loss': error_train,
'regularization_loss': regularization_loss,
'gravity_loss': gravity_loss,
}= {"user_id": U, "movie_id": V}
embeddings
return CFModel(embeddings, total_loss, [losses, loss_components])
# @title Solution
def gravity(U, V):
"""Creates a gravity loss given two embedding matrices."""
return 1. / (U.shape[0].value*V.shape[0].value) * tf.reduce_sum(
=True) * tf.matmul(V, V, transpose_a=True))
tf.matmul(U, U, transpose_a
def build_regularized_model(
=3, regularization_coeff=.1, gravity_coeff=1.,
ratings, embedding_dim=0.1):
init_stddev"""
Args:
ratings: the DataFrame of movie ratings.
embedding_dim: The dimension of the embedding space.
regularization_coeff: The regularization coefficient lambda.
gravity_coeff: The gravity regularization coefficient lambda_g.
Returns:
A CFModel object that uses a regularized loss.
"""
# Split the ratings DataFrame into train and test.
= split_dataframe(ratings)
train_ratings, test_ratings # SparseTensor representation of the train and test datasets.
= build_rating_sparse_tensor(train_ratings)
A_train = build_rating_sparse_tensor(test_ratings)
A_test = tf.Variable(tf.random_normal(
U 0], embedding_dim], stddev=init_stddev))
[A_train.dense_shape[= tf.Variable(tf.random_normal(
V 1], embedding_dim], stddev=init_stddev))
[A_train.dense_shape[
= sparse_mean_square_error(A_train, U, V)
error_train = sparse_mean_square_error(A_test, U, V)
error_test = gravity_coeff * gravity(U, V)
gravity_loss = regularization_coeff * (
regularization_loss *U)/U.shape[0].value + tf.reduce_sum(V*V)/V.shape[0].value)
tf.reduce_sum(U= error_train + regularization_loss + gravity_loss
total_loss = {
losses 'train_error_observed': error_train,
'test_error_observed': error_test,
}= {
loss_components 'observed_loss': error_train,
'regularization_loss': regularization_loss,
'gravity_loss': gravity_loss,
}= {"user_id": U, "movie_id": V}
embeddings
return CFModel(embeddings, total_loss, [losses, loss_components])
It is now time to train the regularized model! You can try different values of the regularization coefficients, and different embedding dimensions.
= build_regularized_model(
reg_model =0.1, gravity_coeff=1.0, embedding_dim=35,
ratings, regularization_coeff=.05)
init_stddev=2000, learning_rate=20.) reg_model.train(num_iterations
Observe that adding the regularization terms results in a higher MSE, both on the training and test set. However, as we will see, the quality of the recommendations improves. This highlights a tension between fitting the observed data and minimizing the regularization terms. Fitting the observed data often emphasizes learning high similarity (between items with many interactions), but a good embedding representation also requires learning low similarity (between items with few or no interactions).
Inspect the results
Let’s see if the results with regularization look better.
=True, k=10) user_recommendations(reg_model, DOT, exclude_rated
Hopefully, these recommendations look better. You can change the similarity measure from COSINE to DOT and observe how this affects the recommendations.
Since the model is likely to recommend items that you rated highly, you have the option to exclude the items you rated, using exclude_rated=True
.
In the following cells, we display the nearest neighbors, the embedding norms, and the t-SNE projection of the movie embeddings.
"Aladdin", DOT)
movie_neighbors(reg_model, "Aladdin", COSINE) movie_neighbors(reg_model,
Here we compare the embedding norms for model
and reg_model
. Selecting a subset of the embeddings will highlight them on both charts simultaneously.
movie_embedding_norm([model, model_lowinit, reg_model])
# Visualize the embeddings
tsne_movie_embeddings(reg_model)
We should observe that the embeddings have a lot more structure than the unregularized case. Try selecting different genres and observe how they tend to form clusters (for example Horror, Animation and Children).
Conclusion
This concludes this section on matrix factorization models. Note that while the scale of the problem is small enough to allow efficient training using SGD, many practical problems need to be trained using more specialized algorithms such as Alternating Least Squares (see tf.contrib.factorization.WALSMatrixFactorization for a TF implementation).
VI. Softmax model
In this section, we will train a simple softmax model that predicts whether a given user has rated a movie.
Note: if you are taking the self-study version of the class, make sure to read through the part of the class covering Softmax training before working on this part.
The model will take as input a feature vector \(x\) representing the list of movies the user has rated. We start from the ratings DataFrame, which we group by user_id.
= (ratings[["user_id", "movie_id"]]
rated_movies "user_id", as_index=False)
.groupby(lambda x: list(x)))
.aggregate( rated_movies.head()
We then create a function that generates an example batch, such that each example contains the following features: - movie_id: A tensor of strings of the movie ids that the user rated. - genre: A tensor of strings of the genres of those movies - year: A tensor of strings of the release year.
#@title Batch generation code (run this cell)
= {
years_dict for movie, year in zip(movies["movie_id"], movies["year"])
movie: year
}= {
genres_dict '-')
movie: genres.split(for movie, genres in zip(movies["movie_id"], movies["all_genres"])
}
def make_batch(ratings, batch_size):
"""Creates a batch of examples.
Args:
ratings: A DataFrame of ratings such that examples["movie_id"] is a list of
movies rated by a user.
batch_size: The batch size.
"""
def pad(x, fill):
return pd.DataFrame.from_dict(x).fillna(fill).values
= []
movie = []
year = []
genre = []
label for movie_ids in ratings["movie_id"].values:
movie.append(movie_ids)for movie_id in movie_ids for x in genres_dict[movie_id]])
genre.append([x for movie_id in movie_ids])
year.append([years_dict[movie_id] int(movie_id) for movie_id in movie_ids])
label.append([= {
features "movie_id": pad(movie, ""),
"year": pad(year, ""),
"genre": pad(genre, ""),
"label": pad(label, -1)
}= (
batch
tf.data.Dataset.from_tensor_slices(features)1000)
.shuffle(
.repeat()
.batch(batch_size)
.make_one_shot_iterator()
.get_next())return batch
def select_random(x):
"""Selectes a random elements from each row of x."""
def to_float(x):
return tf.cast(x, tf.float32)
def to_int(x):
return tf.cast(x, tf.int64)
= tf.shape(x)[0]
batch_size = tf.range(batch_size)
rn = to_float(tf.count_nonzero(x >= 0, axis=1))
nnz = tf.random_uniform([batch_size])
rnd = tf.stack([to_int(rn), to_int(nnz * rnd)], axis=1)
ids return to_int(tf.gather_nd(x, ids))
Loss function
Recall that the softmax model maps the input features \(x\) to a user embedding \(\psi(x) \in \mathbb R^d\), where \(d\) is the embedding dimension. This vector is then multiplied by a movie embedding matrix \(V \in \mathbb R^{m \times d}\) (where \(m\) is the number of movies), and the final output of the model is the softmax of the product \[ \hat p(x) = \text{softmax}(\psi(x) V^\top). \] Given a target label \(y\), if we denote by \(p = 1_y\) a one-hot encoding of this target label, then the loss is the cross-entropy between \(\hat p(x)\) and \(p\).
Exercise 7: Write a loss function for the softmax model.
In this exercise, we will write a function that takes tensors representing the user embeddings \(\psi(x)\), movie embeddings \(V\), target label \(y\), and return the cross-entropy loss.
Hint: You can use the function tf.nn.sparse_softmax_cross_entropy_with_logits
, which takes logits
as input, where logits
refers to the product \(\psi(x) V^\top\).
def softmax_loss(user_embeddings, movie_embeddings, labels):
"""Returns the cross-entropy loss of the softmax model.
Args:
user_embeddings: A tensor of shape [batch_size, embedding_dim].
movie_embeddings: A tensor of shape [num_movies, embedding_dim].
labels: A sparse tensor of dense_shape [batch_size, 1], such that
labels[i] is the target label for example i.
Returns:
The mean cross-entropy loss.
"""
# ========================= Complete this section ============================
# logits =
# loss =
# ============================================================================
return loss
# @title Solution
def softmax_loss(user_embeddings, movie_embeddings, labels):
"""Returns the cross-entropy loss of the softmax model.
Args:
user_embeddings: A tensor of shape [batch_size, embedding_dim].
movie_embeddings: A tensor of shape [num_movies, embedding_dim].
labels: A tensor of [batch_size], such that labels[i] is the target label
for example i.
Returns:
The mean cross-entropy loss.
"""
# Verify that the embddings have compatible dimensions
= user_embeddings.shape[1].value
user_emb_dim = movie_embeddings.shape[1].value
movie_emb_dim if user_emb_dim != movie_emb_dim:
raise ValueError(
"The user embedding dimension %d should match the movie embedding "
"dimension % d" % (user_emb_dim, movie_emb_dim))
= tf.matmul(user_embeddings, movie_embeddings, transpose_b=True)
logits = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
loss =logits, labels=labels))
logitsreturn loss
Exercise 8: Build a softmax model, train it, and inspect its embeddings.
We are now ready to build a softmax CFModel. Complete the build_softmax_model
function in the next cell. The architecture of the model is defined in the function create_user_embeddings
and illustrated in the figure below. The input embeddings (movie_id, genre and year) are concatenated to form the input layer, then we have hidden layers with dimensions specified by the hidden_dims
argument. Finally, the last hidden layer is multiplied by the movie embeddings to obtain the logits layer. For the target label, we will use a randomly-sampled movie_id from the list of movies the user rated.
Complete the function below by creating the feature columns and embedding columns, then creating the loss tensors both for the train and test sets (using the softmax_loss
function of the previous exercise).
def build_softmax_model(rated_movies, embedding_cols, hidden_dims):
"""Builds a Softmax model for MovieLens.
Args:
rated_movies: DataFrame of traing examples.
embedding_cols: A dictionary mapping feature names (string) to embedding
column objects. This will be used in tf.feature_column.input_layer() to
create the input layer.
hidden_dims: int list of the dimensions of the hidden layers.
Returns:
A CFModel object.
"""
def create_network(features):
"""Maps input features dictionary to user embeddings.
Args:
features: A dictionary of input string tensors.
Returns:
outputs: A tensor of shape [batch_size, embedding_dim].
"""
# Create a bag-of-words embedding for each sparse feature.
= tf.feature_column.input_layer(features, embedding_cols)
inputs # Hidden layers.
= inputs.shape[1].value
input_dim for i, output_dim in enumerate(hidden_dims):
= tf.get_variable(
w "hidden%d_w_" % i, shape=[input_dim, output_dim],
=tf.truncated_normal_initializer(
initializer=1./np.sqrt(output_dim))) / 10.
stddev= tf.matmul(inputs, w)
outputs = output_dim
input_dim = outputs
inputs return outputs
= split_dataframe(rated_movies)
train_rated_movies, test_rated_movies = make_batch(train_rated_movies, 200)
train_batch = make_batch(test_rated_movies, 100)
test_batch
with tf.variable_scope("model", reuse=False):
# Train
= create_network(train_batch)
train_user_embeddings = select_random(train_batch["label"])
train_labels with tf.variable_scope("model", reuse=True):
# Test
= create_network(test_batch)
test_user_embeddings = select_random(test_batch["label"])
test_labels = tf.get_variable(
movie_embeddings "input_layer/movie_id_embedding/embedding_weights")
# ========================= Complete this section ============================
# train_loss =
# test_loss =
# test_precision_at_10 =
# ============================================================================
= (
metrics "train_loss": train_loss, "test_loss": test_loss},
{"test_precision_at_10": test_precision_at_10}
{
)= {"movie_id": movie_embeddings}
embeddings return CFModel(embeddings, train_loss, metrics)
# @title Solution
def build_softmax_model(rated_movies, embedding_cols, hidden_dims):
"""Builds a Softmax model for MovieLens.
Args:
rated_movies: DataFrame of traing examples.
embedding_cols: A dictionary mapping feature names (string) to embedding
column objects. This will be used in tf.feature_column.input_layer() to
create the input layer.
hidden_dims: int list of the dimensions of the hidden layers.
Returns:
A CFModel object.
"""
def create_network(features):
"""Maps input features dictionary to user embeddings.
Args:
features: A dictionary of input string tensors.
Returns:
outputs: A tensor of shape [batch_size, embedding_dim].
"""
# Create a bag-of-words embedding for each sparse feature.
= tf.feature_column.input_layer(features, embedding_cols)
inputs # Hidden layers.
= inputs.shape[1].value
input_dim for i, output_dim in enumerate(hidden_dims):
= tf.get_variable(
w "hidden%d_w_" % i, shape=[input_dim, output_dim],
=tf.truncated_normal_initializer(
initializer=1./np.sqrt(output_dim))) / 10.
stddev= tf.matmul(inputs, w)
outputs = output_dim
input_dim = outputs
inputs return outputs
= split_dataframe(rated_movies)
train_rated_movies, test_rated_movies = make_batch(train_rated_movies, 200)
train_batch = make_batch(test_rated_movies, 100)
test_batch
with tf.variable_scope("model", reuse=False):
# Train
= create_network(train_batch)
train_user_embeddings = select_random(train_batch["label"])
train_labels with tf.variable_scope("model", reuse=True):
# Test
= create_network(test_batch)
test_user_embeddings = select_random(test_batch["label"])
test_labels = tf.get_variable(
movie_embeddings "input_layer/movie_id_embedding/embedding_weights")
= softmax_loss(
test_loss
test_user_embeddings, movie_embeddings, test_labels)= softmax_loss(
train_loss
train_user_embeddings, movie_embeddings, train_labels)= tf.metrics.precision_at_k(
_, test_precision_at_10 =test_labels,
labels=tf.matmul(test_user_embeddings, movie_embeddings, transpose_b=True),
predictions=10)
k
= (
metrics "train_loss": train_loss, "test_loss": test_loss},
{"test_precision_at_10": test_precision_at_10}
{
)= {"movie_id": movie_embeddings}
embeddings return CFModel(embeddings, train_loss, metrics)
Train the Softmax model
We are now ready to train the softmax model. You can set the following hyperparameters: - learning rate - number of iterations. Note: you can run softmax_model.train()
again to continue training the model from its current state. - input embedding dimensions (the input_dims
argument) - number of hidden layers and size of each layer (the hidden_dims
argument)
Note: since our input features are string-valued (movie_id, genre, and year), we need to map them to integer ids. This is done using tf.feature_column.categorical_column_with_vocabulary_list
, which takes a vocabulary list specifying all the values the feature can take. Then each id is mapped to an embedding vector using tf.feature_column.embedding_column
.
# Create feature embedding columns
def make_embedding_col(key, embedding_dim):
= tf.feature_column.categorical_column_with_vocabulary_list(
categorical_col =key, vocabulary_list=list(set(movies[key].values)), num_oov_buckets=0)
keyreturn tf.feature_column.embedding_column(
=categorical_col, dimension=embedding_dim,
categorical_column# default initializer: trancated normal with stddev=1/sqrt(dimension)
='mean')
combiner
with tf.Graph().as_default():
= build_softmax_model(
softmax_model
rated_movies,=[
embedding_cols"movie_id", 35),
make_embedding_col("genre", 3),
make_embedding_col("year", 2),
make_embedding_col(
],=[35])
hidden_dims
softmax_model.train(=8., num_iterations=3000, optimizer=tf.train.AdagradOptimizer) learning_rate
Inspect the embeddings
We can inspect the movie embeddings as we did for the previous models. Note that in this case, the movie embeddings are used at the same time as input embeddings (for the bag of words representation of the user history), and as softmax weights.
"Aladdin", DOT)
movie_neighbors(softmax_model, "Aladdin", COSINE) movie_neighbors(softmax_model,
movie_embedding_norm([reg_model, softmax_model])
tsne_movie_embeddings(softmax_model)
Congratulations!
You have completed this Colab notebook.
If you would like to further explore these models, we encourage you to try different hyperparameters and observe how this affects the quality of the model and the structure of the embedding space. Here are some suggestions: - Change the embedding dimension. - In the softmax model: change the number of hidden layers, and the input features. For example, you can try a model with no hidden layers, and only the movie ids as inputs. - Using other similarity measures: In this Colab notebook, we used dot product \(d(u, V_j) = \langle u, V_j \rangle\) and cosine \(d(u, V_j) = \frac{\langle u, V_j \rangle}{\|u\|\|V_j\|}\), and discussed how the norms of the embeddings affect the recommendations. You can also try other variants which apply a transformation to the norm, for example \(d(u, V_j) = \frac{\langle u, V_j \rangle}{\|V_j\|^\alpha}\).