代码之家  ›  专栏  ›  技术社区  ›  DocDriven

运行tf.app.run()时引发异常

  •  1
  • DocDriven  · 技术社区  · 8 年前

    我正在玩弄旗帜,在使用时遇到了一些奇怪的行为。 tf.app.run() . 下面的代码段应该只打印通过命令行给出的字符串。

    import tensorflow as tf
    
    # command line flags
    tf.app.flags.DEFINE_string('mystring', 'Hello World!',
                               '''String to print to console.''')
    
    FLAGS = tf.app.flags.FLAGS
    
    
    def main():
    
        print(FLAGS.mystring)
    
    if __name__ == '__main__':
        tf.app.run()
    

    在执行过程中,引发此错误:

    回溯(最近一次呼叫的最后一次):

    文件“”,第1行,in runfile('/path/flags.py',wdir='/path')

    文件 “/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site packages/spyder/utils/site/sitecustomize.py”, 行710,在runfile中 ExecFile(文件名、命名空间)

    文件 “/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site packages/spyder/utils/site/sitecustomize.py”, 行101,在执行文件中 exec(compile(f.read(),文件名,“exec”),命名空间)

    文件“/path/flags.py”,第19行,in tf.app.run()。

    文件 “/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site packages/tensorflow/python/platform/app.py”, 第126行,运行中 _系统出口(主(argv)

    类型错误:main()接受0个位置参数,但给出了1个

    …这很奇怪,因为我没有给main()一个单独的参数。但是,如果我添加下划线 def main(_): ,没有任何错误。

    我找不到描述使用下划线的文档。有人知道这里发生了什么吗?谢谢您!

    3 回复  |  直到 7 年前
        1
  •  2
  •   Mohan Radhakrishnan    8 年前

    当我执行代码时,我在Pycharm IDE中看到的错误消息更清晰。

    Traceback (most recent call last):
      File "D:/PycharmProjects/TensorFlow/self.py", line 30, in <module>
        tf.app.run()
      File "D:\\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py", 
    line 48, in run
        _sys.exit(main(_sys.argv[:1] + flags_passthrough))
    TypeError: main() takes 0 positional arguments but 1 was given
    

    _sys.exit(main(_sys.argv[:1] + flags_passthrough)) 正在给我们打电话 主要的 一个参数的方法。

    这是 运行 方法 app.py

    精简版的 运行 方法可用于测试。

    import tensorflow as tf
    import sys as _sys
    from tensorflow.python.platform import flags
    
    
    # command line flags
    tf.app.flags.DEFINE_string('mystring', 'Hello World!',
                               '''String to print to console.''')
    
    FLAGS = tf.app.flags.FLAGS
    
    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
    
      print (_sys.argv[:1])
    
      # Call the main function, passing through any arguments
      # to the final program.
      #_sys.exit(main(_sys.argv[:1] + flags_passthrough))
    
      # Call the main function with no arguments
      #_sys.exit(main())
    
    
    def main():
        print(FLAGS.mystring)
    
    if __name__ == '__main__':
        #tf.app.run()
        run()
    

    print(_sys.argv[1:]) 印刷品 ['D:/PycharmProjects/TensorFlow/self.py'] 自从 argv[0]是传递给解释器的脚本名。

        2
  •  1
  •   guangqiang lu    7 年前

    也许你可以从这个链接找到答案来解释 app.py 跑 how app.py runs

    您还可以使用 def main(argv=None): ... 或者像你一样 def main(_): ... ,这样就可以让它为您提供主要的函数参数。

        3
  •  0
  •   finefoot    7 年前

    我在使用cprofile和调用脚本时遇到了类似的问题

    python -m cProfile train.py
    

    似乎问题在于tf.app.run调用了cprofile中的main,但它还没有准备好进行参数传递。在我的例子中,解决方案是指定 tf.app.run() :

    tf.app.run(main=main)
    

    别忘了加上这样的假论点 def main(_): .