AttributeError: 'NestedTensor' object has no attribute 'shape'

I have a variable called samples and when I do type(samples) it gives me <class 'util.misc.NestedTensor'>.
when I print it, it looks like a normal tensor, but when I do samples.shape it gives an error that *** AttributeError: 'NestedTensor' object has no attribute 'shape'

I guess my question is what is NestedTensor n pytorch and how I can get the shape of it?

NestedTensor is a package to work with tensors of different sizes and lengths. It should be released soon, so I guess you might be using the nightly binary?

Anyway, I’m not sure if .shape should return this error or some struct containing different lengths.
CC @cpuhrsch who is the main author of this package.

I was actually using DERT and I see their samples in that format, so I was confused about what is that

Ah OK, in that case it seems to be a custom implementation from here, which just doesn’t support the shape attribute, but I also haven’t looked at this repository deeply.

1 Like

got it! thanks!!
if pytorch is working on NestedTensor as well I guess it would be good to consider supporting .shape if it does not already have

Hi i think nested tensors were implemented in DETR. To get the shape, use: Nested_Tensor.tensors.shape

Here’s the link to the DETR hands-on where I found it; its in the attention visualisation section. Cheers!

5 Likes