tf.app.run()如何工作?

问题:tf.app.run()如何工作?

tf.app.run()Tensorflow 中的工作如何翻译演示?

在中tensorflow/models/rnn/translate/translate.py,有一个呼叫到tf.app.run()。如何处理?

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

How does tf.app.run() work in Tensorflow translate demo?

In tensorflow/models/rnn/translate/translate.py, there is a call to tf.app.run(). How is it being handled?

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

回答 0

if __name__ == "__main__":

表示当前文件在shell下执行,而不是作为模块导入。

tf.app.run()

如您所见,通过文件 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.
  args = argv[1:] if argv else None

  # Parse the known flags from that list, or from the command
  # line otherwise.
  # pylint: disable=protected-access
  flags_passthrough = f._parse_flags(args=args)
  # pylint: enable=protected-access

  main = main or sys.modules['__main__'].main

  # Call the main function, passing through any arguments
  # to the final program.
  sys.exit(main(sys.argv[:1] + flags_passthrough))

让我们逐行中断:

flags_passthrough = f._parse_flags(args=args)

这样可以确保您通过命令行传递的参数有效,例如, python my_model.py --data_dir='...' --max_iteration=10000实际上,此功能是基于python标准argparse模块实现的。

main = main or sys.modules['__main__'].main

第一个main在右边=是当前函数的第一个参数run(main=None, argv=None) 。While sys.modules['__main__']表示当前正在运行的文件(例如my_model.py)。

因此有两种情况:

  1. 您没有的main功能,my_model.py那么您必须调用tf.app.run(my_main_running_function)

  2. 您在中具有main功能my_model.py。(通常是这种情况。)

最后一行:

sys.exit(main(sys.argv[:1] + flags_passthrough))

确保使用解析后的参数正确调用您的main(argv)or my_main_running_function(argv)函数。

if __name__ == "__main__":

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.
  args = argv[1:] if argv else None

  # Parse the known flags from that list, or from the command
  # line otherwise.
  # pylint: disable=protected-access
  flags_passthrough = f._parse_flags(args=args)
  # pylint: enable=protected-access

  main = main or sys.modules['__main__'].main

  # Call the main function, passing through any arguments
  # to the final program.
  sys.exit(main(sys.argv[:1] + flags_passthrough))

Let’s break line by line:

flags_passthrough = f._parse_flags(args=args)

This ensures that the argument you pass through command line is valid,e.g. python my_model.py --data_dir='...' --max_iteration=10000 Actually, this feature is implemented based on python standard argparse module.

main = main or sys.modules['__main__'].main

The first main in right side of = is the first argument of current function run(main=None, argv=None) . While sys.modules['__main__'] means current running file(e.g. my_model.py).

So there are two cases:

  1. You don’t have a main function in my_model.py Then you have to call tf.app.run(my_main_running_function)

  2. you have a main function in my_model.py. (This is mostly the case.)

Last line:

sys.exit(main(sys.argv[:1] + flags_passthrough))

ensures your main(argv) or my_main_running_function(argv) function is called with parsed arguments properly.


回答 1

这只是一个非常快速的包装程序,可以处理标志解析,然后分派到您自己的主程序。参见代码

It’s just a very quick wrapper that handles flag parsing and then dispatches to your own main. See the code.


回答 2

没什么特别的tf.app。这只是一个通用的入口点脚本

使用可选的“ main”功能和“ argv”列表运行程序。

它与神经网络无关,它只是调用main函数,并传递给它的任何参数。

There is nothing special in tf.app. This is just a generic entry point script, which

Runs the program with an optional ‘main’ function and ‘argv’ list.

It has nothing to do with neural networks and it just calls the main function, passing through any arguments to it.


回答 3

简单来说,的工作tf.app.run()首先设置全局标志以供以后使用,例如:

from tensorflow.python.platform import flags
f = flags.FLAGS

然后使用一组参数运行自定义的main函数。

例如,在TensorFlow NMT代码库中,用于训练/推理的程序执行的第一个入口点就是从这一点开始(请参见下面的代码)

if __name__ == "__main__":
  nmt_parser = argparse.ArgumentParser()
  add_arguments(nmt_parser)
  FLAGS, unparsed = nmt_parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

在使用解析参数之后argparsetf.app.run()您将运行函数“ main”,其定义如下:

def main(unused_argv):
  default_hparams = create_hparams(FLAGS)
  train_fn = train.train
  inference_fn = inference.inference
  run_main(FLAGS, default_hparams, train_fn, inference_fn)

因此,在设置了供全局使用的标志之后,tf.app.run()只需运行main传递给它的函数argv作为其参数即可。

PS:正如萨尔瓦多·达利(Salvador Dali)的回答所说,我猜这只是一个很好的软件工程实践,尽管我不确定TensorFlow是否会执行main比使用常规CPython 进行的函数优化的运行。

In simple terms, the job of tf.app.run() is to first set the global flags for later usage like:

from tensorflow.python.platform import flags
f = flags.FLAGS

and then run your custom main function with a set of arguments.

For e.g. in TensorFlow NMT codebase, the very first entry point for the program execution for training/inference starts at this point (see below code)

if __name__ == "__main__":
  nmt_parser = argparse.ArgumentParser()
  add_arguments(nmt_parser)
  FLAGS, unparsed = nmt_parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

After parsing the arguments using argparse, with tf.app.run() you run the function “main” which is defined like:

def main(unused_argv):
  default_hparams = create_hparams(FLAGS)
  train_fn = train.train
  inference_fn = inference.inference
  run_main(FLAGS, default_hparams, train_fn, inference_fn)

So, after setting the flags for global use, tf.app.run() simply runs that main function that you pass to it with argv as its parameters.

P.S.: As Salvador Dali’s answer says, it’s just a good software engineering practice, I guess, although I’m not sure whether TensorFlow performs any optimized run of the main function than that was run using normal CPython.


回答 4

Google代码很大程度上取决于要在库/二进制文件/ python脚本中访问的全局标志,因此tf.app.run()解析出这些标志以在FLAGs(或类似的变量)中创建全局状态,然后调用python main( ) 正如它应该。

如果他们没有对tf.app.run()的调用,则用户可能会忘记进行FLAG解析,从而导致这些库/二进制文件/脚本无法访问所需的FLAG。

Google code depends on a lot on global flags being accessing in libraries/binaries/python scripts and so tf.app.run() parses out those flags to create a global state in FLAGs(or something similar) variable and then calls python main() as it should.

If they didn’t have this call to tf.app.run(), then users might forget to do FLAGs parsing, leading to these libraries/binaries/scripts not having access to FLAGs they need.


回答 5

2.0兼容答:如果你想使用tf.app.run()Tensorflow 2.0,我们应该使用的命令,

tf.compat.v1.app.run()或者您可以使用tf_upgrade_v21.x代码转换为2.0

2.0 Compatible Answer: If you want to use tf.app.run() in Tensorflow 2.0, we should use the command,

tf.compat.v1.app.run() or you can use tf_upgrade_v2 to convert 1.x code to 2.0.