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

When no val dataloader is present and user implements validation_step need to throw useful error #508

Closed
Ne0Ment opened this issue Nov 14, 2019 · 5 comments
Labels
feature Is an improvement or enhancement help wanted Open to be worked on

Comments

@Ne0Ment
Copy link

Ne0Ment commented Nov 14, 2019

I have been using pytorch before, but for performance I decided to use lightning. I rewrote my pytorch into a pl.LightningModule class. While training for the first epoch, everything seems fine, but when it starts validation there is a TypeError: 'NoneType' object is not iterable.
Epoch 1: 100%|████████████████████████| 6514/6514 [01:27<00:00, 71.17batch/s, batch_nb=6513, gpu=0, loss=1.099, v_nb=7]
Validating: 0batch [00:00, ?batch/s]

train_dataset = t.utils.data.TensorDataset(X,y)
trainloader = t.utils.data.DataLoader(train_dataset, batch_size=64)

class FastNN(pl.LightningModule):

    def __init__(self, dataload):
        super(FastNN, self).__init__()
        self.fc1 = nn.Linear(110, 1024)
        self.fc2 = nn.Linear(1024, 512)
        self.fc3 = nn.Linear(512, 256)
        self.fc4 = nn.Linear(256, 128)
        self.fc5 = nn.Linear(128, 64)
        self.fc6 = nn.Linear(64, 32)
        self.fc7 = nn.Linear(32, 3)
        self.dataloader = dataload

    def forward(self, x):
        x = f.relu(self.fc1(x))
        x = f.relu(self.fc2(x))
        x = f.relu(self.fc3(x))
        x = f.relu(self.fc4(x))
        x = f.relu(self.fc5(x))
        x = f.relu(self.fc6(x))
        x = f.relu(self.fc7(x))
        return x

    def training_step(self, batch, batch_nb):
        # REQUIRED
        x, y = batch
        y_hat = self.forward(x)
        loss = t.nn.functional.cross_entropy(y_hat, y)
        tensorboard_logs = {'train_loss': loss}
        return {'loss': loss, 'log': tensorboard_logs}

    def validation_step(self, batch, batch_nb):
        # OPTIONAL
        x, y = batch
        y_hat = self.forward(x)
        return {'val_loss': t.nn.functional.cross_entropy(y_hat, y)}

    def validation_end(self, outputs):
        # OPTIONAL
        avg_loss = t.stack([x['val_loss'] for x in outputs]).mean()
        tensorboard_logs = {'val_loss': avg_loss}
        return {'avg_val_loss': avg_loss, 'log': tensorboard_logs}

    def configure_optimizers(self):
        # REQUIRED
        # can return multiple optimizers and learning_rate schedulers
        # (LBFGS it is automatically supported, no need for closure function)
        return t.optim.Adam(self.parameters(), lr=0.02)

    @pl.data_loader
    def train_dataloader(self):
        # REQUIRED
        return self.dataloader

net = FastNN(trainloader)
trainer = pl.Trainer(gpus=1)    
trainer.fit(net)
@Ne0Ment Ne0Ment added the bug Something isn't working label Nov 14, 2019
@williamFalcon
Copy link
Contributor

You have to add the val_dataloader method:

    @pl.data_loader
    def val_dataloader(self):
        # REQUIRED
        return self.other_dataloader

@williamFalcon williamFalcon changed the title Validation dataloader error When no val dataloader is present and user implements validation_step need to throw useful error Nov 14, 2019
@williamFalcon
Copy link
Contributor

williamFalcon commented Nov 14, 2019

@ILYEEV thanks for pointing out though. We should have a nicer user experience for this error.

@Ne0Ment Ne0Ment closed this as completed Nov 15, 2019
@williamFalcon williamFalcon reopened this Nov 15, 2019
@williamFalcon williamFalcon added feature Is an improvement or enhancement help wanted Open to be worked on and removed bug Something isn't working labels Nov 15, 2019
@stale
Copy link

stale bot commented Feb 22, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

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

Borda commented Feb 22, 2020

@ILYEEV are you interested in sending PR for a better warning? maybe even do this kind of check before starting the training... something like self-diagnostic?

@stale stale bot removed the won't fix This will not be worked on label Feb 22, 2020
@awaelchli
Copy link
Member

This is fixed on master.
If the user implements validation_step but no validation dataloader, it throws an exception:

pytorch_lightning.utilities.exceptions.MisconfigurationException: 
You have defined `validation_step()`, but have not passed in a val_dataloader().

This issue can be closed.

@Borda Borda closed this as completed Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is an improvement or enhancement help wanted Open to be worked on
Projects
None yet
Development

No branches or pull requests

4 participants