A complete guide to Tensorflow recommendations (with Python code) –


Developing comprehensive recommendation systems is a tedious and complicated endeavor for both novices and experts alike. This involves several steps starting with obtaining a dataset, integrating the vectors and, most importantly, the complete coding technique. To avoid the complexity of developing recommender systems, TensorFlow has released an open source package called Tensorflow Recommenders. Here in this article we will discuss the concept behind Tensorflow Recommenders and with its implementation we will see how quickly we can set up a system. The main points to be explored are listed below.

Points to discuss

  1. What are Tensorflow recommenders?
  2. Recovery system
  3. Implement Tensorflow recommendations

Let’s start with the discussions.

Register for our upcoming Masterclass>>

What are Tensorflow recommenders?

TensorFlow Recommenders (TFRS) is an open source TensorFlow package that simplifies the creation, evaluation, and deployment of advanced recommendation models. TFRS, which is based on TensorFlow 2.x, allows us to create and evaluate flexible candidate nomination models, freely include item, user and context information in recommendation models, and more. We can train multitasking models that optimize many recommendation goals at the same time. So, ultimately, by using TensorFlow diffusion, we can effectively serve our resulting models.

Recovery system

Many recommender systems aim to extract a few good recommendations from a pool of millions of applicants. The retrieval step of a referral system addresses the “needle in a haystack” challenge of identifying a shortlist of promising candidates from a large list of applicants. Fortunately, TensorFlow Recommenders simplifies the process by creating two-round recovery models. Such models retrieve data in two stages:

  1. Converting user input to an integration
  2. Identify the best options in the integration space

TensorFlow 2. x and Keras are used to create TFRS, which makes it both familiar and user-friendly. It’s designed to be modular (so we can easily change specific layers and metrics), but it still works as a whole (so individual components work well together).

Looking for a job change? Let us help you.

Implement Tensorflow recommendations

To give you an idea of ​​how to use TensorFlow recommendations, here I present a basic use case based on the official implementation of Tensorflow. We train a basic model for movie recommendations using the MovieLens dataset. This dataset contains information about the movies that a user has watched and the ratings given to those movies.

We’ll use this dataset to train a model that predicts which movies a user will watch and which they won’t. The so-called two-round model is a frequent and efficient design for this type of task which is a neural network with two sub-models which separately train the representations of the questions and of the candidates. The score for a particular query-candidate pair is simply the dot product of the outputs of those two rounds (shown in the animation below).

Two-round analogy

On the queries side, entries can be anything: user IDs, search queries, or timestamps, while on the applicants side, movie titles, descriptions, synopses, and listings main actors. We’ll keep it basic in this example using user IDs for the request round and movie titles for the candidate round.

Now, let’s quickly set up our environment by installing and importing dependencies.

!pip install -q tensorflow-recommenders
!pip install -q --upgrade tensorflow-datasets
import tensorflow_datasets as tfds
import tensorflow_recommenders as tfrs
import numpy as np
import tensorflow as tf
from typing import Dict, Text
import pprint

We will now prepare the dataset extracted from the Tensorflow datasets. The MovieLens dataset contains two files: one is a rating file that contains attributes related to movies and users, and another file is Movies which contains information about a movie. To see how quickly a recommendation can be crafted, we’ll only use user_id and movie_titles for the system.

# ratings data
rating = tfds.load('movielens/100k-ratings', split="train")
# features of all the movies
movies = tfds.load('movielens/100k-movies', split="train")
# limiting the features
rating = rating.map(lambda x:{'movie_title':x['movie_title'],'user_id':x['user_id']})
movies = movies.map(lambda x: x['movie_title'])

To implement the two-tower analogy, we need to create a user tower that will map the user_ids into a high dimensional vector space, in the same way we will create for movie_titles. These embeds will later be used in the Keras embedding layer.

See also

user_id_vocabulary = tf.keras.layers.experimental.preprocessing.StringLookup(mask_token=None)
user_id_vocabulary.adapt(rating.map(lambda x: x['user_id']))
movies_title_vocabulary = tf.keras.layers.experimental.preprocessing.StringLookup(mask_token=None)

Below we define the class which contains the recommender model where two methods are defined: __init __ () and compute_loss (). As part of the __init __ () method, we configure the main components of our model, that is, the user IDs, the movie_titles representation, and the fetch task. Comput_loss is defined for training the model.

class MovieLensModel(tfrs.Model):
  def __init__(
      user_model: tf.keras.Model,
      movie_model: tf.keras.Model,
      task: tfrs.tasks.Retrieval):
    # Set up user and movie representations.
    self.user_model = user_model
    self.movie_model = movie_model
    # Set up a retrieval task.
    self.task = task
  def compute_loss(self, features: Dict[Text, tf.Tensor], training=False) -> tf.Tensor:
    # Define how the loss is computed.
    user_embeddings = self.user_model(features["user_id"])
    movie_embeddings = self.movie_model(features["movie_title"])
    return self.task(user_embeddings, movie_embeddings)

Now we will define the user model and the movie model using the Keras sequential layer and the fetch task using TFRS.

users_model = tf.keras.Sequential([user_id_vocabulary,
movie_model = tf.keras.Sequential([movies_title_vocabulary,                                   tf.keras.layers.Embedding(movies_title_vocabulary.vocab_size(),64)])
task = tfrs.tasks.Retrieval(metrics=tfrs.metrics.FactorizedTopK(

Now let’s create, compile, and train a recovery model.

model = MovieLensModel(users_model,movie_model,task)
model.fit(rating.batch(4096), epochs=3)

To validate the model recommendations, the BruteForce TFRS layer is used. The BruteForce layer is indexed with candidate representations that have already been calculated, allowing us to find the best movies in response to a query by calculating the query-candidate score for all available candidates:

recommends = tfrs.layers.factorized_top_k.BruteForce(model.user_model)
recommends.index_from_dataset(movies.batch(100).map(lambda title: (title, model.movie_model(title))))
id_ = input('Enter the user_id: ')
_, titles = recommends(np.array([str(id_)]))
print('Top recommendation for user',id_,titles[0, :3])

Go out:


In this article, we have followed the four steps to build the movie recommendation system by analyzing the ratings given by the user. We imported the data and sorted the features for simplicity. Next, we built an integration representation using the Keras preprocessing layers. After that, we have defined a class for the TFRS model, a policy for the models, and a recovery task. Finally, we combined the model under the MovieLensModel class, trained and inferred. This article was about how we can start building a recommendation system using SOTA Tensorflow recommendations.

The references:

Join our Discord server. Be part of an engaging online community. Join here.

Subscribe to our newsletter

Receive the latest updates and relevant offers by sharing your email.

Source link

Leave A Reply

Your email address will not be published.