我正在尝试测试一个非常简单的简短程序,如下所示
import numpy as np
import tensorflow as tf
flags = tf.app.flags
FLAGS = flags.FLAGS
import tensorvision.train as train
import tensorvision.utils as utils
flags.DEFINE_string('name', None,
'Append a name Tag to run.')
flags.DEFINE_string('hypes', 'hypes/medseg.json',
'File storing model parameters.')
if __name__ == '__main__':
tf.app.run()
Run Code Online (Sandbox Code Playgroud)
但是,运行该程序会出现以下错误消息,
Traceback (most recent call last):
File "train.py", line 43, in <module>
tf.app.run()
File "/devl/tensorflow/tf_0.12/lib/python3.4/site- packages/tensorflow/python/platform/app.py", line 39, in run
main = main or sys.modules['__main__'].main
AttributeError: 'module' object has no attribute 'main'
Run Code Online (Sandbox Code Playgroud) 我现在正在玩标志,并在使用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()
Run Code Online (Sandbox Code Playgroud)
在执行过程中,抛出此错误:
回溯(最近一次调用最后一次):
File "", line 1, in runfile('/path/flags.py', wdir='/path')
文件“/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py”,第710行,运行文件execfile(文件名,命名空间)
文件“/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py”,第101行,在execfile exec(compile(f.read(), filename) , 'exec'), 命名空间)
文件“/path/flags.py”,第 19 行,在 tf.app.run() 中
文件“/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/platform/app.py”,第126行,运行_sys.exit(main(argv))
类型错误:main() 采用 0 个位置参数,但给出了 1 个
...这很奇怪,因为我没有给 main() 一个参数。但是,如果我添加下划线def main(_):,它可以正常工作而不会出现任何错误。
我找不到描述使用下划线的文档。有人知道这里发生了什么吗?谢谢!