Tensorflow’s New Package Makes Building Recommendation Systems Easier Than Ever

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

Source: TensorFlow

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.

Sign up for The Deep Learning Podcast

by Vijayalakshmi Anandan

The Deep Learning Curve is a technology-based podcast hosted by Vijayalakshmi Anandan - Video Presenter and Podcaster at Analytics India Magazine. This podcast is the narrator's journey of curiosity and discovery in the world of technology.

Ram Sagar
I have a master's degree in Robotics and I write about machine learning advancements.

Our Upcoming Events

24th Mar, 2023 | Webinar
Women-in-Tech: Are you ready for the Techade

27-28th Apr, 2023 I Bangalore
Data Engineering Summit (DES) 2023

23 Jun, 2023 | Bangalore
MachineCon India 2023 [AI100 Awards]

21 Jul, 2023 | New York
MachineCon USA 2023 [AI100 Awards]

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox
MOST POPULAR

Council Post: The Rise of Generative AI and Living Content

In this era of content, the use of technology, such as AI and data analytics, is becoming increasingly important as it can help content creators personalise their content, improve its quality, and reach their target audience with greater efficacy. AI writing has arrived and is here to stay. Once we overcome the initial need to cling to our conventional methods, we can begin to be more receptive to the tremendous opportunities that these technologies present.