TensorFlow 推荐系统
import tensorflow_datasets as tfds
import tensorflow_recommenders as tfrs
# Load data on movie ratings.
ratings = tfds.load("movielens/100k-ratings", split="train")
movies = tfds.load("movielens/100k-movies", split="train")
# Build flexible representation models.
user_model = tf.keras.Sequential([...])
movie_model = tf.keras.Sequential([...])
# Define your objectives.
task = tfrs.tasks.Retrieval(metrics=tfrs.metrics.FactorizedTopK(
movies.batch(128).map(movie_model)
)
)
# Create a retrieval model.
model = MovielensModel(user_model, movie_model, task)
model.compile(optimizer=tf.keras.optimizers.Adagrad(0.5))
# Train.
model.fit(ratings.batch(4096), epochs=3)
# Set up retrieval using trained representations.
index = tfrs.layers.ann.BruteForce(model.user_model)
index.index_from_dataset(
movies.batch(100).map(lambda title: (title, model.movie_model(title)))
)
# Get recommendations.
_, titles = index(np.array(["42"]))
print(f"Recommendations for user 42: {titles[0, :3]}")
TensorFlow Recommenders (TFRS) 是一个用于构建推荐系统模型的库。
它有助于构建推荐系统的整个工作流程:数据准备、模型制定、训练、评估和部署。
它基于 Keras 构建,旨在提供平缓的学习曲线,同时仍然为您提供构建复杂模型的灵活性。
TFRS 使您可以TFRS 是开源的,可在 GitHub 上获取。
要了解更多信息,请参阅 关于如何构建电影推荐系统的教程,或查看 API 文档 以获取 API 参考。
它有助于构建推荐系统的整个工作流程:数据准备、模型制定、训练、评估和部署。
它基于 Keras 构建,旨在提供平缓的学习曲线,同时仍然为您提供构建复杂模型的灵活性。
TFRS 使您可以TFRS 是开源的,可在 GitHub 上获取。
要了解更多信息,请参阅 关于如何构建电影推荐系统的教程,或查看 API 文档 以获取 API 参考。