The forward pass on the model (dot product between the user and movie latent factors) would give us the predicted rating. I quickly patched up a
get_ratings function for use or a suggestion towards inference.
You can pass in any number of movies for a single user as shown below or vice versa.
It would also work for equal lengths of movies and users.
def get_ratings(learn, users=tensor(), items=tensor()):
if not isinstance(items, torch.Tensor): items = tensor(items).view(-1)
if not isinstance(users, torch.Tensor): users = tensor(users).view(-1)
if len(items) == 0:
items = torch.arange(learn.model.i_weight.num_embeddings)
if len(users) == 0:
users = torch.arange(learn.model.u_weight.num_embeddings)
dot = learn.u_weight(users)* learn.i_weight(items)
res = dot.sum(1) + learn.u_bias(users).squeeze() + learn.i_bias(items).squeeze()
return torch.sigmoid(res) * (learn.y_range-learn.y_range) + learn.y_range
print('The user/item index may be invalid')
get_ratings(learn, users=242, items=[10, 20, 3])
There may be some broadcasting tricks that I’m missing here as this does not work for multi-user multi-items when they have different lengths. For example,
get_ratings(learn, users=[242, 10], items=[10, 20, 3]) #this would not work
Hope this helps!