我的小程序:
在玩tensorflow时,按照官方教程,用到fully_connected_feed.py这个文件。运行时发生错误:
ArgumentError: argument --positive_data_file: conflicting option string: --positive_data_file
可以定位到是因为以下代码造成的:
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
flags.DEFINE_integer('max_steps', 3000, 'Number of steps to run trainer.')
flags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.')
flags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.')
flags.DEFINE_integer('batch_size', 100, 'Batch size. '
'Must divide evenly into the dataset sizes.')
flags.DEFINE_string('train_dir', '../material/mnist/', 'Directory to put the training data.')
flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data '
'for unit testing.')
大概意思可以看出是什么重复了的意思,把所有这些定义改成如下的try/except模块就没问题了:
try:
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
except:
print('except')
然后继续运行,控制台输出了如下的内容:
有点奇怪,突然冒出个SystemExit,于是就输入%tb看了下:
其实这是正常输出,并没有异常,因为tf.app.run()函数如下:
"""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, 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))
代码最后就是调用的sys.exit(main(sys.argv[:1] + flags_passthrough)),于是就退出了。
其实反过来再看打印的正确率的信息,因为代码里设置的最大迭代次数是2000次,而打印信息是每1000次打印一次,正好打印了两次。所以是正常输出啦~/尴尬脸