Training and evaluating a recommendation system model
In this section, we first define an EmbeddingNet model using PyTorch.
Figure 18.5: Steps in building, training, and evaluating an EmbeddingNet model
We train this model on the MovieLens dataset to thereafter predict user ratings for unseen movies. We finally evaluate the trained model on the validation set.
Defining the EmbeddingNet architecture
We define the EmbeddingNet model with the following lines of PyTorch code:
class EmbeddingNet(nn.Module):
def __init__(self, n_users, n_movies,
n_factors=50, embedding_dropout=0.02,
hidden=10, dropouts=0.2):
...
n_last = hidden[-1]
def gen_layers(n_in):
nonlocal hidden, dropouts
for n_out, rate in zip_longest(hidden, dropouts):
yield nn.Linear(n_in, n_out)
yield nn.ReLU()
if rate is not None and rate > 0.:
...