深度学习离不开大量的参数配置,Tensorflow内置的参数方法 tf.flags 是很多TF使用者的选择
我在设置参数之后想要每次运行程序时打印出所有参数用来核对与记录,费了些功夫找到了方法
不多说,上代码
import tensorflow as tf
Flags=tf.flags
Flags.DEFINE_float('learning_rate', 0.0001, 'The learning rate for the network')
Flags.DEFINE_integer('decay_step', 500000, 'The steps needed to decay the learning rate')
Flags.DEFINE_float('decay_rate', 0.1, 'The decay rate of each decay step')
Flags.DEFINE_string('mode','train', 'The mode of the model train, test.')
FLAGS = Flags.FLAGS
def print_configuration_op(FLAGS):
print('My Configurations:')
#pdb.set_trace()
for name, value in FLAGS.__flags.items():
value=value.value
if type(value) == float:
print(' %s:\t %f'%(name, value))
elif type(value) == int:
print(' %s:\t %d'%(name, value))
elif type(value) == str:
print(' %s:\t %s'%(name, value))
elif type(value) == bool:
print(' %s:\t %s'%(name, value))
else:
print('%s:\t %s' % (name, value))
#for k, v in sorted(FLAGS.__dict__.items()):
#print(f'{k}={v}\n')
print('End of configuration')
def main(argv):
print_configuration_op(FLAGS)
if __name__ == '__main__':
tf.app.run()
输出
My Configurations:
learning_rate: 0.000100
decay_step: 500000
decay_rate: 0.100000
mode: train
h: False
help: False
helpfull: False
helpshort: False
End of configuration