tensorflow: UnrecognizedFlagError: Unknown command line flag 'f'

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): custom code
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10
  • TensorFlow installed from (source or binary): source
  • TensorFlow version (use command below): tensorflow 1.5.0
  • Python version: pyhton 3.6.3
  • Bazel version (if compiling from source): None
  • GCC/Compiler version (if compiling from source): None
  • CUDA/cuDNN version: None
  • GPU model and memory: GTX960M
  • Exact command to reproduce:

Problem Description

When trying to run this code

import tensorflow as tf

flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
flags.DEFINE_integer('max_steps', 2000, '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', 'data', 'Directory to put the training data.')
flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data '
'for unit testing.')

FLAGS = flags.FLAGS
print(FLAGS.learning_rate)

I got this error message

---------------------------------------------------------------------------
UnrecognizedFlagError                     Traceback (most recent call last)
<ipython-input-3-1ce89dff3e81> in <module>()
      1 FLAGS = flags.FLAGS
----> 2 print(FLAGS.learning_rate)

c:\users\jinsu\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\platform\flags.py in __getattr__(self, name)
     82     # a flag.
     83     if not wrapped.is_parsed():
---> 84       wrapped(_sys.argv)
     85     return wrapped.__getattr__(name)
     86 

c:\users\jinsu\appdata\local\programs\python\python36\lib\site-packages\absl\flags\_flagvalues.py in __call__(self, argv, known_only)
    628       suggestions = _helpers.get_flag_suggestions(name, list(self))
    629       raise _exceptions.UnrecognizedFlagError(
--> 630           name, value, suggestions=suggestions)
    631 
    632     self.mark_as_parsed()

UnrecognizedFlagError: Unknown command line flag 'f'

This is another

import tensorflow as tf

FLAGS = tf.app.flags.FLAGS
FLAGS.learning_rate = 0.02
FLAGS.name = 'test'

print(FLAGS.learning_rate)
print(FLAGS.name)
---------------------------------------------------------------------------
UnrecognizedFlagError                     Traceback (most recent call last)
<ipython-input-1-fe17797ba132> in <module>()
      2 
      3 FLAGS = tf.app.flags.FLAGS
----> 4 FLAGS.learning_rate = 0.02
      5 FLAGS.name = 'test'
      6 

c:\users\jinsu\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\platform\flags.py in __setattr__(self, name, value)
     86 
     87   def __setattr__(self, name, value):
---> 88     return self.__dict__['__wrapped'].__setattr__(name, value)
     89 
     90   def __delattr__(self, name):

c:\users\jinsu\appdata\local\programs\python\python36\lib\site-packages\absl\flags\_flagvalues.py in __setattr__(self, name, value)
    494       raise AttributeError(name)
    495     if name not in fl:
--> 496       return self._set_unknown_flag(name, value)
    497     fl[name].value = value
    498     self._assert_validators(fl[name].validators)

c:\users\jinsu\appdata\local\programs\python\python36\lib\site-packages\absl\flags\_flagvalues.py in _set_unknown_flag(self, name, value)
    372       except NameError:  # Flag name is not valid.
    373         pass
--> 374     raise _exceptions.UnrecognizedFlagError(name, value)
    375 
    376   def append_flag_values(self, flag_values):

UnrecognizedFlagError: Unknown command line flag 'learning_rate'

Did I miss anything?? I cannot figure out what the problem is.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 38 (7 by maintainers)

Most upvoted comments

I solved this issue by adding the line: tf.app.flags.DEFINE_string('f', '', 'kernel') what this essentially does is that it creates a flag f. This is effective as jupyter notebook supplies a flag “-f” to pass a name for a JSON file, likely for the kernel.

I face this problem when running code in jupyter notebook. And I don’t set any flag named f. When using command line to run the same .py file, there’s no more Unknown command line flag 'f'. May this info could help.

As @spotiris mentioned you could just remove all the cli args by

import sys
sys.argv = sys.argv[:1]

Same here in Jupyter Notebook (Working well in terminal)

TF version is latest (1.8.0) Anaconda2(Python2.7) Jupyter notebook

print(FLAGS.learning_rate)
throws

UnrecognizedFlagErrorTraceback (most recent call last) <ipython-input-15-aa999f68f930> in <module>() ----> 1 print(FLAGS.learning_rate)

/home/budweiser/anaconda2/lib/python2.7/site-packages/tensorflow/python/platform/flags.pyc in getattr(self, name) 82 # a flag. 83 if not wrapped.is_parsed(): —> 84 wrapped(_sys.argv) 85 return wrapped.getattr(name) 86

/home/budweiser/anaconda2/lib/python2.7/site-packages/absl/flags/_flagvalues.pyc in call(self, argv, known_only) 628 suggestions = _helpers.get_flag_suggestions(name, list(self)) 629 raise _exceptions.UnrecognizedFlagError( –> 630 name, value, suggestions=suggestions) 631 632 self.mark_as_parsed()

