The matrix factorization model for Retailrocket recommendations
Now let's create a matrix factorization model in Keras:
- Store the number of visitors and items in a variable, as follows:
n_visitors = events.visitorid.nunique() n_items = events.itemid.nunique()
- Set the number of latent factors for embedding to
5
. You may want to try different values to see the impact on the model training:
n_latent_factors = 5
- Import the Input, Embedding, and Flatten layers from the Keras library:
from tensorflow.keras.layers import Input, Embedding, Flatten
- Start with the items – create an input layer for them as follows:
item_input = Input(shape=[1],name='Items')
- Create an Embedding representation layer and then flatten the Embedding layer to get the output in the number of latent dimensions that we set earlier:
item_embed = Embedding(n_items + 1, n_latent_factors, name='ItemsEmbedding')(item_input) item_vec = Flatten(name='ItemsFlatten')(item_embed)
- Similarly...