Skip to content

Welcome!

Amirsina Torfi edited this page Apr 16, 2017 · 3 revisions

Welcome to TensorFlow World

The tutorials in this section is just a start for going into TensorFlow world. The source code is available at this link.

We using Tensorboard for visualizing the outcomes. TensorBoard is the graph visualization tools provided by TensorFlow. Using Google’s words: “The computations you'll use TensorFlow for - like training a massive deep neural network - can be complex and confusing. To make it easier to understand, debug, and optimize TensorFlow programs, we've included a suite of visualization tools called TensorBoard.” A simple Tensorboard implementation is used in this tutorial.

NOTE:*

  • The details of summary operations, Tensorboard and their advantages are beyond the scope of this tutorial and will be presented in more advanced tutorials.

Prepairing the environment

At first we have to import the necessary libraries.

from __future__ import print_function
import tensorflow as tf
import os

Since we are aimed to use Tensorboard, we need a directory to store the information (the operations and their corresponding outputs if desired by the user). These information are exported to event files by TensorFlow. The even files can be transformed to visual data such that the user be able to evaluate the architecture and the operations. The path to store these even files is defined as below:

# The default path for saving event files is the same folder of this python file.
tf.app.flags.DEFINE_string(
'log_dir', os.path.dirname(os.path.abspath(__file__)) + '/logs',
'Directory where event logs are written to.')

# Store all elemnts in FLAG structure!
FLAGS = tf.app.flags.FLAGS

The os.path.dirname(os.path.abspath(__file__)) gets the directory name of the current python file. The tf.app.flags.FLAGS points to all defined flags using the FLAGS indicator. From now on the flags can be called using FLAGS.flag_name.

For convenience it is useful to only work with absolute paths. By using the following script, the user is prompt to use absolute paths for the log_dir directory.

# The user is prompted to input an absolute path.
# os.path.expanduser is leveraged to transform '~' sign to the corresponding path indicator.
#       Example: '~/logs' equals to '/home/username/logs'
if not os.path.isabs(os.path.expanduser(FLAGS.log_dir)):
    raise ValueError('You must assign absolute path for --log_dir')

Inauguration

Some sentence can be defined by TensorFlow:

# Defining some sentence!
welcome = tf.constant('Welcome to TensorFlow world!')

The tf. operator dperforms the specific operation and the output will be a Tensor.

Run the Experiment

The seesion, which is the environment for running the operations, is executed as below:

# Run the session
with tf.Session() as sess:
    writer = tf.summary.FileWriter(os.path.expanduser(FLAGS.log_dir), sess.graph)
    print("output: ", sess.run(welcome))

# Closing the writer.
writer.close()
sess.close()

The tf.summary.FileWriter is defined to write the summaries into event files.The command of sess.run() must be used for evaluation of any Tensor otherwise the operation won't be executed. In the end by using the writer.close(), the summary writer will be closed.

Clone this wiki locally