<jupyter_start><jupyter_text>Working with TensorFlow Recommenders: Quickstart Learning Objectives1. Read the data and build vocabularies.2. Define a model.3. Create and train a model.  IntroductionIn this tutorial, you build a simple matrix factorization model using the [MovieLens 100K dataset](https://grouplens.org/datasets/movielens/100k/) with TFRS. You can use this model to recommend movies for a given user.Each learning objective will correspond to a __TODO__ in this student lab notebook -- try to complete this notebook first and then review the [solution notebook](../solutions/quickstart.ipynb).  Import TFRSFirst, install and import TFRS:<jupyter_code># Install required packages.
!pip install -q tensorflow-recommenders
!pip install -q --upgrade tensorflow-datasets<jupyter_output><empty_output><jupyter_text>Note: Please ignore any incompatibility warnings and errors and re-run the above cell before proceeding.<jupyter_code># Import necessary libraries.
from typing import Dict, Text

import numpy as np
import tensorflow as tf

import tensorflow_datasets as tfds
import tensorflow_recommenders as tfrs<jupyter_output><empty_output><jupyter_text>Read the data<jupyter_code># TODO 1
# 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.
# TODO - Your code goes here<jupyter_output>[1mDownloading and preparing dataset 4.70 MiB (download: 4.70 MiB, generated: 32.41 MiB, total: 37.10 MiB) to /home/jupyter/tensorflow_datasets/movielens/100k-ratings/0.1.0...[0m<jupyter_text>Build vocabularies to convert user ids and movie titles into integer indices for embedding layers:<jupyter_code># Build the vocabularies
user_ids_vocabulary = tf.keras.layers.StringLookup(mask_token=None)
user_ids_vocabulary.adapt(ratings.map(lambda x: x["user_id"]))

movie_titles_vocabulary = tf.keras.layers.StringLookup(mask_token=None)
movie_titles_vocabulary.adapt(movies)<jupyter_output>2021-10-27 11:13:41.722881: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)<jupyter_text>Define a modelWe can define a TFRS model by inheriting from `tfrs.Model` and implementing the `compute_loss` method:<jupyter_code>class MovieLensModel(tfrs.Model):
  # We derive from a custom base class to help reduce boilerplate. Under the hood,
  # these are still plain Keras Models.

  def __init__(
      self,
      user_model: tf.keras.Model,
      movie_model: tf.keras.Model,
      task: tfrs.tasks.Retrieval):
    super().__init__()

    # Set up user and movie representations.
    self.user_model = user_model
    self.movie_model = movie_model

    # Set up a retrieval task.
    self.task = task

  def compute_loss(self, features: Dict[Text, tf.Tensor], training=False) -> tf.Tensor:
    # Define how the loss is computed.

    user_embeddings = self.user_model(features["user_id"])
    movie_embeddings = self.movie_model(features["movie_title"])

    return self.task(user_embeddings, movie_embeddings)<jupyter_output><empty_output><jupyter_text>Define the two models and the retrieval task.<jupyter_code># TODO 2
# Define user and movie models.
user_model = tf.keras.Sequential([
    user_ids_vocabulary,
    tf.keras.layers.Embedding(user_ids_vocabulary.vocabulary_size(), 64)
])
movie_model = tf.keras.Sequential([
    movie_titles_vocabulary,
    tf.keras.layers.Embedding(movie_titles_vocabulary.vocabulary_size(), 64)
])

# Define your objectives.
task = # TODO - Your code goes here<jupyter_output><empty_output><jupyter_text>Fit and evaluate it.Create the model, train it, and generate predictions:<jupyter_code># TODO 3
# Create a retrieval model.
model = MovieLensModel(user_model, movie_model, task)
model.compile(optimizer=tf.keras.optimizers.Adagrad(0.5))

# Train for 3 epochs.
# TODO - Your code goes here

# Use brute-force search to set up retrieval using the trained representations.
index = # TODO - Your code goes here

# Get some recommendations.
_, titles = index(np.array(["42"]))
print(f"Top 3 recommendations for user 42: {titles[0, :3]}")<jupyter_output>Epoch 1/3
25/25 [==============================] - 33s 1s/step - factorized_top_k/top_1_categorical_accuracy: 1.0000e-04 - factorized_top_k/top_5_categorical_accuracy: 0.0016 - factorized_top_k/top_10_categorical_accuracy: 0.0047 - factorized_top_k/top_50_categorical_accuracy: 0.0438 - factorized_top_k/top_100_categorical_accuracy: 0.0988 - loss: 33083.5114 - regularization_loss: 0.0000e+00 - total_loss: 33083.5114
Epoch 2/3
25/25 [==============================] - 32s 1s/step - factorized_top_k/top_1_categorical_accuracy: 1.2000e-04 - factorized_top_k/top_5_categorical_accuracy: 0.0047 - factorized_top_k/top_10_categorical_accuracy: 0.0143 - factorized_top_k/top_50_categorical_accuracy: 0.1051 - factorized_top_k/top_100_categorical_accuracy: 0.2094 - loss: 31010.0450 - regularization_loss: 0.0000e+00 - total_loss: 31010.0450
Epoch 3/3
25/25 [==============================] - 31s 1s/step - factorized_top_k/top_1_categorical_accuracy: 4.5000e-04 - factorized_top_k/top_5_categorical_accu[...]
{'fraction_non_alphanumeric': 0.10942599347283548, 'fraction_numerical': 0.04530620080629679, 'mean_word_length': 5.5125, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '94947', 'license': 'Apache-2.0', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1728, 'n_words': 437, 'path': 'courses/machine_learning/deepdive2/introduction_to_tensorflow/labs/quickstart.ipynb', 'repo_name': 'Glairly/introduction_to_tensorflow', 'size': '5209'}