The author of the question didn’t follow up so I’m unsure, what the root cause in the posted script was.
However, since you are running into the same issue, check the shapes of all involved tensors and make sure you are not removing dimensions you would like to index later.