remove extra kwargs from Trainer init (#1820)

* remove kwargs

* remove useless test

* rename unknown trainer flag

* trainer inheritance and test

* blank line

* test for unknown arg

* changelog
This commit is contained in:
Adrian Wälchli
2020-05-17 09:14:54 -04:00
committed by GitHub
parent 692f302837
commit 769a459d27
5 changed files with 46 additions and 26 deletions
+1 -20
View File
@@ -289,7 +289,7 @@ def test_inf_train_dataloader(tmpdir, check_interval):
trainer = Trainer(
default_root_dir=tmpdir,
max_epochs=1,
train_check_interval=check_interval,
val_check_interval=check_interval
)
result = trainer.fit(model)
# verify training completed
@@ -315,25 +315,6 @@ def test_inf_val_dataloader(tmpdir, check_interval):
assert result == 1
@pytest.mark.parametrize('check_interval', [50, 1.0])
def test_inf_test_dataloader(tmpdir, check_interval):
"""Test inf test data loader (e.g. IterableDataset)"""
model = EvalModelTemplate()
model.test_dataloader = model.test_dataloader__infinite
# logger file to get meta
trainer = Trainer(
default_root_dir=tmpdir,
max_epochs=1,
test_check_interval=check_interval,
)
result = trainer.fit(model)
# verify training completed
assert result == 1
def test_error_on_zero_len_dataloader(tmpdir):
""" Test that error is raised if a zero-length dataloader is defined """
+38
View File
@@ -772,3 +772,41 @@ def test_trainer_config(trainer_kwargs, expected):
assert trainer.on_gpu is expected["on_gpu"]
assert trainer.single_gpu is expected["single_gpu"]
assert trainer.num_processes == expected["num_processes"]
def test_trainer_subclassing():
model = EvalModelTemplate()
# First way of pulling out args from signature is to list them
class TrainerSubclass(Trainer):
def __init__(self, custom_arg, *args, custom_kwarg='test', **kwargs):
super().__init__(*args, **kwargs)
self.custom_arg = custom_arg
self.custom_kwarg = custom_kwarg
trainer = TrainerSubclass(123, custom_kwarg='custom', fast_dev_run=True)
result = trainer.fit(model)
assert result == 1
assert trainer.custom_arg == 123
assert trainer.custom_kwarg == 'custom'
assert trainer.fast_dev_run
# Second way is to pop from the dict
# It's a special case because Trainer does not have any positional args
class TrainerSubclass(Trainer):
def __init__(self, **kwargs):
self.custom_arg = kwargs.pop('custom_arg', 0)
self.custom_kwarg = kwargs.pop('custom_kwarg', 'test')
super().__init__(**kwargs)
trainer = TrainerSubclass(custom_kwarg='custom', fast_dev_run=True)
result = trainer.fit(model)
assert result == 1
assert trainer.custom_kwarg == 'custom'
assert trainer.fast_dev_run
# when we pass in an unknown arg, the base class should complain
with pytest.raises(TypeError, match=r"__init__\(\) got an unexpected keyword argument 'abcdefg'") as e:
TrainerSubclass(abcdefg='unknown_arg')