Forward Function

In Chapter 8 Collaborative Filtering while Implementing it from scratch. In Class DotProduct
forward function How is the parameter x is passed and what data does parameter x contain

class DotProduct(Module):
def __init__(self, n_users, n_movies, n_factors):
    self.user_factors = Embedding(n_users, n_factors)
    self.movie_factors = Embedding(n_movies, n_factors)
    
def forward(self, x):
    users = self.user_factors(x[:,0])
    movies = self.movie_factors(x[:,1])
    print(users,movies)
    return (users * movies).sum(dim=1)

Hey Syed!

In pytorch (and in general in the context of DL) “forward” is usually the equivalent of “_ _call _ _” - meaning it’s the function that gets called when you run the model on some input. model(x) basically calls model.forward(x), and so whenever you see x as an argument in the forward function, it’s always the input. As you can see in the function, it is expected to contain two columns - the first is users and the second is the movies.

1 Like

As a follow-up of this question, can we have a more precise understanding on how forward is called and on what?

  • The body of foward seems to imply that x[:,0] and x[:,1] have the same length, so I’m guessing that x is representing a batch

  • The next lines of codes (in 2022 course) are the following:

model = DotProduct(n_users, n_movies, 50)
learn = Learner(dls, model, loss_func=MSELossFlat())

So I guess that, when we define the learner, there is a mechanism in fastai which recognizes the type of the model, here DotProduct and when automatically applies the .forward(x) method on each batch x it considers. Am I mistaken?

  • Where can I find the doc (or at least, high-level info) on the general behavior of Learner (I didn’t find much explanation concerning these observations in the doc ), including on how the learner will call its different parts? I would be interested with the code as well (very naive question, but I’m still trying to find my way in the fastai galaxy)