TensorFlow Recommenders 简介 3 使用 TensorFlow 构建推荐系统 Intro to TensorFlow Recommenders
Posted AI架构师易筋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TensorFlow Recommenders 简介 3 使用 TensorFlow 构建推荐系统 Intro to TensorFlow Recommenders相关的知识,希望对你有一定的参考价值。
References
- TensorFlow Recommenders homepage
https://www.tensorflow.org/recommenders - TensorFlow Recommenders repository on GitHub
https://github.com/tensorflow/recommenders - Building a retrieval model using TensorFlow Recommenders
https://www.tensorflow.org/recommenders/examples/basic_retrieval
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)
参考
https://youtu.be/jz0-satrmrA
以上是关于TensorFlow Recommenders 简介 3 使用 TensorFlow 构建推荐系统 Intro to TensorFlow Recommenders的主要内容,如果未能解决你的问题,请参考以下文章
谷歌开源推荐系统库(TensorFlow Recommenders)
TensorFlow Recommenders 迎来更新 — 可扩展检索和特征交互建模
TensorFlow Recommenders - ValueError: Shape must be rank 2 but is rank 3
Deep & Cross Network 6 使用 TensorFlow 构建推荐系统
Eclipse安装Code Recommenders代码联想模糊提示,解决安装Code Recommenders代码提示神器出错。
Eclipse安装Code Recommenders代码联想模糊提示,解决安装Code Recommenders代码提示神器出错。