Dataloader has no attribute dataset

ZiQing_Li: <p>I’m running a code that uses <code>dataloader.dataset</code>. The original code is designed to run on Pytorch 1.1, but my Pytorch version is higher. Are there any alternatives to this attribute?</p>
<p>And here’s the error report.<br>
<code>AttributeError: '_SingleProcessDataLoaderIter' object has no attribute 'dataset'</code></p>

ptrblck: <p>Based on the error message it seems you are trying to access the internal <code>.dataset</code> attribute in the iterator, not the <code>DataLoader</code> itself as seen here:</p>
<pre><code class="lang-python">dataset = TensorDataset(torch.randn(100, 1), torch.randn(100, 1))
loader = DataLoader(dataset)

print(loader.dataset)
# &gt; &lt;torch.utils.data.dataset.TensorDataset object at 0x7fd40071a9a0&gt;

loader_iter = iter(loader)
print(loader_iter.dataset)
# &gt; AttributeError: '_SingleProcessDataLoaderIter' object has no attribute 'dataset'
</code></pre>

jiapei100: <p>Is there an easy way to retreive the data from the dataset/dataloader now?<br>
<a href="https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html" rel="noopener nofollow ugc">Sample code from Official PyTorch Tutorial</a> directly fails with this <strong>ERROR</strong> message:</p>
<p><code>AttributeError: '_SingleProcessDataLoaderIter' object has no attribute 'dataset'</code> .</p>
<p>If the following snippet code <strong>ONLY</strong> bings trouble, why it’s <strong>ALWAYS</strong> there on the official website? Any update to make sure it’s runnable?</p>
<pre data-code-wrap="console"><code class="lang-nohighlight"># get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()
</code></pre>

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

jiapei100: <p>Okay… I got it… It looks to me a kind of weird…</p>
<p><code>images, labels = next(iter(validation_loader))</code> is working properly, however,</p>
<pre><code class="lang-auto">dataiter = iter(train_loader)
images, labels = dataiter.next()
</code></pre>
<p>is not…</p>
<p>okay,</p>
<pre data-code-wrap="console"><code class="lang-nohighlight">dataiter = iter(train_loader)
images, labels = next(dataiter)
</code></pre>
<p>also works… <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

ptrblck: <p>Both approaches work for me, so it’s still unclear which change in your code causes the issue.</p>

jiapei100: <p>Does it have something to do with:</p>
<p>I built the <strong>NEWEST PyTorch</strong>  from source ? it’s showing the version: 1.13.0</p>

ptrblck: <p>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.</p>

phuna: <p>I got the same error, so here’s the minimum code snippet that raise the error:</p>
<pre><code class="lang-auto">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 &amp; 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()

</code></pre>

phuna: <p>I found this:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://peps.python.org/pep-3114/">
  <header class="source">
      <img src="https://peps.python.org/_static/py.png" class="site-icon" width="16" height="16">

      <a href="https://peps.python.org/pep-3114/" target="_blank" rel="noopener nofollow ugc">peps.python.org</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://peps.python.org/pep-3114/" target="_blank" rel="noopener nofollow ugc">PEP 3114 – Renaming iterator.next() to iterator.__next__() | peps.python.org</a></h3>

  <p>Python Enhancement Proposals (PEPs)</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

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

ptrblck: <p>The <code>.next()</code> operation was removed in <a href="https://github.com/pytorch/pytorch/pull/78594">this PR</a> as it’s Python 2 syntax according to <a href="https://peps.python.org/pep-3114/">PEP 3114</a> so use the <code>next(iterator)</code> call.</p>

l_c_le: <p>i do the same to you but i got another issue</p>
<pre><code class="lang-auto">dataiter = iter(data_loader)
images, labels = next(dataiter)
print(torch.min(images), torch.max(images))
</code></pre>
<pre><code class="lang-auto">---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_9516\372505004.py in &lt;module&gt;
      1 dataiter = iter(data_loader)
----&gt; 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]
--&gt; 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
--&gt; 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:
---&gt; 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 &lt;listcomp&gt;(.0)
     56                 data = self.dataset.__getitems__(possibly_batched_index)
     57             else:
---&gt; 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:
--&gt; 145             img = self.transform(img)
    146 
    147         if self.target_transform is not None:

TypeError: 'bool' object is not callable
</code></pre>
<p>i dont know how to due with this pls help me.</p>

ptrblck: <p>Based on the error message it seems you’ve defined <code>self.transform</code> as a <code>bool</code> instead of a callable transformation.</p>

piyush_paliwal1: <p>Thank you. it worked for me.</p>