At first, I thought that's because %timeit
only evaluates the time run by functions. But thanks to @Shiva who told me it can calculate the execution time of other things. And I checked the documentation here, and I found out this is true.
So, according to this answer, %timeit
has a problem with re-assignment as the re-assignment to a
causes the function to have an a
local variable, hiding the global. In other words, you can use any other variable other that a
to assign it to torch.where
:
#this works
%timeit c = torch.where(b > 0.5, torch.tensor(0.), a) #c instead of a
# this works
%timeit torch.where(b > 0.5, torch.tensor(0.), a)
# this doesn't work
%timeit a = torch.where(b > 0.5, torch.tensor(0.), a)