Solved by tom in post #6 
              
              
                Sorry for posting a wrong solution before. 
The reason it does not work as expected is because python’s in tries to use ==, and that will not identify tensors. 
Using the parameter names will work (you could also hack around it by keeping a set of p.data_ptr() and filter by that, but that is ugly…): …