UnrecognizedFlagError: Unknown command line flag ‘f’ -----------------------------------------------------------------------------------------------------------EO.Error

This is working … print(FLAGS[‘learning_rate’].value) 0.001

For those who are having issues using the flags module in jupyter notebooks, it is because sys.argv is populated from jupyter, not your notebook code. Put the following in your first cell:

import sys
sys.argv = " --your flags --placed here".split(" ")
# Note the space at the beginning

I had the same issue and I figured out now it works with version 1.7.0. It might be useful for the others: import tensorflow as tf Then define your flags with using tf.app.flags. When you finished defining flags do your assignment as FLAGS = tf.app.flags.FLAGS.

And if you need to run tf.app.run run on main function.

I can confirm this bug is very much still there. What makes it worse is a lot of the tf-agents example scripts out there have the bug baked in… It really needs fixing…

I want to train my model but I am receiving this error kindly help me + [ ! -f DeepSpeech.py ] + python -u DeepSpeech.py --train_files /home/sehar/urdu/train/urdu-train.csv --dev_files /home/sehar/urdu/urdu-train.csv --test_files /home/sehar/urdu/train/urdu-train.csv --train_batch_size 80 --dev_batch_size 80 --test_batch_size 40 --n_hidden 375 --epochs 33 --validation_step 1 --early_stop True --earlystop_nsteps 6 --estop_mean_thresh 0.1 --dropout_rate 0.22 --learning_rate 0.00095 --report_count 100 --use_seq_length False --export_dir /home/sehar/urdu-models --checkpoint_dir /home/nvidia/DeepSpeech/checkout/ --alphabet_config_path /home/sehar/urdu-models/native_client/alphabet.txt/ /home/sehar/DeepSpeech/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’. _np_qint8 = np.dtype([(“qint8”, np.int8, 1)]) /home/sehar/DeepSpeech/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’. _np_quint8 = np.dtype([(“quint8”, np.uint8, 1)]) /home/sehar/DeepSpeech/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’. _np_qint16 = np.dtype([(“qint16”, np.int16, 1)]) /home/sehar/DeepSpeech/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’. _np_quint16 = np.dtype([(“quint16”, np.uint16, 1)]) /home/sehar/DeepSpeech/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’. _np_qint32 = np.dtype([(“qint32”, np.int32, 1)]) /home/sehar/DeepSpeech/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’. np_resource = np.dtype([(“resource”, np.ubyte, 1)]) /home/sehar/venv/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’. _np_qint8 = np.dtype([(“qint8”, np.int8, 1)]) /home/sehar/venv/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’. _np_quint8 = np.dtype([(“quint8”, np.uint8, 1)]) /home/sehar/venv/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’. _np_qint16 = np.dtype([(“qint16”, np.int16, 1)]) /home/sehar/venv/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’. _np_quint16 = np.dtype([(“quint16”, np.uint16, 1)]) /home/sehar/venv/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’. _np_qint32 = np.dtype([(“qint32”, np.int32, 1)]) /home/sehar/venv/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,)type’. np_resource = np.dtype([(“resource”, np.ubyte, 1)]) FATAL Flags parsing error: Unknown command line flag ‘validation_step’ Pass --helpshort or --helpfull to see help on flags.

Thanks @taixhi . It is only valid for jupyter env. guys!

I was getting the error first at 1.5.0 then I upgraded to the latest version 1.7.0. When I call cifar10.maybe_download_and_extract() method it gives the error. I am using anaconda.

`UnrecognizedFlagError Traceback (most recent call last) <ipython-input-13-02a754d7036a> in <module>() ----> 1 cifar10.maybe_download_and_extract()

D:\Google Drive\Akademik\Ozyegin\CS566\Project 2\cifar10.py in maybe_download_and_extract() 381 def maybe_download_and_extract(): 382 “”“Download and extract the tarball from Alex’s website.”“” –> 383 dest_directory = FLAGS.data_dir 384 if not os.path.exists(dest_directory): 385 os.makedirs(dest_directory)

~\anaconda3\lib\site-packages\tensorflow\python\platform\flags.py in getattr(self, name) 82 # a flag. 83 if not wrapped.is_parsed(): —> 84 wrapped(_sys.argv) 85 return wrapped.getattr(name) 86

~\anaconda3\lib\site-packages\absl\flags_flagvalues.py in call(self, argv, known_only) 628 suggestions = _helpers.get_flag_suggestions(name, list(self)) 629 raise _exceptions.UnrecognizedFlagError( –> 630 name, value, suggestions=suggestions) 631 632 self.mark_as_parsed()

UnrecognizedFlagError: Unknown command line flag ‘f’`