Solved by JuanFMontesinos in post #4 
              
              
                I’m a bit loss with your code 
What is see is that you are assigning y  for example here 
y = (x*(1-theta).expand_as(x)).add(z*theta.expand_as(z)) #use pytorch functions for math

Thus y is no longer pointing to the deepcopy you did but a inner variable in the scope. 
x = y.sub(temp)

same there, yo…