Search code examples
pythonpytorchtensorboard

Tensorboard - ValueError: too many values to unpack (expected 2)


I have tried to use tensorboard to visualize a model. I was following the pytorch.org tutorial. Here is the code for dataloader.

writer_train = SummaryWriter('runs/training')

train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=config.train_batch_size, shuffle=True,
                                           num_workers=config.num_workers, pin_memory=True)

images, labels = next(iter(train_loader))
writer_train.graph_model(light_net, images)

and I got this error in the iter line.

images, labels = next(iter(train_loader)) ValueError: too many values to unpack (expected 2)

I have debugged the code and found this. enter image description here


Solution

  • The error is likely caused by the use of a built-in function instead of the .next() method of the train_loader object.

    next() and iter() are builtin methods in Python. See from the docs iter and next.

    In the tutorial is shows the following

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

    Where it uses the next() method to unpack values into the 2 variables. This is not the same as your usage of next(iter(train_loader)). Do it in the way as shown and it should solve your problem.