If you need to pass some parameters when you run the python script, for example, gpus
and batch_size
can be used in three ways.
python script.py 0,1,2 10 python script.py -gpus=0,1,2 --batch-size=10 python script.py -gpus=0,1,2 --batch_size=10
These three different analytical methods format parameters correspond, respectively sys.argv
,argparse、
tf.app.run,
two are python carrying function, which is tensorflow
a convenient way to provide.
1.sys.argv
sys
Module is very common module, which encapsulates the data associated with the python interpreter, for example,
sys.modules
which has all of the loaded module information,
sys.path
which is
PYTHONPATH
content, and
sys.argv
then encapsulates the incoming data parameters.
Use
sys.argv
parametrically receiving the first command above comprising the following:
import sys gpus = sys.argv[1] #gpus = [int(gpus.split(','))] batch_size = sys.argv[2] print(gpus) print(batch_size)
2.argparse
import argparse parser = argparse.ArgumentParser(description='manual to this script') parser.add_argument("--gpus", type=str, default="0") parser.add_argument("--batch-size", type=int, default=32) args = parser.parse_args() print(args.gpus) print(args.batch_size)
python script.py -gpus=0,1,2 --batch-size=10
in
--batch-size
will be automatically parsed into
batch_size
.
parser.add_argument
Method
type
parameter can theoretically be any type of legitimate, but some argument to format is too much trouble, such as list, so the general use
bool
,
int
,
str
,
float
these basic types on the line the more complex needs can be
str
passed, and then manually resolve.
bool
Special type of parsing, any incoming value will be parsed into
True
, if passed as null
False
3.tf.app.run
import tensorflow as tf tf.app.flags.DEFINE_string('gpus', None, 'gpus to use') tf.app.flags.DEFINE_integer('batch_size', 5, 'batch size') FLAGS = tf.app.flags.FLAGS def main(_): print(FLAGS.gpus) print(FLAGS.batch_size) if __name__=="__main__": tf.app.run()
There are several points to note:
tensorflow
Provide only the following methods:
tf.app.flags.DEFINE_string
, , , Four methods, respectively , , , types of arguments. Here to resolve more stringent, incoming 1 will be parsed into , any remaining value will be parsed into .
tf.app.flags.DEFINE_integer
tf.app.flags.DEFINE_boolean
tf.app.flags.DEFINE_float
str
int
bool
float
bool
True
False
To receive a script you need to define a parameter of main
the method: def main(_):
This parameter is passed in the name of the script, generally with less than, the receiver underlined.
With batch_size
parameters for example, the name used when this parameter is passed --batch_size
, that is to say, not as underlined in argparse
the same was resolved to underline.
tf.app.run()
Entrance will find and execute the script main
method. Only in the execution tf.app.run()
order from then FLAGS
removed parameters.
From its signature look, it also can specify your own methods need to be performed, not necessarily have to be called main
: