Python tf.app.run() 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33703624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does tf.app.run() work?
提问by Anurag Ranjan
How does tf.app.run()
work in Tensorflow translate demo?
tf.app.run()
Tensorflow如何在翻译演示中工作?
In tensorflow/models/rnn/translate/translate.py
, there is a call to tf.app.run()
. How is it being handled?
在 中tensorflow/models/rnn/translate/translate.py
,有一个对 的调用tf.app.run()
。它是如何处理的?
if __name__ == "__main__":
tf.app.run()
回答by dga
回答by lei du
if __name__ == "__main__":
means current file is executed under a shell instead of imported as a module.
表示当前文件在 shell 下执行而不是作为模块导入。
tf.app.run()
As you can see through the file app.py
正如你可以通过文件看到的 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.
这确保了您通过命令行传递的参数是有效的,例如,
python my_model.py --data_dir='...' --max_iteration=10000
实际上,此功能是基于python 标准argparse
模块实现的。
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
).
第一个main
在右边=
是当前函数的第一个参数run(main=None, argv=None)
。Whilesys.modules['__main__']
表示当前正在运行的文件(例如my_model.py
)。
So there are two cases:
所以有两种情况:
You don't have a
main
function inmy_model.py
Then you have to calltf.app.run(my_main_running_function)
you have a
main
function inmy_model.py
. (This is mostly the case.)
你没有
main
函数my_model.py
然后你必须调用tf.app.run(my_main_running_function)
你
main
在my_model.py
. (大多数情况下是这种情况。)
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.
确保您的main(argv)
ormy_main_running_function(argv)
函数被正确解析参数调用。
回答by Salvador Dali
There is nothing special in tf.app
. This is just a generic entry point script, which
中没有什么特别之处tf.app
。这只是一个通用的入口点脚本,它
Runs the program with an optional 'main' function and 'argv' list.
使用可选的“main”函数和“argv”列表运行程序。
It has nothing to do with neural networks and it just calls the main function, passing through any arguments to it.
它与神经网络无关,它只是调用主函数,将任何参数传递给它。
回答by kmario23
In simple terms, the job of tf.app.run()
is to firstset the global flags for later usage like:
简单来说,工作tf.app.run()
是首先设置全局标志以供以后使用,例如:
from tensorflow.python.platform import flags
f = flags.FLAGS
and then run your custom mainfunction with a set of arguments.
然后使用一组参数运行您的自定义 main函数。
For e.g. in TensorFlow NMTcodebase, the very first entry point for the program execution for training/inference starts at this point (see below code)
例如,在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)
After parsing the arguments using argparse
, with tf.app.run()
you run the function "main" which is defined like:
使用解析参数后argparse
,tf.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)
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.
因此,在设置全局使用的标志后,tf.app.run()
只需运行main
您argv
作为参数传递给它的函数。
P.S.: As Salvador Dali's answersays, 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.
PS:正如萨尔瓦多·达利 (Salvador Dali) 的回答所说,我猜这只是一种很好的软件工程实践,尽管我不确定 TensorFlow 是否会执行任何优化的main
函数运行,而不是使用普通 CPython 运行的运行。
回答by Mudit Jain
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.
Google 代码在很大程度上依赖于在库/二进制文件/python 脚本中访问的全局标志,因此 tf.app.run() 解析这些标志以在 FLAG(或类似的)变量中创建全局状态,然后调用 python main( ) 正如它应该。
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.
如果他们没有调用 tf.app.run(),那么用户可能会忘记进行 FLAG 解析,导致这些库/二进制文件/脚本无法访问他们需要的 FLAG。
回答by Tensorflow Support
2.0 Compatible Answer: If you want to use tf.app.run()
in Tensorflow 2.0
, we should use the command,
2.0 兼容答案:如果要使用tf.app.run()
in Tensorflow 2.0
,我们应该使用命令,
tf.compat.v1.app.run()
or you can use tf_upgrade_v2
to convert 1.x
code to 2.0
.
tf.compat.v1.app.run()
或者您可以使用tf_upgrade_v2
将1.x
代码转换为2.0
.