1. 程式人生 > >jupter 下呼叫其他目錄下檔案及tensorboard視覺化實現

jupter 下呼叫其他目錄下檔案及tensorboard視覺化實現

【1】jupyter 呼叫其他目錄下函式操作

import numpy as np
import tensorflow as tf
dir1 = r"E:\tf_project\練習"
import sys
sys.path.append(dir1)
import tensorboard1 as tb

只要知道dir1,然後import就可以了。

【2】jupyter 下進行tensorboard視覺化實現

或者可以直接使用如下程式碼

該程式碼段所在檔名稱為: tensorboard1.py

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))

然後自己寫的程式碼位於另一個檔案中,比如下面:

import numpy as np
import tensorflow as tf
dir1 = r"E:\tf_project\練習"
import sys
sys.path.append(dir1)
import tensorboard1 as tb


a = [i+1 for i in range(6)]
b = [2*(i+2) for i in range(6)]

input_c = tf.constant(a, shape=[2, 3], name="input_c")
input_d = tf.constant(b, shape=[3, 2], name="input_d")
input_c = tf.Print(input_c, [input_c, input_c.shape, "anything i want"], message="\ninput_c: ", summarize=100)

e = tf.matmul(input_c, input_d, name="matmul")
with tf.Session() as sess:
    print(sess.run(e))
    tb.show_graph(sess.graph)

顯示結果:

直接使用pycharm,然後使用tensorboard顯示,

import numpy as np
import tensorflow as tf

input_c = tf.constant([i+1 for i in range(6)], shape=[2, 3], name="input_c")
input_d = tf.constant([2*(i+2) for i in range(6)], shape=[3, 2], name="input_d")
input_e = tf.Print(input_c, [input_c, input_c.shape, "anything i want"], message="\ninput_c: ", summarize=100)
result = tf.matmul(input_e, input_d, name="matmul")

with tf.Session() as sess:
    print(sess.run(result))
    summary_writer = tf.summary.FileWriter("./train", sess.graph)
    summary_writer.close()

顯示結果:

顯示結果是一致的。

【3】不使用tf.Print()函式時:

import numpy as np
import tensorflow as tf

input_c = tf.constant([i+1 for i in range(6)], shape=[2, 3], name="input_c")
input_d = tf.constant([2*(i+2) for i in range(6)], shape=[3, 2], name="input_d")
#input_e = tf.Print(input_c, [input_c, input_c.shape, "anything i want"], message="\ninput_c: ", summarize=100)
result = tf.matmul(input_c, input_d, name="matmul")

with tf.Session() as sess:
    print(sess.run(result))
    summary_writer = tf.summary.FileWriter("./train", sess.graph)
    summary_writer.close()

當然,可能因為本人也是剛開始學習tensorflow,從圖中可以看到該結果圖,存在data-1和data_2,但是我一直不知道怎麼驗證他們分別代表什麼,如果有道友瞭解應該怎麼做,歡迎指點。