-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Only invoke setup() once, not in both trainer.fit() and trainer.test() #2620
Comments
we could also just split the method fit_setup |
Or simply just don't calll |
that doesn’t happen no? it you call .test() only the setup(‘test’) gets called |
Since it calls |
oh! yeah that's a bug :) mind submitting a PR? nice catch!! |
I think this bug was reintroduced somehow. At least it is showing the same behavior again for me, with setup being called twice when testing after fitting. I'm new to lightning so I am not a big help yet, but I started digging a bit and at least found that it seems like the code from this fix is not in the current trainer code anymore? |
hey @AlexHarn , could you open up a new issue for this, along with example code to reproduce the behavior you're seeing? The code has changed quite a bit since when this issue was filed |
Yep, I'm actually doing that right now! |
🚀 Feature
Only invoke
def setup(self, step: str)
when callingtrainer.test(net)
if this has not been called before (e.g.trainer.fit(net)
).Motivation
The
setup
function is described in the docs as: "use setup to do splits, and build your model internals".Therefore, I wrote code that does the train-val-test function and some DataFrame labels transformation (e.g. label to one_hot) in this function.
A pretty common pattern is the following:
Contrary to what I expected, I saw from my debug output that
setup
was invoked twice.This is a waste of computational resources, and since I did the train-val split randomly, I do not have access to the indices that were used in either step (and possibly other issues such as the label transformation re-ordering the columns of which number represent which label).
Current situation, train and val step use same
setup
, test step uses another invoke ofsetup
I assume that it is more common that the train-val and test step of the trainer use the same setup code, than that setup does something special for only test (and not val).
As Lightning works by giving sensible defaults, and allowing you to hack at anything you want, the logic should be that
setup
should only be invoked once, and allow for a way to specify a special testsetup
function.Pitch
Have the trainer keep track of whether
setup()
has been invoked or not, sosetup()
can be skipped intrainer.test(net)
if it was already invoked intrainer.fit(net)
. This way the common use case will benefit from less computation power and is more in line what is expected of the magic of Lightning.I'm not sure what would be the best approach for users to set a separate setup call for testing.
Maybe something like:
trainer.test(net, always_invoke_setup=True)
?Alternatives
Include some custom logic that checks if data has been initialized:
Additional context
Ideas formed by discussing this issue on the pytorch-lightning SLACK in the questions channel. Thanks goed to the people who replied.
The text was updated successfully, but these errors were encountered: