Hey guys, I was trying to follow along Chapter 08 on Collaborative Filtering of the fastbook on Google Colab, and I came across this couple of errors.
Firstly, when we attempt to create an Embedding
layer from scratch, the nn.Parameter
object is on the cpu by default for some reason, which leads to this error, RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
I tried to solve this error by returning nn.Parameter(torch.zeros(*size).normal_(0,0.01)).to("cuda")
from the create_params
function, which then led to the next error, IndexError: list index out of range
.
I’m not sure if I did something wrong, so I’ll put in my code below,
def create_params(size):
return nn.Parameter(torch.zeros(*size).normal_(0,0.01)).to("cuda")
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 = user_factors[x[:,0]]
movies = 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)
Any help would be great!