Solved by ptrblck in post #2 
              
              
                The manipulation of the .data attribute cannot be tracked by Autograd and can yield wrong results (as in this case) and other unexpected side effects. 
Remove the x.data manipulation and it should work.