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

What is the best practice to define a model? #1427

Closed
RafailFridman opened this issue Apr 9, 2020 · 1 comment
Closed

What is the best practice to define a model? #1427

RafailFridman opened this issue Apr 9, 2020 · 1 comment
Labels
question Further information is requested

Comments

@RafailFridman
Copy link

I want to try many architectures and it is not convenient to copy all the loading/train/val/test logic from file to file. Currently, I create a "container", where I define all the common logic, and then I inherit that class. It looks like this:

class ModelContainer(pl.LightningModule):
    
    def __init__(self,hparams):

        super().__init__()

    def prepare_data(self):
    ....
    def configure_optimizers(self):
    ....
    def train_dataloader(self):
    ....

And in another file I create a model, where I redefine only a forward method:

class MyCoolNet(ModelContainer):
    def __init__(self, hparams):
        super().__init__(hparams)

    def forward(self, x):
    ...

Am I doing it the right way?

@RafailFridman RafailFridman added the question Further information is requested label Apr 9, 2020
@williamFalcon
Copy link
Contributor

williamFalcon commented Apr 9, 2020

Overall:

class Root(LightningModule):
  def __init__
  def forward
  def training_step
  def train_dataloader
  def val_dataloader

class ModelA(Root):
   def training_step
   def validation_step

class ModelB(Root):
   def training_step
   def validation_step

In ModelA, B you only want to change training_step (ie the inner loop).

https://pytorch-lightning.readthedocs.io/en/0.7.1/introduction_guide.html#child-modules

I recommend to read this full guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants