Using side features: feature preprocessing
One of the great advantages of using a deep learning framework to build recommender models is the freedom to build rich, flexible feature representations.
These need to be appropriately transformed in order to be useful in building models:
User and item ids have to be translated into embedding vectors: high-dimensional numerical representations that are adjusted during training to help the model predict its objective better.
Raw text needs to be tokenized (split into smaller parts such as individual words) and translated into embeddings.
Numerical features need to be normalized so that their values lie in a small interval around 0.
The MovieLens dataset
Let's first have a look at what features we can use from the MovieLens dataset:
There are a couple of key features here:
Movie title is useful as a movie identifier.
User id is useful as a user identifier.
Timestamps will allow us to model the effect of time.
The first two are categorical features; timestamps are a continuous feature.
Turning categorical features into embeddings
A categorical feature is a feature that does not express a continuous quantity, but rather takes on one of a set of fixed values.
Most deep learning models express these feature by turning them into high-dimensional vectors. During model training, the value of that vector is adjusted to help the model predict its objective better.For example, suppose that our goal is to predict which user is going to watch which movie. To do that, we represent each user and each movie by an embedding vector. Initially, these embeddings will take on random values - but during training, we will adjust them so that embeddings of users and the movies they watch end up closer together.Taking raw categorical features and turning them into embeddings is normally a two-step process:
Firstly, we need to translate the raw values into a range of contiguous integers, normally by building a mapping (called a "vocabulary") that maps raw values ("Star Wars") to integers (say, 15)
Secondly, we need to take these integers and turn them into embeddings.
Defining the vocabulary
The first step is to define a vocabulary. We can do this easily using Keras preprocessing layers.
The layer itself does not have a vocabulary yet, but we can build it using our data.
Once we have this we can use the layer to translate raw tokens to embedding ids:
Note that the layer's vocabulary includes one (or more!) unknown (or "out of vocabulary", OOV) tokens. This is really handy: it means that the layer can handle categorical values that are not in the vocabulary. In practical terms, this means that the model can continue to learn about and make recommendations even using features that have not been seen during vocabulary construction.
Using feature hashing
We can take this to its logical extreme and rely entirely on feature hashing, with no vocabulary at all. This is implemented in the tf.keras.layers.experimental.preprocessing.Hashing layer.
We can do the lookup as before without the need to build vocabularies:
Defining the embeddings
Now that we have integer ids, we can use the Embedding layer to turn those into embeddings.
An embedding layer has two dimensions: the first dimension tells us how many distinct categories we can embed; the second tells us how large the vector representing each of them can be.
When creating the embedding layer for movie titles, we are going to set the first value to the size of our title vocabulary (or the number of hashing bins). The second is up to us: the larger it is, the higher the capacity of the model, but the slower it is to fit and serve.
We can put the two together into a single layer which takes raw text in and yields embeddings.
Just like that, we can directly get the embeddings for our movie titles:
We can do the same with user embeddings:
Normalizing continuous features
Continuous features also need normalization. For example, the timestamp feature is far too large to be used directly in a deep model
We need to process it before we can use it. While there are many ways in which we can do this, discretization and standardization are two common ones.
Standardization
Standardization rescales features to normalize their range by subtracting the feature's mean and dividing by its standard deviation. It is a common preprocessing transformation.
This can be easily accomplished using the tf.keras.layers.experimental.preprocessing.Normalization layer:
Discretization
Another common transformation is to turn a continuous feature into a number of categorical features. This makes good sense if we have reasons to suspect that a feature's effect is non-continuous.To do this, we first need to establish the boundaries of the buckets we will use for discretization. The easiest way is to identify the minimum and maximum value of the feature, and divide the resulting interval equally:
Given the bucket boundaries we can transform timestamps into embeddings:
Processing text features
We may also want to add text features to our model. Usually, things like product descriptions are free form text, and we can hope that our model can learn to use the information they contain to make better recommendations, especially in a cold-start or long tail scenario.While the MovieLens dataset does not give us rich textual features, we can still use movie titles. This may help us capture the fact that movies with very similar titles are likely to belong to the same series.The first transformation we need to apply to text is tokenization (splitting into constituent words or word-pieces), followed by vocabulary learning, followed by an embedding.
The Keras tf.keras.layers.experimental.preprocessing.TextVectorization layer can do the first two steps for us:
Let's try it out:
Each title is translated into a sequence of tokens, one for each piece we've tokenized.
We can check the learned vocabulary to verify that the layer is using the correct tokenization:
This looks correct: the layer is tokenizing titles into individual words.To finish the processing, we now need to embed the text. Because each title contains multiple words, we will get multiple embeddings for each title. For use in a donwstream model these are usually compressed into a single embedding. Models like RNNs or Transformers are useful here, but averaging all the words' embeddings together is a good starting point.
Putting it all together
With these components in place, we can build a model that does all the preprocessing together.
User model
The full user model may look like the following:
Let's try it out:
Movie model
We can do the same for the movie model:
Let's try it out:
评论