【TF】tensorflow 中 tf.app.run() 什么意思?

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HelloZEX/article/details/83543476

tensorflow的程序中,在main函数下,都是使用tf.app.run()来启动

查看源码可知,该函数是用来处理flag解析,然后执行main函数,那么flag解析是什么意思呢?诸如这样的:

# tensorflow/tensorflow/python/platform/default/_app.py
 
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
 
"""Generic entry point script."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import sys
 
from tensorflow.python.platform import flags
 
 
def run(main=None):
  f = flags.FLAGS
  f._parse_flags()
  main = main or sys.modules['__main__'].main
  sys.exit(main(sys.argv))

处理flag解析,然后执行main函数,那么flag解析是什么意思呢?诸如这样的:

tf.app.flags.DEFINE_boolean("self_test", False, "True if running a self test.")
tf.app.flags.DEFINE_boolean('use_fp16', False,
                            "Use half floats instead of full floats if True.")
FLAGS = tf.app.flags.FLAGS

那么 tf.app.run()什么意思呢 ? 
应该是函数入口,类似于c/c++中的main()。 

--------------------- 

大概意思是通过处理flag解析,然后执行main函数。

说白了,有两种情况:

如果你的代码中的入口函数不叫main(),而是一个其他名字的函数,如test(),则你应该这样写入口tf.app.run(test())
如果你的代码中的入口函数叫main(),则你就可以把入口写成tf.app.run()

--------------------- 

参考源:

How does tf.app.run() work?

tf.app.run()

猜你喜欢

转载自blog.csdn.net/HelloZEX/article/details/83543476