I am new to PyTorch and trying to implement a recommender system.
I am working on my first model (matrix factorization) which i found here:
https://blog.fastforwardlabs.com/2018/04/10/pytorch-for-recommenders-101.html
I have a list of user ids and item ids (refer to movie ids) and the ratings which i pass to the
model as described on the website. I read the files with the pandas function.
training.py:
ratings = pd.read_csv('../data/ratings.csv')
movies = pd.read_csv('../data/movies.csv')
n_users = int(ratings.userId.nunique())
n_items = int(ratings.movieId.nunique())
users = pd.Series.tolist(ratings.userId)
items = pd.Series.tolist(ratings.movieId)
rating_values = pd.Series.tolist(ratings.rating)
model = MatrixFactorization(n_users, n_items, n_factors=20)
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(),
lr=1e-6)
index = 0
for user, item in zip(users, items):
# get user, item and rating data
rating = Variable(torch.FloatTensor([rating_values[index]]))
user = Variable(torch.LongTensor([int(user)]))
item = Variable(torch.LongTensor([int(item)]))
index += 1
# predict rating
prediction = model(user, item)
loss = loss_fn(prediction, rating)
print(loss)
optimizer.zero_grad()
# backpropagate
loss.backward()
# update weights
optimizer.step()
models.py:
class MatrixFactorization(nn.Module):
def __init__(self, n_users, n_items, n_factors=20):
super().__init__()
# create user embeddings
self.user_factors = nn.Embedding(n_users, n_factors,
sparse=True)
# create item embeddings
self.item_factors = nn.Embedding(n_items, n_factors,
sparse=True)
def forward(self, user, item):
# matrix multiplication
return (self.user_factors(user) * self.item_factors(item)).sum(1)
def predict(self, user, item):
return self.forward(user, item)
At the end i get the following error:
Traceback (most recent call last):
File "srcraining.py", line 31, in <module>
prediction = model(user, item)
File "AppDataLocalProgramsPythonPython39libsite-packagesorch
nmodulesmodule.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "srcmodels.py", line 20, in forward
return (self.user_factors(user) * self.item_factors(item)).sum(1)
File "PythonPython39libsite-packagesorch
nmodulesmodule.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "PythonPython39libsite-packagesorch
nmodulessparse.py", line 124, in forward
return F.embedding(
File "PythonPython39libsite-packagesorch
nfunctional.py", line 1852, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
IndexError: index out of range in self
I don't understand what went wrong. Is something wrong with the model or with my inputs?
question from:
https://stackoverflow.com/questions/66062339/error-training-pytorch-model-indexerror-index-out-of-range-in-self