Can't pass subset into model

I created a subset of data

subset = torch.utils.data.Subset(train_dataset,newIndices)

Here, newIndices is just a list of indices to use in my subset.
I want to analyze the logits I get on a forward pass, so I need to pass all the data of the subset into my model at once.

images=subset.data.type(torch.FloatTensor)
logits = model(images)

This gives me the error that Subset has no attribute ‘data’, and I get the same issue with ‘targets’. Is there a simple fix to this, to access all the subset data/targets and not the original set?

You would need to iterate the Subset and store the data/targets in e.g. new tensors or a list.
The internal Dataset could lazily load the samples in its __getitem__ so you won’t be necessarily able to access a .data or .target attribute as it might not exist until you actually load the sample.
However, if you know that your Dataset pre-loading the data and indeed uses these internal attributes, you might be able to index them via newIndices assuming both are tensors.

I ended up creating a dataloader for the subset and putting batch size as the entire subset size… is this bad practice? Seemed to get the job done.

No, your approach sounds fine.

1 Like