So I need to get the size of a list of tensors… that’s right, [tensor_1, tensor_2, tensor_3]
; apparently, .size()
will not work for this, so I need to use map
function eventually I guess. But what’s the function to get the size of the tensor?
I guess you can use a lambda to do this: lambda t: t.size()
?
call up lambda each time will be slow tho
Not sure it will be slower than any other python invocation.
Why would it be so slow?
Basically everytime (in a loop) you use lambda, you are basically defining a function again. So predefine a function and use it later will be much more efficient than lambda.
Fun fact, the lambda is actually significantly faster than creating a function and reusing it
In [1]: import torch
In [2]: def gs(t):
...: return t.size()
...:
In [3]: t = torch.rand(10, 3, 2)
In [4]: %timeit gs(t)
692 ns ± 12.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [5]: %timeit gs(t)
740 ns ± 5.03 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [6]: %timeit lambda x: x.size()
228 ns ± 3.08 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [7]: %timeit lambda x: x.size()
235 ns ± 3.41 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
2 Likes