Use of Tensorflow tf.app.flags

Carried out prior to performing the main function first parses the flags, that TensorFlow to pass tf.app.run by setting flags () required parameters, we can directly initialize flags before the program is running, you can also set the time to run the program command line parameters to achieve the purpose of parameter passing.

Here is a small demo

import tensorflow as tf

flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string("name", "x1aolata", "name")
flags.DEFINE_integer("age", 23, "age")
flags.DEFINE_boolean("isMarried", None, "isMarried")
flags.DEFINE_float("score", 98.5, "score")


def main(_):
    print("name:{}".format(FLAGS.name))
    print("age:{}".format(FLAGS.age))
    print("isMarried:{}".format(FLAGS.isMarried))
    print("score:{}".format(FLAGS.score))


if __name__ == "__main__":
    tf.app.run()

flags.DEFINE_string these is the definition of the parameters
of its three parameters are parameter names, default values and parameters.

Direct operation results are as follows

name:x1aolata
age:23
isMarried:None
score:98.5

Given the parameters can run from the command line

$ python test.py --name=小邋遢 --age=32

operation result

name:小邋遢
age:32
isMarried:None
score:98.5

Guess you like

Origin www.cnblogs.com/x1aolata/p/11334416.html