Federico_Vasile:

def count_diffs(x):
    diff_x = x[1:] - x[:-1]
    return (diff_x != 0).sum()



This function takes a x that is a float Tensor (potentially with autograd info attached) and returns an integer (inside a Tensor) that represent the value you’re looking for.
But integers are not continuous, so you cannot compute gradients for them.
So you won’t be able to get gradients flowing back if you use this function.