1
+ import tensorflow as tf
2
+ import numpy as np
3
+ from IPython .display import clear_output , Image , display , HTML
4
+
5
+
6
+ def strip_consts (graph_def , max_const_size = 32 ):
7
+ """Strip large constant values from graph_def.
8
+
9
+ From http://stackoverflow.com/questions/38189119/simple-way-to-visualize-a-tensorflow-graph-in-jupyter
10
+
11
+ """
12
+ strip_def = tf .GraphDef ()
13
+ for n0 in graph_def .node :
14
+ n = strip_def .node .add ()
15
+ n .MergeFrom (n0 )
16
+ if n .op == 'Const' :
17
+ tensor = n .attr ['value' ].tensor
18
+ size = len (tensor .tensor_content )
19
+ if size > max_const_size :
20
+ tensor .tensor_content = "<stripped %d bytes>" % size
21
+ return strip_def
22
+
23
+
24
+ def show_graph (graph_def , max_const_size = 32 ):
25
+ """Visualize TensorFlow graph.
26
+
27
+ From http://stackoverflow.com/questions/38189119/simple-way-to-visualize-a-tensorflow-graph-in-jupyter
28
+
29
+ """
30
+ if hasattr (graph_def , 'as_graph_def' ):
31
+ graph_def = graph_def .as_graph_def ()
32
+ strip_def = strip_consts (graph_def , max_const_size = max_const_size )
33
+ code = """
34
+ <script>
35
+ function load() {{
36
+ document.getElementById("{id}").pbtxt = {data};
37
+ }}
38
+ </script>
39
+ <link rel="import" href="https://tensorboard.appspot.com/tf-graph-basic.build.html" onload=load()>
40
+ <div style="height:600px">
41
+ <tf-graph-basic id="{id}"></tf-graph-basic>
42
+ </div>
43
+ """ .format (data = repr (str (strip_def )), id = 'graph' + str (np .random .rand ()))
44
+
45
+ iframe = """
46
+ <iframe seamless style="width:1200px;height:620px;border:0" srcdoc="{}"></iframe>
47
+ """ .format (code .replace ('"' , '"' ))
48
+ display (HTML (iframe ))
0 commit comments