close

[Solved] Pytorch: IndexError: index out of range in self. How to solve?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Pytorch: IndexError: index out of range in self. How to solve? in Python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Pytorch: IndexError: index out of range in self. How to solve Error Occurs?

Today I get the following error Pytorch: IndexError: index out of range in self. How to solve? in Python.

How To Solve Pytorch: IndexError: index out of range in self. How to solve Error ?

  1. How To Solve Pytorch: IndexError: index out of range in self. How to solve Error ?

    To Solve Pytorch: IndexError: index out of range in self. How to solve Error Any input less than zero or more than declared input dimension raise this error. Compare your input and the dimension mentioned in torch.nn.Embedding.

  2. Pytorch: IndexError: index out of range in self. How to solve?

    To Solve Pytorch: IndexError: index out of range in self. How to solve Error Any input less than zero or more than declared input dimension raise this error. Compare your input and the dimension mentioned in torch.nn.Embedding.

Solution 1

I think you have messed up with input dimension declared torch.nn.Embedding and with your input. torch.nn.Embedding is a simple lookup table that stores embeddings of a fixed dictionary and size.

Any input less than zero or more than declared input dimension raise this error. Compare your input and the dimension mentioned in torch.nn.Embedding.

Attached code snippet to simulate the issue.

from torch import nn
input_dim = 10
embedding_dim = 2
embedding = nn.Embedding(input_dim, embedding_dim)
err = True
if err:
    #Any input more than input_dim - 1, here input_dim = 10
    #Any input less than zero
    input_to_embed = torch.tensor([10])
else:
    input_to_embed = torch.tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
embed = embedding(input_to_embed)
print(embed)

Hope this will solve your issue.

Solution 2

Last time I got this same IndexError: index out of range in self using BERT was because my input text was too long and the output tokens from my tokenizer is more than 512 tokens. I solved it by truncating the tokens array at 512.

    encoded_input = tokenizer(text, return_tensors='pt')
    #{'input_ids': tensor([[    0, 12350, ...,  363,     2]]),
    #'attention_mask': tensor([[1, 1,..., 1, 1]])}

    encoded_input_trc={}
    for k,v in encoded_input.items():
        v_truncated = v[:,:512]
        encoded_input_trc[k]=v_truncated
    return encoded_input_trc

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment