Pytorch-lightning: Allow Trainer to accept dictionary as input?

Created on 2 Nov 2020  路  10Comments  路  Source: PyTorchLightning/pytorch-lightning

I searched issues and the forum but couldn't find anyone who mentioned this, so here I'm asking: can we allow Trainer to accept dictionaries as an input argument?

This came up as I'm writing a script to run experiments under various configurations. For each experiment, I'm passing a configuration file (in JSON) into an experiment runner which builds callbacks, Trainer, and models internally. At the moment, I'm doing something that looks like:

trainer = pl.Trainer(
                gpus                = config.trainer.gpus,
                auto_select_gpus    = config.trainer.auto_select_gpus,
                precision           = config.trainer.precision,
                deterministic       = config.trainer.deterministic,
                max_epochs          = config.trainer.max_epochs,
                auto_lr_find        = config.trainer.auto_lr_find,
                gradient_clip_val   = config.trainer.gradient_clip_val,
                logger              = logger,
                callbacks           = callbacks,
            )

However, I would like to be able to do:

trainer = pl.Trainer(config.trainer)

I think running experiments would be so much more flexible this way unless there is a technical reason that prevents this from happening.

discussion enhancement question

All 10 comments

Hi! thanks for your contribution!, great first issue!

trainer currently supports namespace/argparse, check out:
https://pytorch-lightning.readthedocs.io/en/stable/trainer.html#trainer-in-python-scripts

@s-rog sure, I can certainly convert dict to argparse namespace like this:

trainer_params = {
        # some params
    }

    parser = Trainer.add_argparse_args(argparse.ArgumentParser())
    args = parser.parse_args()
    vars(args).update(trainer_params)
    Trainer.from_argparse_args(args)

But this looks unnecessarily complicated. There should be an easier/cleaner way to do this? I just can't find one upfront.

@cheulyop is config.trainer a dict type?

@ydcjeff when I opened this issue, I thought it should be. But thinking of it now, it doesn't have to be?

Actually, in my code, config is a dict of dicts, such as experiment, data, early_stop, trainer, and hparams. I store config as json and load it with a hook converting dicts and their values into an object, i.e., namespace.

If it is namespace, I suppose the whole thing can be as simple as Trainer.from_argparse_args(config.trainer).

I don't think the documentation is very clear about this, neither the name from_argparse_args :/

If config.trainer is a dict, you could use double asterisk, right?

@ydcjeff yes you can do Namespace(**config.trainer)

How about in this? pl.Trainer(**config.trainer) that can be used too?

Yes that works as well, thanks for the clarification 馃憤

Thank you for confirming!

Closing it as it is resolved, feel free to reopen if needed

Was this page helpful?
0 / 5 - 0 ratings