flags defines a distributed command line system, replacing systems like getopt() , optparse , and manual argument processing. Rather than an application having to define all flags in or near main() , each Python module defines flags that are useful to it.
means current file is executed under a shell instead of imported as a module. tf.app.run() As you can see through the file app.py def run(main=None, argv=None): """Runs the program with an optional 'main' function and 'argv' list.""" f = flags.FLAGS # Extract the args from the optional `argv` list.
The tf.app.flags
module is presently a thin wrapper around python-gflags, so the documentation for that project is the best resource for how to use it argparse
, which implements a subset of the functionality in python-gflags
.
Note that this module is currently packaged as a convenience for writing demo apps, and is not technically part of the public API, so it may change in future.
We recommend that you implement your own flag parsing using argparse
or whatever library you prefer.
EDIT: The tf.app.flags
module is not in fact implemented using python-gflags
, but it uses a similar API.
The tf.app.flags
module is a functionality provided by Tensorflow to implement command line flags for your Tensorflow program. As an example, the code you came across would do the following:
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
The first parameter defines the name of the flag while the second defines the default value in case the flag is not specified while executing the file.
So if you run the following:
$ python fully_connected_feed.py --learning_rate 1.00
then the learning rate is set to 1.00 and will remain 0.01 if the flag is not specified.
As mentioned in this article, the docs are probably not present because this might be something that Google requires internally for its developers to use.
Also, as mentioned in the post, there are several advantages of using Tensorflow flags over flag functionality provided by other Python packages such as argparse
especially when dealing with Tensorflow models, the most important being that you can supply Tensorflow specific information to the code such as information about which GPU to use.
Short Answer:
At Google, they use flag systems to set default values for arguments. It's similar to argparse. They use their own flag system instead of argparse or sys.argv.
Source: I worked there before.
Long Answer:
For the arguments you have in that example, they are called hyperparameters. In neural network there are multiple parameters you can optimize in order to get a a desired results. For example, for batch_size, it's the number of data vector (This can be image, text, or raw data points) that can be passed in a single shot to the optimizer.
You can Google the name of the argument, and see what's the purpose of it of it. If you want to learn about Deep Learning, I recommend you take Andrew Ng course.
When you use tf.app.run()
, you can transfer the variable very conveniently between threads using tf.app.flags
. See this for further usage of tf.app.flags
.
After trying many times I found this to print all FLAGS key as well as actual value -
for key in tf.app.flags.FLAGS.flag_values_dict():
print(key, FLAGS[key].value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With