I'm following the Retrieval tutorial from the TFRS (TensorFlow Recommenders) library, and I'm getting confused in this part:
class MovielensModel(tfrs.Model):
def __init__(self, user_model, movie_model):
super().__init__()
self.movie_model: tf.keras.Model = movie_model
self.user_model: tf.keras.Model = user_model
self.task: tf.keras.layers.Layer = task
def compute_loss(self, features: Dict[Text, tf.Tensor], training=False) -> tf.Tensor:
# We pick out the user features and pass them into the user model.
user_embeddings = self.user_model(features["user_id"])
# And pick out the movie features and pass them into the movie model,
# getting embeddings back.
positive_movie_embeddings = self.movie_model(features["movie_title"])
# The task computes the loss and the metrics.
return self.task(user_embeddings, positive_movie_embeddings)
Which is followed by this:
model = MovielensModel(user_model, movie_model)
model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.1))
I have a question about these chunks of code:
- when it says
user_embeddings = self.user_model(features["user_id"])(and also (self.movie_model(features["movie_title"])) it seems thatfeaturesis a dictionary, but it's not defined in any part before in the tutorial. I also checked incompute_losssource code here, to see if it's an attribute of that method or something, but I don't find anything either... So my question here is, what isfeatures? How can the code work well, running that that was not defined before? I tried it outside of the class, by running just this:user_model(features["user_id"])and of course it doesn't work saying thatfeaturesis not defined. But why does it work then, when the class is instantiated and later when it's compiled? (second chunk of code above).
Thank you very much!