Skip to content

Instantly share code, notes, and snippets.



Last active Jan 16, 2019
What would you like to do?
import tensorflow as tf
import numpy as np
from IPython.display import clear_output, 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()
if n.op == 'Const':
tensor = n.attr['value'].tensor
size = len(tensor.tensor_content)
if size > max_const_size:
tensor.tensor_content = tf.compat.as_bytes("<stripped %d bytes>"%size)
return strip_def
def rename_nodes(graph_def, rename_func):
res_def = tf.GraphDef()
for n0 in graph_def.node:
n = res_def.node.add()
n.MergeFrom(n0) = rename_func(
for i, s in enumerate(n.input):
n.input[i] = rename_func(s) if s[0]!='^' else '^'+rename_func(s[1:])
return res_def
def show_graph(graph_def, max_const_size=32, width=800, height=600):
"""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 = """
function load() {{
document.getElementById("{id}").pbtxt = {data};
<link rel="import" href="" onload=load()>
<div style="height:{height}px">
<tf-graph-basic id="{id}"></tf-graph-basic>
""".format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()), height=str(height))
iframe = """
<iframe seamless style="width:{width}px;height:{height}px;border:0" srcdoc="{code}"></iframe>
""".format(code=code.replace('"', '&quot;'), width=width, height=str(height + 20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment