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
How to clean flags ? #36
Comments
If "blank state" means "reset all flag values to the default values", you can call If "blank state" means "remove all flags as if no flags are defined", you can do something like: from absl import flags
for name in list(flags.FLAGS):
delattr(flags.FLAGS, name) But this means if you call other libraries that uses the flags they have defined previously, they won't work anymore (unless you define the same flags again). So I'm curious, what is your use case? I assume you already have some code that does this before Tensorflow switch to this library, do you have an example? And if you just want a separate from absl import flags
MY_FLAGS = flags.FlagValues() # This is a separate "blank" instance different from the "global" flags.FLAGS instance. |
@yilei thanks for your answer. Your snippet did perfectly the job. from absl import flags
for name in list(flags.FLAGS):
delattr(flags.FLAGS, name) For your question about the use-case, resetting the flags is actually the only way I found to achieve my objective. Let's take a simple example, I want to create a GAN network composed of it's Generator and Discriminator or a VAE with its decoder and encoder. I want to create unittests for each part of the network namely the Encoder, Decoder and full VAE network or the Generator, Discriminator and full GAN network. The problem is that the tests for each sub-part is written in different files and when I create master_test file calling all of the others I have flag collisions. Each test file creates its own set of flags with different conditions and then I initialize everything from this. It works perfectly when I call each test file separately, however, when I want to call all of them from the same master file and execute them in a row. I need to reset my flags each time I switch to a test in a different test file. I would be very curious how you do this at google, I'm convinced there's a far better way doing this. |
I would ordinarily point out That you seem to be wanting to redefine conflicting sets of flags from your test files themselves makes me think that you are not doing your flag definitions in the "right" place. A test file should not be defining flags. Flag definitions should happen in top level of each library that is going to use them (or for widely shared flags, in a shared library that everything expected to use such a flag depends on). When you've done it that way, they are defined in only one place, and you can use flagsaver to restore values to defaults after each test so that you can setup whatever set of values you need them to have in the next test by simply assigning values to to them. It is highly unusual to want to undefine flags within a process. Hopefully I'm making sense here. |
Usually each of our test files is run as separate binaries, so different flags from different test files don't interact. (This is done via bazel.) In your case, each test file is imported Do you need to specify the flags defined in individual test files on the command line?
# File: shared_flags.py
flags.DEFINE_string('encoder', None, 'The encoder to use') # File: test_a.py
def test_a():
encoder = create(FLAGS.encoder)
... # File: test_b.py
def test_b():
encoder = create(FLAGS.encoder)
... # File: master_test.py
from absl import app
from absl import flags
from absl.testing import flagsaver
import shared_flags # Import to define --encoder.
import test_a
import test_b
FLAGS = flags.FLAGS
def main(argv):
test_cases = [('encoder a', test_a.test_a), ('encoder b', test_b.test_b)]
for encoder, test_func in test_funcs:
with flagsaver.flagsaver():
FLAGS.encoder = encoder
test_func()
if __name__ == '__main__':
app.run(main) |
Thanks for your writing.
but it gives the following error: from absl import flags
How can I solve it? otherwise, when I run again my code it give the error: Is there any suggestion please? thanks |
That snippets will delete all defined flags, so that What do you really want by saying "delete or reset or clean flags"? Do you mean to reset all flag values, but keep the definitions? Or do you really want to delete them, since
When you say "I run again my code", how did you run again your code? Is this a function call? module reload? a new process? Sorry without more context/details it's really hard to know what's going on. |
@yilei
When I ran in second time, it gives the following error:
How can I solve it? I would like to run the program several times as I wish? Thanks |
Looks like you are using IPython, I'm not very familiar with IPython but Line 250 in 06edd9c
And So the conclusion is probably that this won't work with your IPython workflow. If you have the code: from absl import app
from absl import flags
FLAGS = flags.FLAGS
flags.DEFINE_string('name', 'Jane Random', 'Your name.')
def main(argv):
print('Happy Birthday', FLAGS.name)
if __name__ == '__main__':
app.run(main) In a file called python main.py will work. |
Hello dear friends,
A few weeks ago, Tensorflow have moved to this library to manage flags using tf.app.flags.
Link to file: https://github.com/tensorflow/tensorflow/blob/r1.5/tensorflow/python/platform/flags.py
I can't manage to find any way to "reset" the flags state with this library. Is there any way to do this ?
I would like to remove every flags created and return to a blank state as if it was just created.
Thanks a lot.
The text was updated successfully, but these errors were encountered: