To learn the example of distributed Tensorflow, I wrote this snippet:
import argparse
FLAGS = None
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.register("type", "bool", lambda v: v.lower() == "true")
parser.add_argument(
"--training",
type = bool,
default = True,
)
FLAGS, unparsed = parser.parse_known_args()
print(FLAGS)
The “parser.register()” is the Tensorflow way of register ‘bool’ type for parser. But it can’t work! In my shell, I run
python test.py --training false
python test.py --training False
They all print out “Namespace(training=True)”, which means the code above can’t change value of argument ‘training’ (My Python’s version is 2.7.5).
The correct codes should be:
def str2bool(value):
return value.lower() == 'true'
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--training",
type = str2bool,
default = True,
)
use type=’bool’
It works. Thank you for your help!