torch
torch copied to clipboard
Is it possible to detect if tensor is sparse or dense?
I'm trying to make a custom collate_fn where some of my data is sparse and some dense. With the sparse data I want to stack and then use to_dense() but with the dense I only want to stack.
In pytorch there is the function is_sparse which relies on the tensor.layout property.
It doesn't seem like the layout property is exposed here. Is it possible to expose it so I can detect if the tensor is sparse? Or is there already some way to do it?
Regards, Egill
@egillax sorry, this is currently not possible, but we will make sure to implement it for the version.
It turns out this would be handy to have for autograd functional (#738 ), so I can add this functionality as part of the PR for that. It should be a pretty straightforward update to Lantern, since the Torch C++ API provides is_sparse()
(so we just need a basic wrapper). Or would it be better to make a separate PR for this?
Cool! No need to create a separate PR.
Just adding a reference to the is_sparse()
function in C++ so I don't forget where it is (curious that the Python interface doesn't use this and instead uses the layout
property, but since R doesn't yet implement layout
, going this route seems easier for now).
https://github.com/pytorch/pytorch/blob/0032fa772588cd20f2b85324bdeb31ac497bbf29/c10/core/TensorImpl.h#L813-L820