TypeError: cannot unpack non-iterable float object

I have recently started on my machine learning and deep learning journey and I was following this DataQuest tutorial. Everything was going fine. Each cell was running just the way it should. Until i got to the training and even then the model trained for 3.5 hours then i got this error when it finished.

TypeError: cannot unpack non-iterable float object

This is the block of code that would gave me the error

for epoch in range(EPOCHS):
for batch, (images, labels, img_paths) in enumerate(train):
optimizer.zero_grad()

    images = images.to(device)
    pred = model(images.float())
    labels = labels.to(device)
    loss = loss_fn(pred, labels)

    loss.backward()
    optimizer.step()

loss, current = loss.item()
print(f"loss: {loss:>7f} [{epoch}]")

This is the entire Code on GitHub.

I would usually play around with the code a bit and see what i can do but 3.5 hours is a long time to wait to see if what i tried worked.

Any help would be appreciated.

Thank you

Hi, the issue lies in the line:
loss, current = loss.item()

loss.item() gives you a single float value. And it cannot be packed to two values, loss, current. The correct way would be:
loss = loss.item()

If you want a more in depth tutorial please refer to this example over here.

If you want to read more about loss.item() please refer this.

Regarding, it taking 3.5 hours. I thinking you are not using GPUs in google colab. Computations would be much faster in GPU.

1 Like

Just checked the video you linked as well, the author uses this as well over here

Thank you for your help and for the references for loss.item(). It is extremely appreciated. I used googles colab to code in because my computer isnt the greatest its an old hp prodesk 6200 and definitely doesnt have a nvidia gpu. I changed the runtime to gpu i just figured it took so long because of the size of the data set and the amount of epochs. In the video i was following he said it took him til the next day to finish training

1 Like