Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions docs/source/hyperparameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ modify the network. The `Trainer` can add all the available options to an Argume
parser.add_argument('--layer_1_dim', type=int, default=128)
parser.add_argument('--layer_2_dim', type=int, default=256)
parser.add_argument('--batch_size', type=int, default=64)

# add all the available options to the trainer
parser = pl.Trainer.add_argparse_args(parser)

args = parser.parse_args()

Now we can parametrize the LightningModule.
Expand Down
6 changes: 5 additions & 1 deletion docs/source/introduction_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,12 @@ modify the network. The `Trainer` can add all the available options to an Argume

# parametrize the network
parser.add_argument('--layer_1_dim', type=int, default=128)
parser.add_argument('--layer_1_dim', type=int, default=256)
parser.add_argument('--layer_2_dim', type=int, default=256)
parser.add_argument('--batch_size', type=int, default=64)

# add all the available options to the trainer
parser = pl.Trainer.add_argparse_args(parser)

args = parser.parse_args()

Now we can parametrize the LightningModule.
Expand Down
6 changes: 4 additions & 2 deletions pytorch_lightning/trainer/deprecated_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ def min_nb_epochs(self, min_epochs):
@property
def nb_sanity_val_steps(self):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `nb_sanity_val_steps` has renamed to `num_sanity_val_steps` since v0.5.0"
warnings.warn("Attribute `nb_sanity_val_steps` has renamed to "
"`num_sanity_val_steps` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
return self.num_sanity_val_steps

@nb_sanity_val_steps.setter
def nb_sanity_val_steps(self, nb):
"""Back compatibility, will be removed in v0.8.0"""
warnings.warn("Attribute `nb_sanity_val_steps` has renamed to `num_sanity_val_steps` since v0.5.0"
warnings.warn("Attribute `nb_sanity_val_steps` has renamed to "
"`num_sanity_val_steps` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
self.num_sanity_val_steps = nb
24 changes: 20 additions & 4 deletions pytorch_lightning/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ def __init__(
self.num_sanity_val_steps = num_sanity_val_steps
# Backward compatibility, TODO: remove in v0.8.0
if nb_sanity_val_steps is not None:
warnings.warn("Argument `nb_sanity_val_steps` has renamed to `num_sanity_val_steps` since v0.5.0"
warnings.warn("Argument `nb_sanity_val_steps` has renamed to "
"`num_sanity_val_steps` since v0.5.0"
" and this method will be removed in v0.8.0", DeprecationWarning)
self.nb_sanity_val_steps = nb_sanity_val_steps
self.print_nan_grads = print_nan_grads
Expand Down Expand Up @@ -437,17 +438,32 @@ def slurm_job_id(self) -> int:

@classmethod
def default_attributes(cls):
return vars(cls())
import inspect

init_signature = inspect.signature(Trainer)

args = {}
for param_name in init_signature.parameters:
value = init_signature.parameters[param_name].default
args[param_name] = value

return args

@classmethod
def add_argparse_args(cls, parent_parser: ArgumentParser) -> ArgumentParser:
"""Extend existing argparse by default `Trainer` attributes."""
parser = ArgumentParser(parents=[parent_parser])
parser = ArgumentParser(parents=[parent_parser], add_help=False)

trainer_default_params = Trainer.default_attributes()

# TODO: get "help" from docstring :)
for arg in trainer_default_params:
parser.add_argument('--{0}'.format(arg), default=trainer_default_params[arg], dest=arg)
parser.add_argument(
f'--{arg}',
default=trainer_default_params[arg],
dest=arg,
help='autogenerated by pl.Trainer'
)

return parser

Expand Down