Registry / ai-ml / tensorflow-recommenders

tensorflow-recommenders

JSON →
library0.7.7pypypiunverified

TensorFlow Recommenders (TFRS) is a library for building recommender system models using TensorFlow. It helps with the full workflow of building a recommender system: data preparation, model formulation, training, evaluation, and deployment. It's built on Keras and aims to have a gentle learning curve while still giving you the flexibility to build complex models.

pip install tensorflow-recommenders
INSTALL
IMPORT
SIG · TENSORFLOW-RECOMME
T
tensorflow-recommenders
ai-mlpythonv0.7.7
Install
33.3s avg
Import
Disk
1114MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.1MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 33.3s · import 0.000s · 2150.4MB
1114MB installed
● package 1114MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

tfrs
import tensorflow_recommenders as tfrs
import tfrs
The canonical import alias for tensorflow_recommenders is 'tfrs'.
tf
import tensorflow as tf
Standard import for TensorFlow core functionalities.
tfds
import tensorflow_datasets as tfds
Standard import for TensorFlow Datasets, frequently used with TFRS.

This quickstart builds a simple two-tower retrieval model for movie recommendations using the MovieLens 100K dataset. It demonstrates data loading, model definition (user and movie towers), task setup with FactorizedTopK metrics, training, and generating recommendations.

import tensorflow as tf import tensorflow_datasets as tfds import tensorflow_recommenders as tfrs # Load the MovieLens 100K dataset ratings = tfds.load('movielens/100k-ratings', split="train") movies = tfds.load('movielens/100k-movies', split="train") # Prepare data by selecting relevant features ratings = ratings.map(lambda x: {"movie_title": x["movie_title"], "user_id": x["user_id"]}) movies = movies.map(lambda x: x["movie_title"]) # Build vocabularies for user IDs and movie titles user_ids_vocabulary = tf.keras.layers.StringLookup(mask_token=None) user_ids_vocabulary.adapt(ratings.map(lambda x: x["user_id"])) movie_titles_vocabulary = tf.keras.layers.StringLookup(mask_token=None) movie_titles_vocabulary.adapt(movies) # Define user and movie models using Keras Sequential user_model = tf.keras.Sequential([ user_ids_vocabulary, tf.keras.layers.Embedding(user_ids_vocabulary.vocabulary_size(), 32) ]) movie_model = tf.keras.Sequential([ movie_titles_vocabulary, tf.keras.layers.Embedding(movie_titles_vocabulary.vocabulary_size(), 32) ]) # Define the retrieval task with FactorizedTopK metric task = tfrs.tasks.Retrieval( metrics=tfrs.metrics.FactorizedTopK( candidates=movies.batch(128).map(movie_model) ) ) # Create a TFRS model class MovieLensModel(tfrs.Model): def __init__(self, user_model, movie_model): super().__init__() self.movie_model: tf.keras.Model = movie_model self.user_model: tf.keras.Model = user_model self.task: tf.keras.layers.Layer = task def compute_loss(self, features: dict, training=False) -> tf.Tensor: user_embeddings = self.user_model(features["user_id"]) positive_movie_embeddings = self.movie_model(features["movie_title"]) return self.task(user_embeddings, positive_movie_embeddings) # Compile and train the model model = MovieLensModel(user_model, movie_model) model.compile(optimizer=tf.keras.optimizers.Adagrad(0.1)) model.fit(ratings.batch(4096), epochs=3) # Generate recommendations (example for a specific user) index = tfrs.layers.factorized_top_k.BruteForce(model.user_model) index.index_from_dataset( movies.batch(100).map(lambda title: (title, model.movie_model(title))) ) # Example: get recommendations for user with ID '42' _, titles = index(tf.constant(["42"])) print(f"Top 3 recommendations for user '42': {titles[0, :3].numpy().astype(str)}")
Debug
Known issues
breakingThe `tfrs.layers.factorized_top_k.TopK` layer's indexing API changed in v0.6.0. Direct indexing with datasets is no longer supported in the same way.
fix
Use the `index_from_dataset` method for indexing with datasets to ensure correct alignment of embeddings and candidate identifiers.
affects: >=0.6.0
breakingIn v0.7.0, the `tfrs.metrics.FactorizedTopK` constructor parameters `k` was replaced with `ks` (a list of k values) and the `metrics` parameter was removed as it only makes sense with top-k metrics.
fix
Update `FactorizedTopK` instantiation to use `ks=[k1, k2, ...]` instead of `k=k_value`. Remove the `metrics` argument if present.
affects: >=0.7.0
breakingThe `tfrs.tasks.Retrieval` task was updated in v0.7.3 to accept a *list* of factorized metrics, instead of a single optional metric.
fix
Ensure that the `metrics` argument passed to `tfrs.tasks.Retrieval` is a list, even if it contains only one metric. E.g., `metrics=[tfrs.metrics.FactorizedTopK(...)]`.
affects: >=0.7.3
deprecatedThe `batch_size` argument for `tfrs.layers.embedding.TPUEmbedding` is deprecated and no longer required since v0.7.0.
fix
Remove the `batch_size` argument from `tfrs.layers.embedding.TPUEmbedding` constructor calls. The layer now supports dynamic input shapes.
affects: >=0.7.0
gotchaTensorFlow Recommenders, while built on Keras, can have a steep learning curve due to its advanced concepts in recommendation systems and deep integration with TensorFlow.
fix
Start with official tutorials and simple examples, progressively increasing complexity. Focus on understanding core concepts like embeddings, retrieval, and ranking tasks before building complex, production-ready models.
affects: all
Upgrade
Version history
0.7.7latest on PyPI · released Jan 23, 2026
Audit
Dependencies
tensorflow>=2.9.0requiredTFRS is built on TensorFlow and specific versions are pinned in releases. Version 0.7.0 pins to >=2.9.0.
tensorflow-datasetsoptionalCommonly used for loading benchmark datasets like MovieLens in tutorials and examples.
scannoptionalUsed for efficient approximate nearest neighbor search, often integrated with TFRS for retrieval tasks.
Agent activity
27 hits · last 30 days
node
24
Amazon
1
OpenAI (training)
1
Resources
tensorflow-recommenders — pip install tensorflow-recommenders · libregistry