2020-10-13 11:18:07 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-10-03 18:05:01 +00:00
|
|
|
import pytest
|
2021-01-14 12:51:20 +00:00
|
|
|
|
2020-10-03 18:05:01 +00:00
|
|
|
from pytorch_lightning.trainer import Trainer
|
2021-02-09 10:10:52 +00:00
|
|
|
from tests.helpers import BoringModel
|
2020-10-03 18:05:01 +00:00
|
|
|
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
@pytest.mark.parametrize("max_epochs", [1, 2, 3])
|
|
|
|
@pytest.mark.parametrize("denominator", [1, 3, 4])
|
2021-03-06 12:40:19 +00:00
|
|
|
def test_val_check_interval(tmpdir, max_epochs, denominator):
|
2021-02-08 08:52:54 +00:00
|
|
|
class TestModel(BoringModel):
|
2020-10-03 18:05:01 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.train_epoch_calls = 0
|
|
|
|
self.val_epoch_calls = 0
|
|
|
|
|
|
|
|
def on_train_epoch_start(self) -> None:
|
|
|
|
self.train_epoch_calls += 1
|
|
|
|
|
|
|
|
def on_validation_epoch_start(self) -> None:
|
2021-03-06 12:40:19 +00:00
|
|
|
if not self.trainer.sanity_checking:
|
2020-10-03 18:05:01 +00:00
|
|
|
self.val_epoch_calls += 1
|
|
|
|
|
|
|
|
model = TestModel()
|
2021-07-26 11:37:35 +00:00
|
|
|
trainer = Trainer(max_epochs=max_epochs, val_check_interval=1 / denominator, logger=False)
|
2020-10-03 18:05:01 +00:00
|
|
|
trainer.fit(model)
|
|
|
|
|
2021-03-06 12:40:19 +00:00
|
|
|
assert model.train_epoch_calls == max_epochs
|
|
|
|
assert model.val_epoch_calls == max_epochs * denominator
|