The problem is I don’t know which function ** calls to. My guess is that it is not checking if the graph is broken or not. For sure you cannot do this: res.flatten()[::x.size(0) + 1] = 0.0. Its not a matter of how you code it. You are hard assigning some values of the tensor as 0, that’s not backproagable. There are no gradients between that and the previous state as you manually hard-coded it.