Dataloader has no attribute dataset

I’m running a code that uses dataloader.dataset. The original code is designed to run on Pytorch 1.1, but my Pytorch version is higher. Are there any alternatives to this attribute?

And here’s the error report.
AttributeError: '_SingleProcessDataLoaderIter' object has no attribute 'dataset'

Based on the error message it seems you are trying to access the internal .dataset attribute in the iterator, not the DataLoader itself as seen here:

dataset = TensorDataset(torch.randn(100, 1), torch.randn(100, 1))
loader = DataLoader(dataset)

print(loader.dataset)
# > <torch.utils.data.dataset.TensorDataset object at 0x7fd40071a9a0>

loader_iter = iter(loader)
print(loader_iter.dataset)
# > AttributeError: '_SingleProcessDataLoaderIter' object has no attribute 'dataset'

Is there an easy way to retreive the data from the dataset/dataloader now?
Sample code from Official PyTorch Tutorial directly fails with this ERROR message:

AttributeError: '_SingleProcessDataLoaderIter' object has no attribute 'dataset' .

If the following snippet code ONLY bings trouble, why it’s ALWAYS there on the official website? Any update to make sure it’s runnable?

# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()

The posted code snippet it not accessing the .dataset attribute so I’m unsure how this error can even be raised.
The tutorial works fine for me, so please post a minimal, executable code snippet to reproduce the issue.

Okay… I got it… It looks to me a kind of weird…

images, labels = next(iter(validation_loader)) is working properly, however,

dataiter = iter(train_loader)
images, labels = dataiter.next()

is not…

okay,

dataiter = iter(train_loader)
images, labels = next(dataiter)

also works… :slight_smile:

1 Like

Both approaches work for me, so it’s still unclear which change in your code causes the issue.

Does it have something to do with:

I built the NEWEST PyTorch from source ? it’s showing the version: 1.13.0

I doubt it, as I’m also using a (quite new) source build, so please feel free to point to the line of code in the tutorial creating the issue (I cannot reproduce it) or post a code snippet which would raise the error.

I got the same error, so here’s the minimum code snippet that raise the error:

import torch
import torchvision
import torchvision.transforms as transforms

print("Pytorch version: {}".format(torch.__version__))

transform = transforms.Compose(
    [transforms.ToTensor(),
    transforms.Normalize((0.5,), (0.5,))])

# Create datasets for training & validation, download if necessary
training_set = torchvision.datasets.FashionMNIST('./data', train=True, transform=transform, download=True)

# Create data loaders for our datasets; shuffle for training, not for validation
training_loader = torch.utils.data.DataLoader(training_set, batch_size=4, shuffle=True)

dataiter = iter(training_loader)
images, labels = dataiter.next()

1 Like

I found this:

Python has renamed iterator.next() to iterator.__next__() in version 3.0, maybe that relates to this issue?

1 Like

The .next() operation was removed in this PR as it’s Python 2 syntax according to PEP 3114 so use the next(iterator) call.

1 Like

i do the same to you but i got another issue

dataiter = iter(data_loader)
images, labels = next(dataiter)
print(torch.min(images), torch.max(images))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_9516\372505004.py in <module>
      1 dataiter = iter(data_loader)
----> 2 images, labels = next(dataiter)
      3 print(torch.min(images), torch.max(images))

~\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in __next__(self)
    626                 # TODO(https://github.com/pytorch/pytorch/issues/76750)
    627                 self._reset()  # type: ignore[call-arg]
--> 628             data = self._next_data()
    629             self._num_yielded += 1
    630             if self._dataset_kind == _DatasetKind.Iterable and \

~\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in _next_data(self)
    669     def _next_data(self):
    670         index = self._next_index()  # may raise StopIteration
--> 671         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    672         if self._pin_memory:
    673             data = _utils.pin_memory.pin_memory(data, self._pin_memory_device)

~\anaconda3\lib\site-packages\torch\utils\data\_utils\fetch.py in fetch(self, possibly_batched_index)
     56                 data = self.dataset.__getitems__(possibly_batched_index)
     57             else:
---> 58                 data = [self.dataset[idx] for idx in possibly_batched_index]
     59         else:
     60             data = self.dataset[possibly_batched_index]

~\anaconda3\lib\site-packages\torch\utils\data\_utils\fetch.py in <listcomp>(.0)
     56                 data = self.dataset.__getitems__(possibly_batched_index)
     57             else:
---> 58                 data = [self.dataset[idx] for idx in possibly_batched_index]
     59         else:
     60             data = self.dataset[possibly_batched_index]

~\anaconda3\lib\site-packages\torchvision\datasets\mnist.py in __getitem__(self, index)
    143 
    144         if self.transform is not None:
--> 145             img = self.transform(img)
    146 
    147         if self.target_transform is not None:

TypeError: 'bool' object is not callable

i dont know how to due with this pls help me.

Based on the error message it seems you’ve defined self.transform as a bool instead of a callable transformation.

Thank you. it worked for me.