Display the graph of tensorBoard on jupyter

python / tensorflow beginners build jupyter + tensorflow environment and do Hello World --Qiita

Continuing from, this time it is the procedure to display the graph of tensorBoard on jupyter. tensorBoard is very convenient, so let's display it on jupyter as it is! It is a procedure called.

Goal screen

2016-09-20_7_53_11.png

Creation procedure

Various preparations are required to display the tensorboard graph. Since it is expected to be used many times, we will remove the troublesome part with the name tensorboard.py so that it can be used quickly when calling.

Create tensorboard.py

First we will make the part to display the tensor board

Create an empty file

To create an empty file, select TextFile from new as below (If you do it from the command line, you can simply create tensorboard.py).

2016-09-20_16_07_26.png

It can be created anywhere, but it is assumed that it was created in the same folder as notebook.

Rename and copy

Since it is untitled.txt as it is created, press the name part to make it tensorboard.py.

2016-09-20_16_09_40.png

Please paste the following for the source code. You can change the size, etc.

from IPython.display import clear_output, Image, display, HTML
import tensorflow as tf
import numpy as np

def strip_consts(graph_def, max_const_size=32):
    """Strip large constant values from graph_def."""
    strip_def = tf.GraphDef()
    for n0 in graph_def.node:
        n = strip_def.node.add() 
        n.MergeFrom(n0)
        if n.op == 'Const':
            tensor = n.attr['value'].tensor
            size = len(tensor.tensor_content)
            if size > max_const_size:
                tensor.tensor_content = bytes("<stripped %d bytes>"%size, 'utf-8')
    return strip_def

def show_graph(graph_def, max_const_size=32):
    """Visualize TensorFlow graph."""
    if hasattr(graph_def, 'as_graph_def'):
        graph_def = graph_def.as_graph_def()
    strip_def = strip_consts(graph_def, max_const_size=max_const_size)
    code = """
        <script>
          function load() {{
            document.getElementById("{id}").pbtxt = {data};
          }}
        </script>
        <link rel="import" href="https://tensorboard.appspot.com/tf-graph-basic.build.html" onload=load()>
        <div style="height:600px">
          <tf-graph-basic id="{id}"></tf-graph-basic>
        </div>
    """.format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))

    iframe = """
        <iframe seamless style="width:1200px;height:620px;border:0" srcdoc="{}"></iframe>
    """.format(code.replace('"', '&quot;'))
    display(HTML(iframe))

Reference: Simple way to visualize a TensorFlow graph in Jupyter? --Stack Overflow

Verification

OK if the file is created as below

2016-09-20_7_53_41.png

Modify the caller

Add ʻimport tensorboard as tb to read the created tensorboard.py and tb.show_graph`, which is the code to be displayed, to the program you want to display.

import tensorflow as tf
import tensorboard as tb   # ->add to

const1 = tf.constant(2)
const2 = tf.constant(3)
add_op = tf.add(const1, const2)
mul_op = tf.mul(add_op, const2)

with tf.Session() as sess:
    result, result2 = sess.run([mul_op, add_op])
    print(result)
    print(result2)

    tf.summary.FileWriter('./log/', sess.graph)
    
tb.show_graph(tf.get_default_graph().as_graph_def()) # ->add to

After that, it will be displayed if you execute it with ctrl + Enter

Remarks

If you modify the source code to copy and paste well, it will be easier to use. And I really want to display events rather than graph, but I couldn't find a way (´ω`)

Recommended Posts

Display the graph of tensorBoard on jupyter
Install matplotlib and display graph on Jupyter Notebook
Simply display a line graph on Jupyter Notebook
Display PIL images on Jupyter
Drawing on Jupyter using the plot function of pandas
Change the theme of Jupyter
Connected components of the graph
Read the csv file with jupyter notebook and write the graph on top of it
Unable to display tensorboard in jupyter notebook on docker (solved)
Display the absolute path on the Finder
Read the coordinates of the plot on the graph with Python-matplotlib (super beginner)
Display the image of the camera connected to the personal computer on the GUI.
Real-time display of video acquired from webcam on Jupyter notebook (Python3)
How to use Jupyter on the front end of supercomputer ITO
[pyqtgraph] Set the size ratio of the graph
Post the subject of Gmail on twitter
Build the execution environment of Jupyter Lab
Clone the github repository on jupyter notebook
GPU check of PC on jupyter notebook
The usage of TensorBoard has changed slightly
Display histogram / scatter plot on Jupyter Notebook
Change the order of PostgreSQL on Heroku
When generating a large number of graphs with matplotlib, I do not want to display the graph on the screen (jupyter environment)
Understand how to display images on Jupyter (utilization of imshow / matplotlib of OpenCV)
[Android] Display images on the web in the info Window of Google Map
You can see the transition of points in the J League on the graph!
I tried to display the altitude value of DTM in a graph
Display the edge
Execution environment on the Web by "Project Jupyter"
Let's automatically display the lyrics of the song being played on iTunes in Python
The behavior of signal () depends on the compile options
Increase the font size of the graph with matplotlib
[2020July] Check the UDID of the iPad on Linux
Use the latest version of PyCharm on Ubuntu
I checked the list of shortcut keys of Jupyter
Golang on jupyter
Fill the browser with the width of Jupyter Notebook
Jupyter on AWS
Calculate the probability of outliers on a boxplot
The basis of graph theory with matplotlib animation
At the time of python update on ubuntu
Change the resolution of Ubuntu running on VirtualBox
Remotely open Jupyter notebook launched on the server
[AWS S3] Confirmation of the existence of folders on S3
I tried to display the infection condition of coronavirus on the heat map of seaborn
The story of launching python2.x jupyter notebook using docker (crushed on Saturday and Sunday)
Put Scipy + Matplotlib in Ubuntu on Vagrant and display the graph with X11 Forwarding
I want to plot the location information of GTFS Realtime on Jupyter! (With balloon)
[Don't refer to 04.02.17] Display the temperature sensor on a real-time graph with rasberry pi 3.
I tried to make it easy to change the setting of authenticated Proxy on Jupyter
Gradually display the output of the command executed by subprocess.Popen
Install the latest version of CMake on Ubuntu 18.04.4 LTS
The kernel of jupyter notebook can no longer connect
Monitor the training model with TensorBord on Jupyter Notebook
Maybe I overestimated the impact of ShellShock on CGI
Try to estimate the number of likes on Twitter
Tweet the triple forecast of the boat race on Twitter
Difference in results depending on the argument of multiprocess.Process
Execute the command on the web server and display the result
Scroll Japanese on the LED of RaspberryPi Sense HAT
A Study on Visualization of the Scope of Prediction Models