import tensorflow as tf import tensorflow_datasets as tfds import tensorflow_recommenders as tfrs import numpy as np
# 模型定义 继承于tfrs.Model # 实现loss 函数 class MovieLensModel(tfrs.Model): # We derive from a custom base class to help reduce boilerplate. Under the hood, # these are still plain Keras Models.
if __name__ == '__main__': # 获取配置 # Ratings data. ratings = tfds.load("movie_lens/100k-ratings", split="train") # Features of all the available movies. movies = tfds.load("movie_lens/100k-movies", split="train") print("movie_id,movie_title,user_gender,user_id,user_rating") for line in ratings.take(3): # print(line) res = [line["movie_id"].numpy(), line["movie_title"].numpy(), line["user_gender"].numpy(), line["user_id"].numpy(), line["user_rating"].numpy()] print(res) print("movie_id,movie_title") for line in movies.take(3): res = [line["movie_id"].numpy(), line["movie_title"].numpy()] print(res)
# 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 for 3 epochs. model.fit(ratings.batch(4096), epochs=3)
# Use brute-force search to set up retrieval using the trained representations. index = tfrs.layers.factorized_top_k.BruteForce(model.user_model) index.index(movies.batch(100).map(model.movie_model), movies)
# Get some recommendations. _, titles = index(np.array(["42"])) print(f"Top 3 recommendations for user 42: {titles[0, :3]}")
1 2 3 4 5
1、tfrs.tasks.Retrieval模型:a retrieval model, retrieving O(thousands) candidates from a corpus of O(millions) candidates. 召回模型 # In this case, our metrics are top-k metrics: given a user and a known # watched movie, how highly would the model rank the true movie out of # all possible movies? 2、tfrs.tasks.Ranking模型:a ranker model, scoring the candidates retrieved by the retrieval model to return a ranked shortlist of a few dozen candidates. 排序模型
(1)函数式API** With the “Functional API”, where you start from Input,you chain layer calls to specify the model’s forward pass,and finally you create your model from inputs and outputs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import tensorflow as tf from tensorflow import keras
inputs = tf.keras.Input(shape=(128,)) # 构建一个输入张量 x = layers.Dense(256, activation='relu')(inputs) x = layers.Dense(512, activation='relu')(x) x = layers.Dense(128, activation='relu')(x) x = layers.Dense(64, activation='relu')(x) predictions = tf.nn.layers.Dense(10, activation='softmax')(x) model = tf.keras.Model(inputs=inputs, outputs=predictions) # 编译模型 model.compile(optimizer=tf.train.RMSPropOptimizer(0.001), loss='categorical_crossentropy', metrics=['accuracy']) model.fit(data, labels, batch_size=32, epochs=5)
(2)模型子类化 需要实现两个方法(init,call) By subclassing the Model class: in that case, you should define your layers in __init__ and you should implement the model’s forward pass in call
defcall(self, inputs, training=None): out = self.conv_initial(inputs) out = self.blocks(out, training=training) out = self.final_bn(out, training=training) out = tf.nn.relu(out) out = self.avg_pool(out) out = self.fc(out) return out
if __name__ == "__main__": model = ResNet([2, 2, 2], 10) model.build(input_shape=(None, 28, 28, 1)) model.summary()