Skip to content

Instantly share code, notes, and snippets.

@Dexter1618
Created September 26, 2019 12:05
Show Gist options
  • Save Dexter1618/b9fe008326286abc40af518c70feccf1 to your computer and use it in GitHub Desktop.
Save Dexter1618/b9fe008326286abc40af518c70feccf1 to your computer and use it in GitHub Desktop.
Plot Tensorflow Graphs in Jupyter Notebook
'''
Source:https://nbviewer.jupyter.org/github/tensorflow/examples/blob/master/community/en/r1/deepdream.ipynb#Loading-and-displaying-the-model-graph
'''
from IPython.display import Image, display, HTML
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 = "<stripped %d bytes>"%size
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:950px;height:620px;border:0" srcdoc="{}"></iframe>
""".format(code.replace('"', '&quot;'))
display(HTML(iframe))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment