Error: to many values to unpack

I have some code:

from torchvision.datasets import ImageFolder
import torchvision.transforms as T
from torch.utils.data import DataLoader
import torch
import torch.nn as nn
import torch.nn.functional as f
import torch.optim as optim

path ="/Users/edenbrown/Downloads/kagglecatsanddogs_3367a/PetImages"
transform = T.Compose([T.Resize((50, 50)),T.ToTensor()])

dataset = ImageFolder(root=path, transform=transform)

dataloader = DataLoader(dataset, batch_size=10, shuffle=True)

for batch_number, (images, labels) in enumerate(dataloader):
break

class Net(nn.Module):

def __init__(self):
    super().__init__()
    self.fc1 = nn.Linear(50 * 50, 64)
    self.fc2 = nn.Linear(64, 64)
    self.fc3 = nn.Linear(64, 64)
    self.fc4 = nn.Linear(64, 2)

def forward(self, x):
    x = f.relu(self.fc1(x))
    x = f.relu(self.fc2(x))
    x = f.relu(self.fc3(x))
    x = self.fc4(x)
    return f.log_softmax(x, dim = 1)

net = Net()

optimizer = optim.Adam(net.parameters(), lr = 0.001)

Epochs = 3

for epoch in range(Epochs):
for data in labels, images:
a, b = data
net.zero_grad()
output = net(a.view(-1, 50 * 50))
loss = f.nll_loss(output, b)
loss.backward()
optimizer.step()
print(loss)

I am getting this error:

Traceback (most recent call last):
File “/Users/edenbrown/sample.ws45/new.py”, line 45, in
a, b = data
ValueError: too many values to unpack (expected 2)

Is there a way to fix this error.

Based on the code and error it seems that you are trying to iterate:

for data in labels, images:

instead of the dataloader and later unpack data via:

a, b = data

Check that data can be unpacked and make sure you really want to iterate labels, images.

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.

Just so you know I changed:

for data in labels, images:

into:

for data in images, labels

What I am trying to do is make a = labels, b = images but for the one image which is being processed through the for loop. So basically, if the image is a dog the label should be one and the image variable shall be the tensor of that image. Therefore for that one image, a should = 1 and b should = the correct tensor.

I do not need a answer anymore I figured this out: