Skip to content

Commit

Permalink
Merge pull request #32 from nathancooperjones/main
Browse files Browse the repository at this point in the history
Fix unit tests error, failures, and warnings
  • Loading branch information
gsganden committed Aug 17, 2021
2 parents 2a7b027 + ae10388 commit 6c9b1c2
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 33 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project uses [Semantic Versioning](http://semver.org/).

# [1.1.1] - 2021-8-16
### Changed
- added property ``max_epochs`` to ``CollieTrainer`` and ``CollieMinimalTrainer`` with ``setter`` method
- `CollieTrainer`'s default `max_epochs` from `1000` to `10`
### Fixed
- used new API for setting verbosity in ``ModelSummary`` in ``CollieMinimalTrainer``

# [1.1.0] - 2021-7-15
### Added
- multi-stage model template ``MultiStagePipeline``
Expand Down
2 changes: 1 addition & 1 deletion collie/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.1.0'
__version__ = '1.1.1'
63 changes: 62 additions & 1 deletion collie/model/base/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class CollieTrainer(Trainer):
----------
model: collie.model.BasePipeline
Initialized Collie model
max_epochs: int
Stop training once this number of epochs is reached
benchmark: bool
If set to ``True``, enables ``cudnn.benchmark``
deterministic: bool
Expand All @@ -48,6 +50,7 @@ class CollieTrainer(Trainer):
"""
def __init__(self,
model: torch.nn.Module,
max_epochs: int = 10,
benchmark: bool = True,
deterministic: bool = True,
**kwargs):
Expand All @@ -60,11 +63,42 @@ def __init__(self,
print('Detected GPU. Setting ``gpus`` to 1.')
kwargs['gpus'] = 1

kwargs['max_epochs'] = max_epochs
kwargs['benchmark'] = benchmark
kwargs['deterministic'] = deterministic

super().__init__(**kwargs)

@property
def max_epochs(self):
"""
Property that just returns ``max_epochs``, included only so we can have
a setter for it without an ``AttributeError``.
"""
try:
return self.fit_loop.max_epochs
except AttributeError:
# compatible with old Pytorch Lightning ``Trainer`` API prior to version ``1.4.0``
return self._max_epochs

@max_epochs.setter
def max_epochs(self, value: int):
"""
Set the ``max_epochs`` attribute to ``value``.
Parameters
----------
value: int
Value to set ``max_epochs`` attribute to
"""
try:
self.fit_loop.max_epochs = value
except AttributeError:
# compatible with old Pytorch Lightning ``Trainer`` API prior to version ``1.4.0``
self._max_epochs = value


class CollieMinimalTrainer():
"""
Expand Down Expand Up @@ -212,6 +246,28 @@ def __init__(self,
torch.backends.cudnn.benchmark = self.benchmark
torch.backends.cudnn.deterministic = self.deterministic

@property
def max_epochs(self):
"""
Property that just returns ``max_epochs``, included only so we can have
a setter for it without an ``AttributeError``.
"""
return self._max_epochs

@max_epochs.setter
def max_epochs(self, value: int):
"""
Set the ``max_epochs`` attribute to ``value``.
Parameters
----------
value: int
Value to set ``max_epochs`` attribute to
"""
self._max_epochs = value

def fit(self, model: BasePipeline) -> None:
"""
Runs the full optimization routine.
Expand Down Expand Up @@ -304,7 +360,12 @@ def _pre_training_setup(self, model: BasePipeline) -> None:
self.val_dataloader = model.val_dataloader()

if self.verbosity != 0 and self.weights_summary is not None:
print(ModelSummary(model, mode=self.weights_summary))
try:
max_depth = ModelSummary.MODES[self.weights_summary]
print(ModelSummary(model, max_depth=max_depth))
except TypeError:
# compatible with old ``ModelSummary`` API used in versions prior to ``1.6``
print(ModelSummary(model, mode=self.weights_summary))

# log model hyperparameters, if applicable
if self.logger is not None:
Expand Down
18 changes: 9 additions & 9 deletions tests/fixtures/model_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def models_trained_for_one_step(request,

model_trainer = CollieTrainer(model=model,
gpus=gpu_count,
max_steps=1,
max_epochs=1,
deterministic=True,
logger=False,
checkpoint_callback=False)
Expand Down Expand Up @@ -377,7 +377,7 @@ def models_trained_for_one_step(request,
optimizer='adam')
implicit_model_trainer = CollieTrainer(model=implicit_model,
gpus=gpu_count,
max_steps=1,
max_epochs=1,
deterministic=True,
logger=False,
checkpoint_callback=False)
Expand All @@ -401,7 +401,7 @@ def models_trained_for_one_step(request,
weight_decay=0.0)
model_frozen_trainer = CollieTrainer(model=model_frozen,
gpus=gpu_count,
max_steps=1,
max_epochs=1,
deterministic=True,
logger=False,
checkpoint_callback=False)
Expand Down Expand Up @@ -482,7 +482,7 @@ def models_trained_for_one_step(request,

model_trainer = CollieTrainer(model=model,
gpus=gpu_count,
max_steps=1,
max_epochs=1,
deterministic=True,
logger=False,
checkpoint_callback=False)
Expand All @@ -498,7 +498,7 @@ def models_trained_for_one_step(request,
model_trainer.fit(model)

if idx < (number_of_stages - 1):
model_trainer.max_steps += 1
model_trainer.max_epochs += 1
model.advance_stage()

model.eval()
Expand Down Expand Up @@ -578,7 +578,7 @@ def explicit_models_trained_for_one_step(request,
loss='mse')
implicit_model_trainer = CollieTrainer(model=implicit_model,
gpus=gpu_count,
max_steps=1,
max_epochs=1,
deterministic=True,
logger=False,
checkpoint_callback=False)
Expand Down Expand Up @@ -609,7 +609,7 @@ def explicit_models_trained_for_one_step(request,
weight_decay=0.0)
model_frozen_trainer = CollieTrainer(model=model_frozen,
gpus=gpu_count,
max_steps=1,
max_epochs=1,
deterministic=True,
logger=False,
checkpoint_callback=False)
Expand Down Expand Up @@ -654,7 +654,7 @@ def explicit_models_trained_for_one_step(request,

model_trainer = CollieTrainer(model=model,
gpus=gpu_count,
max_steps=1,
max_epochs=1,
deterministic=True,
logger=False,
checkpoint_callback=False)
Expand All @@ -670,7 +670,7 @@ def explicit_models_trained_for_one_step(request,
model_trainer.fit(model)

if idx < (number_of_stages - 1):
model_trainer.max_steps += 1
model_trainer.max_epochs += 1
model.advance_stage()

model.freeze()
Expand Down

0 comments on commit 6c9b1c2

Please sign in to comment.