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.
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.
First we will make the part to display the tensor board
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
).
It can be created anywhere, but it is assumed that it was created in the same folder as notebook.
Since it is untitled.txt as it is created, press the name part to make it tensorboard.py
.
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('"', '"'))
display(HTML(iframe))
Reference: Simple way to visualize a TensorFlow graph in Jupyter? --Stack Overflow
OK if the file is created as below
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
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