Created
January 22, 2021 07:53
-
-
Save jacKlinc/adeb1b6459055eb7d8911acfef931c17 to your computer and use it in GitHub Desktop.
Collaborative filtering model architecture for movie recommendation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastai import * | |
from fastbook import * | |
def create_params(size): | |
""" | |
Pass tensor shape | |
Returns normalised model parameters | |
""" | |
return nn.Parameter(torch.zeros(*size).normal_(0, 0.01)) | |
class DotProductBias(Module): | |
""" | |
Model architecture for collaborative filtering | |
""" | |
def __init__(self, n_users, n_movies, n_factors, y_range=(0, 5.5)): | |
""" | |
Initialises model with parameters | |
:param n_users: number of users | |
:param n_movies: number of movies | |
:param n_factors: number of factors | |
:param y_range: sigmoid limit | |
""" | |
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): | |
""" | |
Applies a forward pass on the dataset passed | |
:param x: data as DataLoaders obj | |
:return: predictions in sigmoid range (tensor) | |
""" | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment