'What's the best way of checking whether a torch::Tensor is empty?

I'm currently using the has_storage() method to check whether a tensor is empty or not, but I wonder if there is anything better other than this! and whether there are any implications involved in using this other than the fact that an initialized torch::Tensor always has a storage while an empty one doesn't!



Solution 1:[1]

Yes there is a small nuance here : all tensors do not have the same underlying implementation, and some implementations will have has_storage return false no matter what. This is in particular the case for sparse tensor (see here).

However I am not aware of any better way. Just be sure to correctly track your sparse tensors if you use them (and your opaque tensors, if you ever need whatever they are ^^)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 trialNerror