ValueError: too many values to unpack (expected 2) with mnist

username: ati
cooked: <p>Hi<br>
I’m using mnist dataset in my code , but there’s an error when i run it</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "C:\Users\Ati\Desktop\GD\GD.py", line 306, in &lt;module&gt;
    update_interval=update_interval, max_prob=max_prob, plot=plot, train=train, gpu=gpu)
  File "C:\Users\Ati\Desktop\GD\GD.py", line 105, in main
    images, labels = dataiter.next()
ValueError: too many values to unpack (expected 2)
</code></pre>
<p>i think problem is related to this part of my code:</p>
<pre><code class="lang-auto"> transform = transforms.Compose([transforms.ToTensor(),
                              transforms.Normalize((0.5,), (0.5,)),
                              ])
    root = data_path
    dataset = MNIST(root='root', download=True , transform=transform)

    trainloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True)
    dataiter = iter(trainloader)
    images, labels = dataiter.next()
    images, labels = images.view(-1, 784) ,  labels
</code></pre>
<p>Could someone give me a solution for this problem please?</p>

username: beaupreda
cooked: <p>Hello,</p>
<p>I tried your code and did not get any errors. Is there another place where you call <code>images, labels = dataiter.next()</code>? Also, what is your PyTorch and Torchvision versions?</p>
<p>And one last thing, why do you divide your images by 255 on the last line?</p>

username: ati
cooked: <p>thanks <a class="mention" href="/u/beaupreda">@beaupreda</a> for your response<br>
I use in another part of my code</p>
<pre><code class="lang-auto">    for i in range(60000):
        label = torch.Tensor([labels[i % len(labels)]]).long()
        image = images[i % len(labels)]
</code></pre>
<p>by the way , when i write this <code>print(dataiter.next())</code> it just shows me two arrays of 28*28 (it means for two images …while we have 60000 training examples in mnist)<br>
Do you think i need to put it in a loop or not needed?<br>
about last line and dividing on 255 , it was my fault that i modified it<br>
the original code is here, if you liked to check it<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/BINDS-LAB-UMASS/bindsnet_experiments/blob/master/experiments/mnist/backprop.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/BINDS-LAB-UMASS/bindsnet_experiments/blob/master/experiments/mnist/backprop.py" target="_blank" rel="nofollow noopener">BINDS-LAB-UMASS/bindsnet_experiments/blob/master/experiments/mnist/backprop.py</a></h4>
<pre><code class="lang-py">import os
import torch
import argparse
import numpy as np
import matplotlib.pyplot as plt

from time import time as t
from sklearn.metrics import confusion_matrix

from bindsnet.datasets import MNIST
from bindsnet.network import Network, load_network
from bindsnet.network.monitors import Monitor
from bindsnet.network.topology import Connection
from bindsnet.network.nodes import RealInput, IFNodes
from bindsnet.analysis.plotting import plot_spikes, plot_weights

from experiments import ROOT_DIR

data = 'mnist'
model = 'backprop'
</code></pre>

  This file has been truncated. <a href="https://github.com/BINDS-LAB-UMASS/bindsnet_experiments/blob/master/experiments/mnist/backprop.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>

username: beaupreda
cooked: <p>What <code>dataiter.next()</code> does is it gives you a <code>B x 1 x H x W</code>  image tensor and a <code>B</code> label tensor, where <code>B</code> is the batch size specified in your dataloader. If you want to see every training example, an easier way is to simply to a <code>for</code> loop on the <code>trainloader</code> directly.</p>
<pre><code class="lang-auto">for images, labels in trainloader:
    # training loop, reshape images if you have to
    print(image.shape)
    # torch.Size([1, 1, 28, 28])
</code></pre>
<p>Hope this helps!</p>