[PYTHON] [Introduction to TensorBoard] Visualize TensorFlow processing to deepen understanding

Accelerate your understanding of TensorFlow with TensorBoard

If you're a liberal arts graduate and you're new to Python, let alone Deep Learning, it's difficult to understand TensorFlow. I also learned TensorBoard ** to speed up my understanding. It can be used for various purposes such as debugging, processing optimization, and redesign, as well as promoting understanding of complex deep learning. There are various visualizations, but ** this article focuses on the Graph output method for beginners ** (I wish I could make it wider and deeper, but my understanding has not caught up to that point. Hmm: sweat :). The Official Guide "TensorBoard: Visualizing Learning" was confusing, so I've simplified it a lot. The site of Visualize processing with TensorBoard was wonderful and I used it as a reference. Environment: Confirmed with both python3.5 tensorflow1.1 and tensorflow1.21

Reference link

-Installing TensorFlow on Windows was easy even for Python beginners -[Explanation for beginners] TensorFlow basic syntax and concept -[Explanation for beginners] TensorFlow tutorial MNIST (for beginners) -[Visualize TensorFlow Tutorial MNIST (for beginners) with TensorBoard] (http://qiita.com/FukuharaYohei/items/2834cc054a8b8884e150) -[Introduction to TensorBoard: image] TensorFlow Visualize image processing to deepen understanding -[Introduction to TensorBoard: Projector] Make TensorFlow processing look cool -[Explanation for beginners] TensorFlow Tutorial Deep MNIST -TensorFlow API memo -Yuki Kashiwagi's facial features to understand TensorFlow [Part 1]

Simple logic of TensorBoard (Graph)

Visualize 1 + 2 expressions in Graph

Calculate the constant 1 + 2 like this and visualize it. To be honest, it's very easy.

import tensorflow as tf
sess = tf.InteractiveSession()

#TensorBoard information output directory
log_dir = '/tmp/tensorflow/mnist/logs/simple01'

#Delete the specified directory if it exists and recreate it
if tf.gfile.Exists(log_dir):
    tf.gfile.DeleteRecursively(log_dir)
tf.gfile.MakeDirs(log_dir)

#1 with a constant+ 2
x = tf.constant(1, name='x')
y = tf.constant(2, name='y')
z = x + y

#Output z on the graph with this command
_ = tf.summary.scalar('z', z)

#Draw a graph with SummaryWriter(Commands after this will not be output to the graph)
summary_writer = tf.summary.FileWriter(log_dir , sess.graph)

#Run
print(sess.run(z))

#SummaryWriter closed
summary_writer.close()

Then launch Tensorboard. Since my environment is built with Anaconda, I first start Terminal from Anaconda Navigator. 30.TensorFlow_Install01.JPG

Then, start Tensorbaord from Terminal by specifying the directory (the directory is stored in the variable log_dir in the Python program).

tensorboard --logdir=/tmp/tensorflow/mnist/logs/simple01

After booting, open http: // localhost: 6006 / in your browser and you will see the TensorBoard screen.

TensorBoardBasic00.JPG

I get angry with "No scalar data was found", but this time there is no problem because scalar is not output and only Graph output is used. By selecting "Graphs" from the menu on the screen, the Graph shown below will be output. I was able to see the formula of 1 + 2 here.

TensorBoardBasic01.JPG

Grouping using tf.name_scope

Grouping can be done by adding tf.name_scope to the previous source code.

import tensorflow as tf
sess = tf.InteractiveSession()

#TensorBoard information output directory
log_dir = '/tmp/tensorflow/mnist/logs/simple02'

#Delete the specified directory if it exists and recreate it
if tf.gfile.Exists(log_dir):
    tf.gfile.DeleteRecursively(log_dir)
tf.gfile.MakeDirs(log_dir)

# add_Grouping by the name of scope
with tf.name_scope('add_scope'):
    
    #1 with a constant+ 2
    x = tf.constant(1, name='x')
    y = tf.constant(2, name='y')
    z = x + y
    
    #Output z on the graph with this command
    _ = tf.summary.scalar('z', z)


#Draw a graph with SummaryWriter(Commands after this will not be output to the graph)
summary_writer = tf.summary.FileWriter(log_dir , sess.graph)

#Run
print(sess.run(z))

#SummaryWriter closed
summary_writer.close()

result Grouping (frame) is created with the name add_scope. When the calculation is complicated, it is too difficult to see if it cannot be grouped.

TensorBoardBasic02.JPG

Expression hierarchy

Add tf.name_scope to the previous source code in a nested manner to create a hierarchy of multiple expressions.

import tensorflow as tf
sess = tf.InteractiveSession()

#TensorBoard information output directory
log_dir = '/tmp/tensorflow/mnist/logs/simple02'

#Delete the specified directory if it exists and recreate it
if tf.gfile.Exists(log_dir):
    tf.gfile.DeleteRecursively(log_dir)
tf.gfile.MakeDirs(log_dir)

#add_Grouping by the name of scope
with tf.name_scope('add_scope'):

    #1 with a constant+ 2
    x = tf.constant(1, name='x')
    y = tf.constant(2, name='y')
    z = x + y
    
    #Output z on the graph with this command
    _ = tf.summary.scalar('z', z)
    
    #Multiply the above result
    with tf.name_scope('multiply_scope'):
        zz = y * z

#Draw a graph with SummaryWriter(Commands after this will not be output to the graph)
summary_writer = tf.summary.FileWriter(log_dir , sess.graph)

#Run
print(sess.run(z))

#SummaryWriter closed
summary_writer.close()

result It was layered and neat.

TensorBoardBasic03.JPG

reference By the way, if you put them in the same line instead of layering, they will be output in this form.

TensorBoardBasic04.JPG

Recommended Posts

[Introduction to TensorBoard] Visualize TensorFlow processing to deepen understanding
[Introduction to TensorBoard: image] TensorFlow Visualize image processing to deepen understanding
opencv-python Introduction to image processing
An introduction to private TensorFlow
[Explanation for beginners] Introduction to convolution processing (explained in TensorFlow)
[Explanation for beginners] Introduction to pooling processing (explained in TensorFlow)
Introduction to TensorFlow --Hello World Edition
Introduction to regular expression processing system
I tried to visualize AutoEncoder with TensorFlow
Probably the most straightforward introduction to TensorFlow
Beginners read "Introduction to TensorFlow 2.0 for Experts"
Hands-on to visualize your tweets while understanding BERT
Introduction to MQTT (Introduction)
Introduction to Scrapy (1)
Introduction to Scrapy (3)
Introduction to Supervisor
Introduction to Tkinter 1: Introduction
Introduction to PyQt
Introduction to Scrapy (2)
[Linux] Introduction to Linux
Introduction to Scrapy (4)
Introduction to discord.py (2)
Introduction to discord.py
Understanding TensorFlow Arithmetic
[Introduction to Python] How to use while statements (repetitive processing)
An introduction to Python distributed parallel processing with Ray
[Chapter 6] Introduction to scikit-learn with 100 knocks of language processing
[Chapter 3] Introduction to Python with 100 knocks of language processing
From running MINST on TensorFlow 2.0 to visualization on TensorBoard (2019 edition)
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Introduction to RasPi4] Environment construction; OpenCV / Tensorflow, Japanese input ♪
[Chapter 4] Introduction to Python with 100 knocks of language processing