From Amazon to Netflix to Pinterest, recommendation systems are the cornerstone of a majority of the modern-day billion-dollar industries. However, building recommender systems is not a straightforward task.
What if we can build them in a few lines? Dropping the nitty-gritty details and concentrating on implementing algorithms with more ease is what any data scientist would like to get their hands on. Abstraction is a common trait amongst popular machine learning libraries or frameworks like TensorFlow. Now, the team extends its services to recommender systems as well.
Google has introduced TensorFlow Recommenders (TFRS), an open-source TensorFlow package that makes building, evaluating, and serving sophisticated recommender models easy.
AIM Daily XO
Join our editors every weekday evening as they steer you through the most significant news of the day, introduce you to fresh perspectives, and provide unexpected moments of joy
Your newsletter subscriptions are subject to AIM Privacy Policy and Terms and Conditions.
TFRS Highlights
With TFRS, users can
Download our Mobile App
- Build and evaluate flexible candidate nomination models;
- Incorporate item, user, and context information into recommendation models;
- Multi-task models can be trained to optimise multiple recommendation objectives jointly;
- UseTensorFlow Serving to efficiently serve the resulting models
Deep Learning algorithms are the go-to solution to almost all the recommender systems nowadays. Deep learning thrives at devouring tonnes of data and spewing out recommendations with great accuracy. These systems are ubiquitous and have touched many lives in some form or the other. From YouTube to Netflix, the applications have risen multifold.
However, the lack of publicly available details of representative models and data sets has slowed down the research into recommendation systems.
Overview Of TFRS
TensorFlow’s machine learning platform has a comprehensive, flexible ecosystem of tools, libraries and community resources that lets researchers push the state-of-the-art in ML and developers easily build and deploy ML-powered applications.
TensorFlow recommenders are built on TensorFlow 2.x and Keras. The design is modular for users to easily customise individual layers and metrics while also making sure that the individual components work well together. Throughout the design of TFRS, the team has emphasised on flexibility and ease-of-use so that the default settings are sensible; common tasks are intuitive and straightforward to implement and more complex, or custom recommendation tasks are possible.
The TensorFlow team states that their goal is to make it an evolving platform, flexible enough for conducting academic research and highly scalable for building web-scale recommender systems.
For instance, suppose one has to build a movie recommender system. If developers have to use only user ids and movie titles, TensorFlow’s simple two-tower model is very similar to a typical matrix factorisation model. We would need the following:
- For high-dimensional vector representations, a user tower that turns user ids into user embeddings
- A movie tower to turn movie titles into embeddings
- A loss that maximises the predicted user-movie affinity for the observed and minimises for those that did not happen.
TensorFlow’s TFRS and Keras provide a lot of the building blocks to make all the aforementioned processes happen. Let’s take a look at how it works:
# Setting up user and movie representations.
self.user_model = tf.keras.Sequential([
# Converting the raw user ids into contiguous integers by looking them up in a vocabulary.
tf.keras.layers.experimental.preprocessing.StringLookup(
max_tokens=num_unique_users),
# Mapping the result into embedding vectors.
tf.keras.layers.Embedding(num_unique_users, embedding_dim)
TFRS is also offering the BruteForce layer to sanity-check the model’s recommendations. The BruteForce layer is indexed with precomputed representations of candidates, and allows one to retrieve top movies in response to a query by computing the query-candidate score for all possible candidates:
index = tfrs.layers.ann.BruteForce(model.user_model)
Going forward, TensorFlow states that it would expand its capabilities for multi-task learning, feature cross modelling, self-supervised learning, and state-of-the-art efficient approximate nearest neighbours computation.
Getting Started With TFRS
TensorFlow Recommenders is open-source and available on Github.
!pip install tensorflow_recommenders
Code Snippet from TensorFlow :
To start with, let’s prepare data. This data can be accessed from TensorFlow Datasets.
import tensorflow as tf
import tensorflow_datasets as tfds
import tensorflow_recommenders as tfrs
ratings = tfds.load("movie_lens/100k-ratings", split="train")
movies = tfds.load("movie_lens/100k-movies", split="train")
Once both the user and movie models are ready, the objective and its evaluation metrics need to be defined, which can be done using the Retrieval task in TFRS:
self.task = tfrs.tasks.Retrieval(
Get your hands-on TensorFlow Recommender Systems package here.