Tensorflow Recommenders, a TensorFlow library for recommender systems.
Project description
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for tensorflow-recommenders-0.5.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86feaba7d2d6cc5f918c224263da6e72880a2be0f4c35aec1ac5609326a406be |
|
MD5 | 607e00d27c5d0b221b4082e32c12ce72 |
|
BLAKE2b-256 | 4e6e9b890cd84298638dcaf90ee87cf76124420fc7ca675e84b4f6904a542c8b |
Close
Hashes for tensorflow_recommenders-0.5.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1e0b8b617d77a2563c2f0ee5cfda7d7f1099db68999d7f1d151b9e0fdf6b5db |
|
MD5 | c9f7bfe85e8479da011fc0308c25b5ce |
|
BLAKE2b-256 | 38ea7e9c6f241b6b81d9fa49fb69ee371f1275a673dda02938588a5689558f33 |