I have code like this:
batch_size = 100
train_dataset, test_dataset = convos.split(split_ratio=0.7)
train_iterator = torchtext.data.BucketIterator(
train_dataset,
batch_size=3000,
sort_key=lambda x: torchtext.data.interleave_keys(len(x.context), len(x.response)),
device=device
)
print("Batch size: ", batch_size)
print("Train size: ", len(train_iterator))
```text
Batch size: 100
Train size: 1
Following this, I do
```python
for count, batch in enumerate(train_iterator):
print(count)
And this program doesn't stop (at least I checked until count = 3000). I suppose it should iterate just once!
Yes, your code does not stop.
Iteration class's behavior may be confusing. If you want to iterate just once, you need to pass False to repeat argument like this.
torchtext.data.BucketIterator(
train_dataset,
batch_size=3000,
sort_key=lambda x: torchtext.data.interleave_keys(len(x.context), len(x.response)),
device=device,
repeat=False
)
False as the default value of repeat seems to be better than None, I guess.
Does this mean, the iterator keeps repeating and is there a way to fix the number of repeats?
Yes, if you want to iterate just once in each epoch, my code snippet above can do that.
Thanks you. I would suggest to set default repeat to True False in BucketIterator Iterator -- that may break existing codebases but I think typically iterators are not in infinite loop.
My pleasure.
I do not think so because example in PyTorch official repo also set False to repeat in BucketIterator, so I propose to set True to repeat as default in all iterator class for usability.
In addition, even if all iterator has repeat=False as default, this repo may need to provide some example codes to break infinite loop during training (but I do not have a specific example yet...).
How do you think about this? @mttk
Yeah, I believe it is more or less agreed that the default infinite loop in Iterators is confusing.
I'd say that the expected behavior is to iterate for one epoch (so, the default would be repeat=False), and leave the infinite loop as an option if users prefer it. This might break something backwards but it's manageable.
I will just close this issue since a PR is attached. Thanks all for the help.
Most helpful comment
Yeah, I believe it is more or less agreed that the default infinite loop in Iterators is confusing.
I'd say that the expected behavior is to iterate for one epoch (so, the default would be
repeat=False), and leave the infinite loop as an option if users prefer it. This might break something backwards but it's manageable.