I am trying to implement a transformer model for the translation task, from some youtube tutorials. But I am getting the index out-of-range error. It seems The problem is with the input dimensions, but I can't figure it out. Here is the code (google colab link)
You can find the datasets here
I tried to change the dimensions but It didn't help or I couldn't do it correctly. I hope someone can help solve this problem. Thanks
I went through your code and found out that in the error trace of yours (error in
forwardcall ofSentenceEmbedding,encoderstage)If you add
print(torch.max(x))before the linex = self.embedding(x)Then you can see that the error is because
xcontainsidthat is >=68. If the value is greater than 68, then Pytorch will raise the error mentioned in the stack trace.It means that while you are converting
tokenstoids, you are assigning a value greater than 68.To prove my point:
when you are creating
english_to_index, since there are three""in yourenglish_vocabulary(START_TOKEN,PADDING_TOKEN,END_TOKENare all"") you end up generating{ "": 69 }. Since this value is greater than thelen(english_to_index) # length = 68.Hence, you are getting
IndexError: index out of range in selfSolution
As a solution, you can give unique tags to these tokens (which is generally prescribed) as:
This will make sure that the generated dictionaries will have the correct sizes.
Please find the working Google Colaboratory file here with the solution section.
I added
'\\'to theenglish_vocabularysince after a few iterations we get aKeyError: '\\'.Hope it helps.