forked from allenai/allennlp
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b88b876
commit 0be0faa
Showing
4 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import torch | ||
|
||
from allennlp.data import DatasetReader, Instance, Vocabulary | ||
from allennlp.data.fields import LabelField | ||
from allennlp.data.dataset_readers import MultiTaskDatasetReader | ||
from allennlp.data.data_loaders.multitask_data_loader import MultiTaskDataLoader | ||
from allennlp.data.data_loaders.multitask_scheduler import RoundRobinScheduler | ||
from allennlp.data.data_loaders.multitask_epoch_sampler import UniformSampler | ||
|
||
|
||
class FakeDatasetReaderA(DatasetReader): | ||
def _read(self, file_path: str): | ||
while True: | ||
yield Instance({"label": LabelField("A")}) | ||
|
||
|
||
class FakeDatasetReaderB(DatasetReader): | ||
def _read(self, file_path: str): | ||
while True: | ||
yield Instance({"label": LabelField("B")}) | ||
|
||
|
||
class MultiTaskDataLoaderTest: | ||
def test_loading(self): | ||
reader = MultiTaskDatasetReader( | ||
readers={"a": FakeDatasetReaderA(), "b": FakeDatasetReaderB()} | ||
) | ||
data_path = {"a": "ignored", "b": "ignored"} | ||
batch_size = 4 | ||
scheduler = RoundRobinScheduler() | ||
sampler = UniformSampler() | ||
instances_per_epoch = 8 | ||
batch_size_multiplier = {"a": 1, "b": 2} | ||
loader = MultiTaskDataLoader( | ||
reader=reader, | ||
data_path=data_path, | ||
batch_size=batch_size, | ||
scheduler=scheduler, | ||
sampler=sampler, | ||
instances_per_epoch=instances_per_epoch, | ||
batch_size_multiplier=batch_size_multiplier, | ||
max_instances_in_memory={"a": 10, "b": 10}, | ||
) | ||
vocab = Vocabulary() | ||
vocab.add_tokens_to_namespace(["A", "B"], "labels") | ||
loader.index_with(vocab) | ||
iterator = iter(loader) | ||
batch = next(iterator) | ||
assert torch.all(batch["label"] == torch.IntTensor([0, 1, 0])) | ||
batch = next(iterator) | ||
assert torch.all(batch["label"] == torch.IntTensor([1, 0])) | ||
batch = next(iterator) | ||
assert torch.all(batch["label"] == torch.IntTensor([1, 0])) | ||
batch = next(iterator) | ||
assert torch.all(batch["label"] == torch.IntTensor([1])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from allennlp.data.data_loaders.multitask_scheduler import ( | ||
RoundRobinScheduler, | ||
HomogeneousRoundRobinScheduler, | ||
) | ||
|
||
|
||
class RoundRobinSchedulerTest: | ||
def test_order_instances(self): | ||
scheduler = RoundRobinScheduler() | ||
epoch_instances = { | ||
"a": [1] * 5, | ||
"b": [2] * 3, | ||
} | ||
flattened = scheduler.order_epoch_instances(epoch_instances) | ||
assert list(flattened) == [ | ||
("a", 1), | ||
("b", 2), | ||
("a", 1), | ||
("b", 2), | ||
("a", 1), | ||
("b", 2), | ||
("a", 1), | ||
("a", 1), | ||
] | ||
|
||
|
||
class HomogeneousRoundRobinSchedulerTest: | ||
def test_order_instances(self): | ||
scheduler = HomogeneousRoundRobinScheduler({"a": 2, "b": 3}) | ||
epoch_instances = { | ||
"a": [1] * 9, | ||
"b": [2] * 9, | ||
} | ||
flattened = scheduler.order_epoch_instances(epoch_instances) | ||
assert list(flattened) == [ | ||
("a", 1), | ||
("a", 1), | ||
("b", 2), | ||
("b", 2), | ||
("b", 2), | ||
("a", 1), | ||
("a", 1), | ||
("b", 2), | ||
("b", 2), | ||
("b", 2), | ||
("a", 1), | ||
("a", 1), | ||
("b", 2), | ||
("b", 2), | ||
("b", 2), | ||
("a", 1), | ||
("a", 1), | ||
("a", 1), | ||
] |