Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

fix len calculation for new data loader #4618

Merged
merged 3 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions allennlp/data/data_loaders/multi_process_data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ def __init__(
deque(self.iter_instances(), maxlen=0)

def __len__(self) -> int:
if self.max_instances_in_memory is None:
if self.batches_per_epoch is not None:
return self.batches_per_epoch
elif self.max_instances_in_memory is None:
# We haven't read the instances yet, so we do so now, caching them as we go.
if not self._instances:
deque(self.iter_instances(), maxlen=0)
Expand All @@ -218,8 +220,6 @@ def __len__(self) -> int:
return num_instances // batch_size
else:
return 1 + num_instances // batch_size
elif self.batches_per_epoch is not None:
return self.batches_per_epoch
else:
# We can't know the number of batches for a lazy loader when batches_per_epoch
# is not specified.
Expand Down
11 changes: 11 additions & 0 deletions tests/data/data_loaders/multi_process_data_loader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,14 @@ def test_drop_last():
for batch in batches:
assert len(batch["index"]) == 16
assert len(batches) == 6


def test_batches_per_epoch():
loader = MultiProcessDataLoader(
MockDatasetReader(), "some path", batch_size=4, batches_per_epoch=10
)
vocab = Vocabulary.from_instances(loader.iter_instances())
loader.index_with(vocab)

assert len(loader) == 10
assert len(list(loader)) == 10