1. 程式人生 > >執行fully_connected_feed.py報錯問題conflicting option string以及sys.exit(main(sys.argv[:1] + flags_passthrou

執行fully_connected_feed.py報錯問題conflicting option string以及sys.exit(main(sys.argv[:1] + flags_passthrou

在玩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次列印一次,正好列印了兩次。所以是正常輸出啦~/尷尬臉