Embeddings index out of range error

You have to check the range of the input tensor to the nn.Embedding layer and make sure its values are in [0, num_embeddings-1].
Here is another small example showing the IndexError:

# create embedding layer which expects inputs with indices in [0, num_embeddings-1]
num_embeddings = 10
embedding_dim = 5
emb = nn.Embedding(num_embeddings, embedding_dim)

# valid input since in range [0, 9]
x = torch.tensor([0, 4, 9])
out = emb(x)

# invalid input as it contains indices with are out-of-bounds
x = torch.tensor([0, 4, 10]) # 10 is invalid!
out = emb(x)
# IndexError: index out of range in self