上代码:(假设为testflags.py)
import tensorflow as tf
tf.app.flags.DEFINE_boolean("attention", True, "Whether to use the attention mechanism.")
FLAGS = tf.app.flags.FLAGS
def main(_):
print(FLAGS.attention)
if __name__ == '__main__':
tf.app.run()
当我使用带有参数的命令时:
python src/testflags.py --attention False
返回了如下结果:
True.
是不是很奇怪,明明传入了False,为什么没有变化?
这是因为输入的语法有问题,可以换成如下命令:
python src/testflags.py --noattention
或者
python src/testflags.py --attention=False
第一种方法是在参数前面加上no,第二种方法是中间写等号“=”。