Tutorial Example

TensorFlow Print All Arguments Names and Values in Application: A Beginner Guide – TensorFLow Tutorial

We can set some arguments for our tenorflow application, to understand how to set and parse these arguments, you can read this tutorial.

Set and Parse Command Line Arguments with Flags in TensorFlow – TensorFlow Tutorial

However, if you do not know how many arguments have been set in python script, the best way is to print all arguemnts in application. In this tutorial, we will discuss how to print these arguments for tensorflow beginners.

To print tensorflow arguments, we can use:

tf.app.flags.FLAGS.argument_name

For example, if you have set a learning_rate argument for tensorflow like this:

#coding=utf-8
import tensorflow as tf
 
#set arguments
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_float('learning_rate',0.01,'Initial learning rate')

To print it, we can do like this:

print(tf.app.flags.FLAGS.learning_rate)

However, if you have set more arguments and want to print all names and values of these arguments, how to do?

Print all tensorflow arguments names and values

First, we create more tensorflow arguments.

flags.DEFINE_integer('max_steps',2000,'Number of steps to run trainer')
flags.DEFINE_integer('hidden1_unit',128,'Number of units in hidden layer 1')
flags.DEFINE_integer('batch_size',100,'Batch size')
flags.DEFINE_string('train_dir','mnist-data/','Directory to put the training data')

Then print all of them.

for attr, v in FLAGS.__flags.items():
    print("{}={}".format(attr, str(v.value)))

Run this code, you will get result like below.

From the result, you can find the name and value of each argument.

Meanwhile, if you want to print all arguments into a log file, you can read this tutorial.

Save Python Message into a Log File with logging – Deep Learning Tutorial

The result may like: