Solved by yuanyin in post #2 
              
              
                You can replace “input = x_train[i]” by “input = x_train[i].clone().detach()”. This error was caused by loop dependency “x_train ← input ← prediction ← x_train”.