Loss.backward() Error: RuntimeError: Index out of range

I got the error

  File "/home/x/Projects/ProjectIM/network-opt-rl/src/agent/rl4im/agent_cc.py", line 72, in learn
  File "/home/x/anaconda3/envs/torch1.7/lib/python3.7/site-packages/torch/tensor.py", line 221, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/home/x/anaconda3/envs/torch1.7/lib/python3.7/site-packages/torch/autograd/__init__.py", line 132, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: Index out of range

Does anyone know how to fix it?

Could you post a minimal and executable code snippet to reproduce the issue, please?
Also, in case you are not using the latest PyTorch release, could you update and rerun the script?