This video introduces TensorFlow Recommenders, a library built on TensorFlow 2 and Keras for building recommendation systems, demonstrating how to construct a basic two-tower retrieval model using the MovieLens dataset. The tutorial covers data preparation, model architecture with separate user and item towers that output embeddings, training with matrix factorization principles, and evaluation using top-K metrics. The model uses implicit feedback (watched vs. unwatched movies) for retrieval, with predictions generated via brute-force nearest neighbor search on embeddings.
Building Recommender Systems with TensorFlow Recommenders | A Comprehensive Guide
Added:hello welcome back to our video series of building recommendation systems with sensoro my name is we and I'm a developer Advocate at Google in our last video we discussed content-based filing and collaborative filtering in this video we'll be introducing you to the powerful tensorflow recommenders Library which is specifically designed to handle all stages of modern recommendation systems as you may recall in our very first app epe we already touched upon tensorflow recommenders tensorflow recommenders was open sourced on GitHub in September 2020 and is our recommended library for building recommendation systems it is built on top of tensorflow 2 and Caris our goal is to make it an evolving platform flexible enough for conducting academic research and highly scalable for building web scale recommendation systems tensorflow recommenders provides a set of components for building evaluating and deploying recommenders using tensor flow it aims at covering the entire stack from retrieval through ranking to post ranking and ties into the larger tensorflow ecosystem for powering research and production use tfl recommenders already seemlessly integrates with scan and T ranking and we are working on additional integration with other Google products such as TPU t recom recommenders is driven by practical needs and grounded in years of experience of production recommendation system at Google some of these products have a super large set of users and candidates items which means they rely on highly reliable and efficient recommendation systems tflow recommenders incorporates research results on multitask learning feature interaction modeling TPU training and more easy to use is one of the key design goals of tensorflow recommenders tensorflow recommenders is built on top of tensorflow 2 and caras and developed with ease of use in mind in tensorflow recommenders model building follows the caras convention and it's down with composition of caras layers model fitting can be done with caros or estimator we also provide detailed extensive tutorials on tensorflow.org recommenders to help you get started with tensorflow recommenders in our very first episode we highlighted that modern recommenders usually have multiple components to do retrieval ranking and post ranking the retrieval stage is responsible for selecting an initial set of hundreds of candidates from all possible candidates the main objective for retrieval is to efficiently we out all candidates that the user is not interested in because the retrieval model may be dealing with millions of candidates it has to be computationally efficient so now let's first try to build a retrieval model using tensorflow recommenders before we dive into the modeling code let's first talk about the movie lens data set we're going to use movie lens data set is a classic data set from grp lens research group at the University of Minnesota we can use tensorflow data sets API to load movie lens data sets as you see in the first line movie lens contains a set of ratings given to movies by a set of users and here we're showing a preview of the ratings we have movie features like movie title and movie ID we also have user features like user gender and zip code then we have a movie rating we can look at the data set in two ways first we can interpret it as an expressing which movies the users watched and R it and which they did not this is what we call implicit feedback where the users watches tell us which movies they prefer to see and which they rather not see in this video we'll be focusing on implicit feedback here for the retrieval system we treat each movie watched by a user as a positive example and each movie unwatched as a negative example another way to think about it is to interpret the movie ratings as how much the users likes the movies this is what we call explicit feedback where the users are telling us how much they like the movies we'll be focusing on explicit feedback for the ranking system in our next video here we can take a look at all the movie features movie genre movie ID and movie title next we Shuffle and split our data set using tf. dat API the next thing we need is to populate the vocabularies for the movie titles and user IDs this is very important because we need to map the movie titles and user IDs into embeddings we'll be using the vocabularies when we create the embedding layers for the model now we can start building the model the retrieval model we're going to build is simple two Tower model which is a very common model and often serves as a stutter for more sophisticated recommenders the word tower means that the fully connected layers above the input layer follow a tower pattern namely the width of these layers gradually decrease which makes them look like like a stacked Tower as you can see here we have a tower on the left that Maps user features to user embeddings we call this query Tower and we have another tower on the right that Maps item features to item embeddings we call this candidate Tower the output of the model is defined as the dot product of user invading and item eding this simple model actually corresponds to The Matrix factorization model we discussed in our last video If you recall so let's define the Cy Tower first if you happen to have worked on natural language processing using tensor 2 the car pre-processing layer string lookup must look very familiar to you it Maps user ID strings from the user ID vocabulary to integer indexes here we're using the user ID vocabulary we populated in the data preparation step and the embedding layer follows the pre-processing layer you can of course make the model more sophisticated as long as it outputs a vector at the top then is good to go this means that we can include image features and text features here as well anything that makes sense for your application can go here tensorflow recommenders gives you total flexibility to build more Advanced models for the candidate Tower we do the same now we need to Define our matric in our training data we have positive user movie pairs to figure out how good our model is we need to compare the Affinity score that the model calculates for this pair to the scores of all the other possible candidates if the score for the positive pair is higher than for all other candidates our model then is highly accurate to do this we can use the tfrs do matrix. factorized topk matric which helps evaluate how often the true candidate is in the top K candidates for a given query this matri has one required argument the data set of candidates that are used as implicit negatives for evaluation we can then Define our training laws based on the metric tensor follow recommenders provides a retrieval task object which is a convenience wrapper that bundles together the loss function and Metric computation the task itself is a Caris layer that takes the query and the candidate embeddings as arguments and Returns the computed loss now we have all the pieces together and we can wire them up into a model tlow recommenders exposes a base model cost tfrs do models do model which streamlines building models all we need to do is set up the components in the init method and implements the compute loss method and the base model will take care of creating the training Loop to fit our model so in the unit method we are taking the mo movie model user model and task object we defined just now in the compute loose method we use the task object to compute the lock and here we are done building the retriever model we can then compile the model and prepare the training and test the data sets then we run the familiar fit method you can see the loss force and the set of topk retrieval Matrix are updated this Matrix tell us whether the true positive in the topk retrieved items from the entire candidate set for example a top five categorical accuracy matric of 02 would tell us that on average the true positive is in the top five retriev the items 20% of the time after training we can evaluate test set performance if you are happy with the model performance we can use the trend model to make predictions to do that we use tfrs do layers. factorized top k.
boot Force layer to build an index first boot Force basically means we do an exhaustive search on the neighbors of an embedding Vector now we can make an query we pass in a user ID 42 in this case and our model recommends three movies for this user Bridges of Madison County father of bride part two and Rudy here we're using the Brute Force approach to retrieve interesting movies this works okay for our small data set but might be very slow for large candidate pools to speed up embedding nearest neighbor search we have another tool called scan we'll be discussing how to do efficient retrieval with scan in a future episode to deploy the model into production system you can export the trend model using save method and load it back using load model method the exported model is in save the model format so you could also use TF serving to run inference in your production system so that's it our quick walk through of how to use tensor recommenders to build a complete retrieval system to sum up today we give you an introduction of tensorflow recommenders and an end to endend demo of how to build a two Tower retrieval model using tensorflow recommenders I hope you can appreciate how simple and elegant it is to build SE a model with tens recommenders I also want to point out that tfrs is still very young while we continue to add more features your feedback is incredibly important when deciding our future directions if there's anything you need for your applications definitely let last know by filing feature request at our GitHub repository in our next episode we'll be going over how to build a ranking model using torer recommenders see you next time [Music]
Up Next

Building a TensorFlow Ranking Model with TF Recommenders
@TensorFlow
27.5K views•2021-07-20

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

Content-Based vs Collaborative Filtering for Recommendation Systems
@TensorFlow
89.1K views•2021-07-06

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science

![The moment we stopped understanding AI [AlexNet]](https://i.ytimg.com/vi_webp/UZDiGooFs54/maxresdefault.webp)





![[ИТ-лекторий] Рекомендательные системы: от простого к сложному и обратно](https://i.ytimg.com/vi/kTWVGOvws8Q/maxresdefault.jpg)




















![[CVPR20 Tutorial] Billion-scale Approximate Nearest Neighbor Search](https://i.ytimg.com/vi_webp/SKrHs03i08Q/maxresdefault.webp)













