Skip to content

Trainer.from_argparse_args fails on unknown kwargs #1929

@nateraw

Description

@nateraw

🐛 Bug

Since **kwargs was removed from Trainer's init in #1820, initializing Trainer objects fails if you have any non Trainer specific arguments in your parser.

If this is the expected behavior, the docs should be updated to reflect the workaround I mention below, as a few of them would currently fail.

To Reproduce

This works

parser = ArgumentParser()
parser = Trainer.add_argparse_args(parser)
args = parser.parse_args("")
trainer = Trainer.from_argparse_args(args)

Trainer init fails on unexpected kwarg 'script_specific_arg'

parser = ArgumentParser()
parser = Trainer.add_argparse_args(parser)

parser.add_argument('--script_specific_arg', type=str, default='hope this works')

args = parser.parse_args('')
trainer = Trainer.from_argparse_args(args)

Trainer init fails on unexpected kwarg 'some_argument'

class SomeModel(LightningModule):

    def __init__(self, hparams):
        super().__init__()
        self.hparams = hparams

    @staticmethod
    def add_model_specific_args(parent_parser):
        parser = ArgumentParser(parents=[parent_parser], add_help=False)
        parser.add_argument('--some_argument', type=int, default=128)
        return parser


parser = ArgumentParser()
parser = Trainer.add_argparse_args(parser)

parser = SomeModel.add_model_specific_args(parser)

args = parser.parse_args("")
trainer = Trainer.from_argparse_args(args)

You can get around it if you init Trainer on temp args

parser = ArgumentParser()
parser = Trainer.add_argparse_args(parser)

# Grab only the trainer args and init it right away
temp_args, _ = parser.parse_known_args('')
trainer = Trainer.from_argparse_args(temp_args)

parser.add_argument('--script_specific_arg', type=str, default='hope this works')
args = parser.parse_args("")

Expected behavior

Trainer.from_argparse_args should ignore unknown kwargs.

Environment

  • Google Colab
  • Current master branch (0.7.7.dev0)

Additional context

  • No error if using stable PyPi version (0.7.6)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions