Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

python - Error training PyTorch model IndexError: index out of range in self

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...