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

i’m trying to extract features and got this error from this line

feats = model(samples).copy()

features = torch.zeros(len(data_loader.dataset), feats.shape[-1])

i read that i should convert it to numpy but couldn’t write it well

the first error was

AttributeError: ‘list’ object has no attribute ‘clone’

so i changed it to copy but got current error in the title of the post

Based on the error messages it seems that feats is a list, while you are trying to use it as a tensor.
You could thus make sure that the forward method of your model returns a tensor or convert the list to a tensor using e.g. torch.stack.

1 Like

thanks but you mean that i got

AttributeError: ‘list’ object has no attribute ‘clone’

as feats is a list and i’m trying to use it as a tensor ?

Yes, Python lists don’t have the clone() or shape operations, which are PyTorch tensor ops, so it seems you are assuming feats is a tensor, while it’s a list.

1 Like

excuse me do you mean this function

def forward(self, x):
x = self.forward_features(x)

    if self.F4:
        x = x[3:4]

    return x

Yes, the forward method seems to return a list, so you would have to check why this is the case (check the type of x and try to isolate where it’s transformed to a list) and/or create a tensor, if needed.