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

dataframes passed outside hparams causing issues #2250

Closed
s-rog opened this issue Jun 19, 2020 · 7 comments · Fixed by #2253
Closed

dataframes passed outside hparams causing issues #2250

s-rog opened this issue Jun 19, 2020 · 7 comments · Fixed by #2253
Labels
bug Something isn't working help wanted Open to be worked on
Milestone

Comments

@s-rog
Copy link
Contributor

s-rog commented Jun 19, 2020

🐛 Bug

In 0.8.0 (updating from 0.7.5), using hyperparameters parsed by test-tube causes an exception.

In 0.8.0 (updating from 0.7.5), dataframes passed outside hparams errors out

To Reproduce

import argparse
import pandas as pd
import pytorch_lightning as pl

parser = argparse.ArgumentParser()
parser.add_argument('--hparam1', default=8, type=int)
parser.add_argument('--hparam2', default="test", type=str)
parser.add_argument('--hparam3', default=True, type=bool)
hparams = parser.parse_args()

some_data = pd.DataFrame([1, 2, 3])

class module(pl.LightningModule):
    def __init__(self, hparams, some_data):
        super(module, self).__init__()
        self.hparams = hparams
        self.data = some_data

    def train_dataloader(self):
        return

    def val_dataloader(self):
        return

    def configure_optimizers(self):
        return
    
    def forward(self, x):
        return x
    
    def training_step(self, batch, batch_idx):
        return batch

    def validation_step(self, batch, batch_idx):
        return batch

model = module(hparams, some_data)
Traceback (most recent call last):
  File "train_test.py", line 37, in <module>
    model = module(hparams, some_data)
  File "train_test.py", line 16, in __init__
    self.hparams = hparams
  File "/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py", line 638, in __setattr__
    object.__setattr__(self, name, value)
  File "/opt/conda/lib/python3.6/site-packages/pytorch_lightning/core/lightning.py", line 1695, in hparams
    self.save_hyperparameters(hp, frame=inspect.currentframe().f_back.f_back)
  File "/opt/conda/lib/python3.6/site-packages/pytorch_lightning/core/lightning.py", line 1662, in save_hyperparameters
    cand_names = [k for k, v in init_args.items() if v == hp]
  File "/opt/conda/lib/python3.6/site-packages/pytorch_lightning/core/lightning.py", line 1662, in <listcomp>
    cand_names = [k for k, v in init_args.items() if v == hp]
  File "/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py", line 1479, in __nonzero__
    f"The truth value of a {type(self).__name__} is ambiguous. "
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Expected behavior

hparams is saved correctly as previous versions, disregarding the dataframes

@s-rog s-rog added bug Something isn't working help wanted Open to be worked on labels Jun 19, 2020
@williamFalcon
Copy link
Contributor

williamFalcon commented Jun 19, 2020

ummm wait... TTNamespace is a Namespace... this is weird.

can you post minimal code to reproduce?

@s-rog s-rog changed the title test-tube parsed hyperparameters no longer supported? argparse hyperparameters no longer supported? Jun 19, 2020
@s-rog
Copy link
Contributor Author

s-rog commented Jun 19, 2020

Yeah, I swapped over to pure argparse to test and my hparams is <class 'argparse.Namespace'> but it doesn't seem to evaluate correctly... let me compile it

@williamFalcon
Copy link
Contributor

argparse is 100% supported. we have explicit tests for this

@s-rog
Copy link
Contributor Author

s-rog commented Jun 19, 2020

import argparse
import pandas as pd
import pytorch_lightning as pl

parser = argparse.ArgumentParser()
parser.add_argument('--hparam1', default=8, type=int)
parser.add_argument('--hparam2', default="test", type=str)
parser.add_argument('--hparam3', default=True, type=bool)
hparams = parser.parse_args()

some_data = pd.DataFrame([1, 2, 3])

class module(pl.LightningModule):
    def __init__(self, hparams, some_data):
        super(module, self).__init__()
        self.hparams = hparams
        self.data = some_data

    def train_dataloader(self):
        return

    def val_dataloader(self):
        return

    def configure_optimizers(self):
        return
    
    def forward(self, x):
        return x
    
    def training_step(self, batch, batch_idx):
        return batch

    def validation_step(self, batch, batch_idx):
        return batch

model = module(hparams, some_data)

ok I just tested it, it's caused by dataframes passed outside hparams

Edit: updated the original post to reflect the true source of the bug

@s-rog s-rog changed the title argparse hyperparameters no longer supported? dataframes passed outside hparams causing issues Jun 19, 2020
@williamFalcon
Copy link
Contributor

got it.
Found the bug!

@Borda

         self.save_hyperparameters(hp, frame=inspect.currentframe().f_back.f_back)

we shouldn't be doing frame inspection here...

@s-rog
Copy link
Contributor Author

s-rog commented Jun 19, 2020

thanks for the quick response, I'll pull from the fork and give it a shot

@williamFalcon
Copy link
Contributor

williamFalcon commented Jun 19, 2020

@Borda Borda added this to the 0.8.x milestone Jun 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Open to be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants