Sathsara_Rasantha:

What are the best ways to debug these kinds of errors in pytorch. Are there any tutorials or books I can refer to ?


The first step would be to check the error message, which provides good information for this particular failure: the target contains indices, which are out of bounds in nll_loss.
From a general point I don’t know, if there are any good books to debug Python applications.
If you are using the GPU, rerun the script with CUDA_LAUNCH_BLOCKING=1 python script.py args, as you might often get the wrong lines of code in the stack trace due to the asynchronous CUDA operations.
Alternatively, you could rerun the code on the CPU and should see a better error message.
If you need to debug further, you can use e.g. pdb or gdb for the backend.