In Pytorch 1.0.0, I found that a tensor
variable occupies very small memory. I wonder how it stores so much data.
Here\'s the code.
a = np.random.ra
The answer is in two parts. From the documentation of sys.getsizeof, firstly
All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.
so it could be that for tensors __sizeof__
is undefined or defined differently than you would expect - this function is not something you can rely on. Secondly
Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.
which means that if the torch.Tensor
object merely holds a reference to the actual memory, this won't show in sys.getsizeof
. This is indeed the case, if you check the size of the underlying storage instead, you will see the expected number
import torch, sys
b = torch.randn(1, 1, 128, 256, dtype=torch.float64)
sys.getsizeof(b)
>> 72
sys.getsizeof(b.storage())
>> 262208
Note: I am setting dtype
to float64
explicitly, because that is the default dtype
in numpy
, whereas torch
uses float32
by default.