Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using multiple trainers vs. single trainer if max_epochs needs to change. #3037

Closed
MarioIshac opened this issue Aug 18, 2020 · 6 comments
Closed
Assignees
Labels
question Further information is requested won't fix This will not be worked on

Comments

@MarioIshac
Copy link
Contributor

MarioIshac commented Aug 18, 2020

❓ Questions and Help

Before asking:

I've looked at Issue with running multiple models in PyTorch Lightning which talks about using multiple trainers for multiple models.

What is your question?

I want to clarify when I should be using multiple trainers vs. one trainer if I'm trying to train a bunch of models (all of BaseModel, a custom subclass of pytorch_lightning.LightningModule) which only differ among their hyperparameters (and there hyperparameters are provided as hparams in the instantiation of BaseModel). The catch is that the max epochs is a hyperparameter in my case.

Code

Given that train_model is being called for each model I want to train, do I go this route:

def train_model(model: BaseModel):
    trainer = pytorch_lightning.Trainer(..., max_epochs=model.hparams.max_epochs)
    trainer.fit(model)

or this route:

trainer = pytorch_lightning.Trainer(...)

def train_model(model: BaseModel):
    trainer.max_epochs = model.hparam.max_epochs
    trainer.fit(model)

Everything else about the trainer remains unchanged after initialization (such as gpus, logger, early_stop_callback, etc, this is all part of the ...). Only max_epochs needs to change.

What's your environment?

  • OS: Ubuntu 20.04
  • Packaging: pip
  • Version: 0.8.5
@MarioIshac MarioIshac added the question Further information is requested label Aug 18, 2020
@github-actions
Copy link
Contributor

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

@awaelchli
Copy link
Contributor

awaelchli commented Aug 19, 2020

@MarioIshac I would choose the first option, because the Trainer saves some state (e.g. in the loggers) that you probably don't want to reuse in the following model that will train.
Besides, I don't recommend setting attributes on the Trainer directly (like trainer.max_epochs = ...). These attributes are not meant to be modified from outside.

@stale
Copy link

stale bot commented Oct 22, 2020

This issue has been automatically marked as stale because it hasn't had any recent activity. This issue will be closed in 7 days if no further activity occurs. Thank you for your contributions, Pytorch Lightning Team!

@stale stale bot added the won't fix This will not be worked on label Oct 22, 2020
@drozzy
Copy link

drozzy commented Dec 9, 2020

I wish there was a bit more clarification on this in the docs, as I had to google for this.

@awaelchli
Copy link
Contributor

I'm sorry that you had to google it. Where did you find your answer? Here in github?
Any idea where would be a good place to mention this?

@drozzy
Copy link

drozzy commented Dec 10, 2020

Yeah your answer above.
I think good place for it would be here: https://pytorch-lightning.readthedocs.io/en/stable/trainer.html#
Something like "don't use the same trainer multiple times" etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested won't fix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants