Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/test pass overrides #918

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion pytorch_lightning/trainer/evaluation_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def evaluate(self, model, dataloaders, max_batches, test=False):

def run_evaluation(self, test=False):
# when testing make sure user defined a test step
if test and not (self.is_overriden('test_step') and self.is_overriden('test_end')):
if test and not (self.is_overriden('test_step') or self.is_overriden('test_end')):
m = '''You called `.test()` without defining model's `.test_step()` or `.test_end()`.
Please define and try again'''
raise MisconfigurationException(m)
Expand Down
34 changes: 34 additions & 0 deletions tests/test_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,5 +762,39 @@ def test_trainer_min_steps_and_epochs(tmpdir):
trainer.current_epoch > 0, "Model did not train for at least min_steps"


def test_testpass_overrides(tmpdir):
hparams = tutils.get_hparams()
from pytorch_lightning.utilities.debugging import MisconfigurationException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be loaded at the beginning of the file


class TestModelNoEnd(LightningTestModelBase):
def test_step(self, *args, **kwargs):
return {}

def test_dataloader(self):
return self.train_dataloader()

class TestModelNoStep(LightningTestModelBase):
def test_end(self, outputs):
return {}

def test_dataloader(self):
return self.train_dataloader()

# Misconfig when neither test_step or test_end is implemented
with pytest.raises(MisconfigurationException):
model = LightningTestModelBase(hparams)
Trainer().test(model)

# No exceptions when one or both of test_step or test_end are implemented
model = TestModelNoStep(hparams)
Trainer().test(model)

model = TestModelNoEnd(hparams)
Trainer().test(model)

model = LightningTestModel(hparams)
Trainer().test(model)


# if __name__ == '__main__':
# pytest.main([__file__])