TypeError: 'int' object is not callable when using second iterator

I have two Dataloader for train dataset and val dataset. However, when I use:

train_iter = iter(trainloader)

It seems to be ok. But if I want to use anthor iterator to test data, I try to:

val_iter = iter(valloader)

I got error: TypeError: ‘int’ object is not callable. Besides, if train_iter came to the end and get StopIteration, I want to delete train_iter and redefine it again for training, I got the same error. The code is as follow:

    image, label = data_iter.next()
except StopIteration:
    del data_iter
    data_iter = iter(trainloader)
    image, label = data_iter.next()

Why I cannot use second dataloader iterator, how to fix it?

I change all the iter(dataloader) to dataloader.iter(). For example:

train_iter = trainloader.__iter__()
val_iter = valloader.__iter__()

Everything is ok. But I cannot find out what happened. Could anyone tell me why?

you likely reassigned iter to an int

1 Like