Solved by InnovArul in post #2 
              
              
                I think you do not require gradient to states variable. Commenting this line will make the code work. 
Why .detach()ing works? 
This is my understanding. You are using the same variable (states) that is having requires_grad = True again and again. When you use it for the first time, there is no pro…