In the MovieLens example, why doesn't the CollabNN class include biases?

Towards the end of the lesson, we’re introduced to the CollabNN class, which we write in order to show an example of how to use deep learning to solve collaborative filtering problems:

class CollabNN(Module):
    def __init__(self, user_sz, item_sz, y_range=(0,5.5), n_act=100):
        self.user_factors = Embedding(*user_sz)
        self.item_factors = Embedding(*item_sz)
        self.layers = nn.Sequential(
            nn.Linear(user_sz[1]+item_sz[1], n_act),
            nn.ReLU(),
            nn.Linear(n_act, 1))
        self.y_range = y_range
        
    def forward(self, x):
        embs = self.user_factors(x[:,0]),self.item_factors(x[:,1])
        x = self.layers(torch.cat(embs, dim=1))
        return sigmoid_range(x, *self.y_range)

However, I notice that this class doesn’t seem to implement biases, the way that the DotProductBias class did earlier:

class DotProductBias(Module):
    def __init__(self, n_users, n_movies, n_factors, y_range=(0,5.5)):
        self.user_factors = create_params([n_users, n_factors])
        self.user_bias = create_params([n_users])
        self.movie_factors = create_params([n_movies, n_factors])
        self.movie_bias = create_params([n_movies])
        self.y_range = y_range
        
    def forward(self, x):
        users = self.user_factors[x[:,0]]
        movies = self.movie_factors[x[:,1]]
        res = (users*movies).sum(dim=1)
        res += self.user_bias[x[:,0]] + self.movie_bias[x[:,1]]
        return sigmoid_range(res, *self.y_range)

As we can see above, self.user_bias and self.movie_bias are both defined in DotProductBias, but not in CollabNN.

Am I correct in my interpretation of the code and the biases really were left out of CollabNN, or am I missing them somehow? If I’m correct, were they left out intentionally? And if so, what’s the intuition behind leaving them out here?

nn.Linear includes bias by default. Check the PyTorch docs.

:slightly_smiling_face:

2 Likes