TensorFlow的cifar10.py

时间:2016-03-20 23:28:37

标签: argparse tensorflow

运行python cifar10.py时出现以下错误:

argparse.ArgumentError: argument --batch_size: conflicting option string(s): --batch_size

这是运行的完整输出,包括完整的跟踪:

I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcublas.so.7.0 locally                          
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcudnn.so.6.5 locally                           
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcufft.so.7.0 locally                           
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcuda.so locally                                
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcurand.so.7.0 locally                          
Traceback (most recent call last):                                                                                                 
  File "cifar10.py", line 54, in <module>                                                                                          
    """Number of images to process in a batch.""")                                                                                 
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/default/_flags.py", line 86, in DEFINE_integer           
    _define_helper(flag_name, default_value, docstring, int)                                                                       
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/default/_flags.py", line 60, in _define_helper           
    type=flagtype)                                                                                                                 
  File "/usr/lib/python2.7/argparse.py", line 1297, in add_argument                                                                
    return self._add_action(action)                                                                                                
  File "/usr/lib/python2.7/argparse.py", line 1671, in _add_action                                                                 
    self._optionals._add_action(action)                                                                                            
  File "/usr/lib/python2.7/argparse.py", line 1498, in _add_action                                                                 
    action = super(_ArgumentGroup, self)._add_action(action)                                                                       
  File "/usr/lib/python2.7/argparse.py", line 1311, in _add_action                                                                 
    self._check_conflict(action)                                                                                                   
  File "/usr/lib/python2.7/argparse.py", line 1449, in _check_conflict                                                             
    conflict_handler(action, confl_optionals)                                                                                      
  File "/usr/lib/python2.7/argparse.py", line 1456, in _handle_conflict_error                                                      
    raise ArgumentError(action, message % conflict_string)                                                                         
argparse.ArgumentError: argument --batch_size: conflicting option string(s): --batch_size 

此错误似乎来自cifar10.py中的以下行:tf.app.flags.DEFINE_integer('batch_size', 128, """Number of images to process in a batch.""")

似乎argparse库认为我已经定义了选项字符串--batch_size,但我没有。

[Stack:Amazon g2.2xlarge spot instance,Python 2.7.6]

1 个答案:

答案 0 :(得分:2)

cifr10.py文件中:

import tensorflow as tf

from tensorflow.models.image.cifar10 import cifar10_input

FLAGS = tf.app.flags.FLAGS

# Basic model parameters.
tf.app.flags.DEFINE_integer('batch_size', 128,
                            """Number of images to process in a batch.""")
....

错误是由最后一个语句产生的,该语句在_flags.py文件中定义了具有该名称的argparse参数。显然,此时tf.app已经有了这样的参数定义。

因此,我们需要进一步回顾import tensorflow as tf以了解tf.app是如何创建的?

Amazon g2.2xlarge是什么?那也可以定义batch_size吗?

看起来tf.app来自

tensorflow/python/platform/app.py

反过来从

这样的东西中获取它
from tensorflow.python.platform.google._app import *

因此,如果您在某个本身接受google参数的amazonbatch_size平台上运行此操作,则可能会产生此错误。

关于cifr10batch_size参数的另一个问题:

How to use "FLAGS" (command line switches) in TensorFlow?

同样的错误:

Tensorflow ArgumentError Running CIFAR-10 example 答案是使用cifar10_train.pycifar10_eval.py,而不是cifar10.py