TensorFlow Recommenders

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]}")
Notebook 中執行
TensorFlow Recommenders (TFRS) 是用於建構推薦系統模型的程式庫。

它能協助您完成建構推薦系統的完整工作流程:資料準備、模型制定、訓練、評估和部署。

它以 Keras 為基礎建構,目標是提供平緩的學習曲線,同時仍保有彈性,讓您能建構複雜模型。

TFRS 讓您能夠TFRS 是開放原始碼,可在 Github 上取得。

如要進一步瞭解,請參閱關於如何建構電影推薦系統的教學課程,或查看 API 文件以取得 API 參考資料。