recommenders
TensorFlow Recommenders is a library for building recommender system models using TensorFlow.
Top Related Projects
Google Research
Best Practices on Recommendation Systems
Fast Python Collaborative Filtering for Implicit Feedback Datasets
A Python implementation of LightFM, a hybrid recommendation algorithm.
Deep recommender models using PyTorch.
A unified, comprehensive and efficient recommendation library
Quick Overview
TensorFlow Recommenders is an open-source library for building recommender systems using TensorFlow. It provides a set of tools and algorithms to simplify the development of recommendation models, including features for data preprocessing, model building, and evaluation.
Pros
- Easy integration with TensorFlow ecosystem
- Provides pre-built models and layers for common recommendation tasks
- Supports both retrieval and ranking models
- Includes utilities for dataset preparation and feature engineering
Cons
- Limited documentation and examples compared to more established libraries
- May require a good understanding of TensorFlow for advanced customization
- Performance can be slower compared to specialized recommendation libraries
- Limited support for non-neural recommendation algorithms
Code Examples
- Creating a basic retrieval model:
import tensorflow as tf
import tensorflow_recommenders as tfrs
# Define user and movie models
user_model = tf.keras.Sequential([
tf.keras.layers.StringLookup(vocabulary=unique_user_ids, mask_token=None),
tf.keras.layers.Embedding(len(unique_user_ids) + 1, 32),
])
movie_model = tf.keras.Sequential([
tf.keras.layers.StringLookup(vocabulary=unique_movie_titles, mask_token=None),
tf.keras.layers.Embedding(len(unique_movie_titles) + 1, 32),
])
# Create a retrieval model
model = tfrs.models.Retrieval(user_model, movie_model)
model.compile(optimizer=tf.keras.optimizers.Adagrad(0.1))
- Training a ranking model:
class RankingModel(tfrs.Model):
def __init__(self):
super().__init__()
self.ranking_model = tf.keras.Sequential([
tf.keras.layers.Dense(256, activation="relu"),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(1),
])
self.task = tfrs.tasks.Ranking(
loss=tf.keras.losses.MeanSquaredError(),
metrics=[tf.keras.metrics.RootMeanSquaredError()],
)
def call(self, features):
return self.ranking_model(features)
def compute_loss(self, features, training=False):
labels = features.pop("rating")
predictions = self(features)
return self.task(labels=labels, predictions=predictions)
model = RankingModel()
model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.1))
- Evaluating a model:
metrics = tfrs.metrics.FactorizedTopK(
candidates=movies.batch(128).map(movie_model)
)
model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.1),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=[metrics])
model.evaluate(test_dataset, return_dict=True)
Getting Started
To get started with TensorFlow Recommenders:
- Install the library:
pip install tensorflow-recommenders
- Import the library in your Python script:
import tensorflow as tf
import tensorflow_recommenders as tfrs
-
Prepare your dataset and define your model architecture using the provided tools and layers.
-
Train and evaluate your model using TensorFlow's standard training loops or custom training procedures.
Competitor Comparisons
Google Research
Pros of google-research
- Broader scope, covering various AI/ML research areas beyond just recommender systems
- More frequent updates and contributions from Google researchers
- Includes cutting-edge research implementations and experimental code
Cons of google-research
- Less focused on production-ready recommender systems
- May require more effort to integrate into existing projects
- Documentation and examples might be more research-oriented and less user-friendly
Code Comparison
google-research:
import tensorflow as tf
from google_research.simclr import data_util
from google_research.simclr import model as simclr_model
model = simclr_model.SimCLR(num_classes=10)
tensorflow/recommenders:
import tensorflow as tf
import tensorflow_recommenders as tfrs
model = tfrs.models.DNNRankingModel(
rating_task=tfrs.tasks.Ranking(
loss=tf.keras.losses.MeanSquaredError(),
metrics=[tf.keras.metrics.RootMeanSquaredError()],
)
)
The google-research example shows a more research-oriented approach, while tensorflow/recommenders provides a more streamlined, production-ready implementation for recommender systems.
Best Practices on Recommendation Systems
Pros of recommenders
- Supports multiple frameworks (TensorFlow, PyTorch, PySpark)
- Offers a wider range of algorithms and evaluation metrics
- Provides more comprehensive documentation and examples
Cons of recommenders
- Less integrated with TensorFlow ecosystem
- May have slower performance for TensorFlow-specific tasks
- Requires more setup and configuration for TensorFlow users
Code Comparison
recommenders:
from recommenders.models.deeprec.models.base_model import BaseModel
class MyModel(BaseModel):
def _build_graph(self):
self.user_embeddings = tf.Variable(tf.random_normal([self.n_users, self.embedding_size]))
self.item_embeddings = tf.Variable(tf.random_normal([self.n_items, self.embedding_size]))
tensorflow/recommenders:
import tensorflow_recommenders as tfrs
class MyModel(tfrs.Model):
def __init__(self):
super().__init__()
self.user_model = tf.keras.Sequential([tf.keras.layers.StringLookup(), tf.keras.layers.Embedding(num_users, embedding_dim)])
self.item_model = tf.keras.Sequential([tf.keras.layers.StringLookup(), tf.keras.layers.Embedding(num_items, embedding_dim)])
Fast Python Collaborative Filtering for Implicit Feedback Datasets
Pros of implicit
- Lightweight and focused on collaborative filtering algorithms
- Fast implementation using Cython and OpenMP
- Easier to integrate into existing Python projects
Cons of implicit
- Limited to implicit feedback datasets
- Fewer built-in features compared to TensorFlow Recommenders
- Less flexibility for complex, multi-stage recommendation pipelines
Code comparison
implicit:
model = implicit.als.AlternatingLeastSquares(factors=50)
model.fit(user_item_matrix)
recommendations = model.recommend(user_id, user_item_matrix[user_id])
TensorFlow Recommenders:
model = tfrs.models.BasicModel(
user_model=tf.keras.Sequential(...),
item_model=tf.keras.Sequential(...),
task=tfrs.tasks.Retrieval()
)
model.compile(optimizer=tf.keras.optimizers.Adagrad(0.1))
model.fit(dataset, epochs=5)
Summary
implicit is a specialized library for collaborative filtering with implicit feedback, offering fast performance and easy integration. TensorFlow Recommenders provides a more comprehensive framework for building recommendation systems, leveraging TensorFlow's ecosystem and supporting complex, multi-stage pipelines. The choice between the two depends on the specific requirements of the recommendation task and the desired level of customization and scalability.
A Python implementation of LightFM, a hybrid recommendation algorithm.
Pros of LightFM
- Lightweight and easy to use, with a simpler API
- Supports both explicit and implicit feedback
- Faster training and prediction times for smaller datasets
Cons of LightFM
- Less flexibility for complex recommendation tasks
- Limited integration with deep learning models
- Fewer built-in features for data preprocessing and evaluation
Code Comparison
LightFM:
from lightfm import LightFM
model = LightFM(learning_rate=0.05, loss='warp')
model.fit(interactions, epochs=10)
TensorFlow Recommenders:
import tensorflow_recommenders as tfrs
class MovieLensModel(tfrs.Model):
def __init__(self):
super().__init__()
self.ranking_model = tf.keras.Sequential([...])
def call(self, features):
return self.ranking_model(features)
LightFM offers a more straightforward approach for basic recommendation tasks, while TensorFlow Recommenders provides greater flexibility and integration with the TensorFlow ecosystem for more complex models. LightFM is better suited for smaller datasets and simpler use cases, whereas TensorFlow Recommenders excels in scenarios requiring deep learning capabilities and scalability for large-scale recommendation systems.
Deep recommender models using PyTorch.
Pros of Spotlight
- Lightweight and easy to use, especially for beginners
- Focuses specifically on recommender systems, providing a streamlined experience
- Supports both implicit and explicit feedback models
Cons of Spotlight
- Less active development and community support
- Limited integration with other machine learning ecosystems
- Fewer advanced features and customization options
Code Comparison
Spotlight:
from spotlight.interactions import Interactions
from spotlight.factorization.explicit import ExplicitFactorizationModel
model = ExplicitFactorizationModel(n_iter=1)
model.fit(interactions)
TensorFlow Recommenders:
import tensorflow_recommenders as tfrs
model = tfrs.models.BasicRanking(
user_model=tf.keras.Sequential(...),
item_model=tf.keras.Sequential(...),
task=tfrs.tasks.Ranking()
)
model.compile(optimizer=tf.keras.optimizers.Adagrad(0.1))
Summary
Spotlight is a lightweight, focused library for recommender systems, making it easier for beginners to get started. However, TensorFlow Recommenders offers more advanced features, better integration with the broader TensorFlow ecosystem, and more active development. The choice between the two depends on the specific needs of the project and the user's familiarity with TensorFlow.
A unified, comprehensive and efficient recommendation library
Pros of RecBole
- Offers a wider range of recommendation algorithms and models
- Provides comprehensive evaluation metrics and tools
- Supports multiple deep learning frameworks (PyTorch, TensorFlow, PaddlePaddle)
Cons of RecBole
- Steeper learning curve due to its extensive features
- Less integration with TensorFlow ecosystem
- May require more setup and configuration for specific use cases
Code Comparison
RecBole:
from recbole.quick_start import run_recbole
run_recbole(model='BPR', dataset='ml-100k')
TensorFlow Recommenders:
import tensorflow_recommenders as tfrs
model = tfrs.models.BasicModel(
user_model=tf.keras.Sequential(...),
item_model=tf.keras.Sequential(...),
task=tfrs.tasks.Retrieval()
)
RecBole offers a more straightforward approach for running predefined models, while TensorFlow Recommenders provides more flexibility in model architecture but requires more code to set up. RecBole's simplicity in this aspect can be advantageous for quick experimentation, whereas TensorFlow Recommenders allows for more customization within the TensorFlow ecosystem.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
TensorFlow Recommenders
TensorFlow Recommenders 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.
Installation
Make sure you have TensorFlow 2.x installed, and install from pip
:
pip install tensorflow-recommenders
Documentation
Have a look at our tutorials and API reference.
Quick start
Building a factorization model for the Movielens 100K dataset is very simple (Colab):
from typing import Dict, Text
import tensorflow as tf
import tensorflow_datasets as tfds
import tensorflow_recommenders as tfrs
# Ratings data.
ratings = tfds.load('movielens/100k-ratings', split="train")
# Features of all the available movies.
movies = tfds.load('movielens/100k-movies', split="train")
# Select the basic features.
ratings = ratings.map(lambda x: {
"movie_id": tf.strings.to_number(x["movie_id"]),
"user_id": tf.strings.to_number(x["user_id"])
})
movies = movies.map(lambda x: tf.strings.to_number(x["movie_id"]))
# Build a model.
class Model(tfrs.Model):
def __init__(self):
super().__init__()
# Set up user representation.
self.user_model = tf.keras.layers.Embedding(
input_dim=2000, output_dim=64)
# Set up movie representation.
self.item_model = tf.keras.layers.Embedding(
input_dim=2000, output_dim=64)
# Set up a retrieval task and evaluation metrics over the
# entire dataset of candidates.
self.task = tfrs.tasks.Retrieval(
metrics=tfrs.metrics.FactorizedTopK(
candidates=movies.batch(128).map(self.item_model)
)
)
def compute_loss(self, features: Dict[Text, tf.Tensor], training=False) -> tf.Tensor:
user_embeddings = self.user_model(features["user_id"])
movie_embeddings = self.item_model(features["movie_id"])
return self.task(user_embeddings, movie_embeddings)
model = Model()
model.compile(optimizer=tf.keras.optimizers.Adagrad(0.5))
# Randomly shuffle data and split between train and test.
tf.random.set_seed(42)
shuffled = ratings.shuffle(100_000, seed=42, reshuffle_each_iteration=False)
train = shuffled.take(80_000)
test = shuffled.skip(80_000).take(20_000)
# Train.
model.fit(train.batch(4096), epochs=5)
# Evaluate.
model.evaluate(test.batch(4096), return_dict=True)
Top Related Projects
Google Research
Best Practices on Recommendation Systems
Fast Python Collaborative Filtering for Implicit Feedback Datasets
A Python implementation of LightFM, a hybrid recommendation algorithm.
Deep recommender models using PyTorch.
A unified, comprehensive and efficient recommendation library
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot