【问题标题】:Simple way to visualize a TensorFlow graph in Jupyter?在 Jupyter 中可视化 TensorFlow 图的简单方法?
【发布时间】:2016-11-06 10:41:55
【问题描述】:

可视化 TensorFlow 图的官方方法是使用 TensorBoard,但有时我只是想在使用 Jupyter 时快速查看一下图。

是否有快速的解决方案,最好是基于 TensorFlow 工具或标准 SciPy 包(如 matplotlib),但如果有必要基于 3rd 方库?

【问题讨论】:

  • DeepDream recipe 工作得很好,但是 TensorBoard 使用 TensorFlow 添加的内部额外节点来绘制一个难以理解的图形来运行它的操作。为了提高易读性,我写了一个article,其中包含一些指南来定义你的模型,以便更好地了解它。

标签: tensorflow jupyter graph-visualization tensorboard


【解决方案1】:

这是我在某个时候从 Alex Mordvintsev 深梦notebook 中复制的一个食谱

from IPython.display import clear_output, Image, display, HTML
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 = "<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:1200px;height:620px;border:0" srcdoc="{}"></iframe>
    """.format(code.replace('"', '&quot;'))
    display(HTML(iframe))

然后可视化当前图表

show_graph(tf.get_default_graph().as_graph_def())

如果你的图表保存为 pbtxt,你可以这样做

gdef = tf.GraphDef()
from google.protobuf import text_format
text_format.Merge(open("tf_persistent.pbtxt").read(), gdef)
show_graph(gdef)

你会看到这样的东西

【讨论】:

  • 我刚刚找到你提到的来源。也许您可以将 URL 添加到您的答案中? github.com/tensorflow/tensorflow/blob/master/tensorflow/…
  • 有没有办法从主图中添加/删除节点,类似于 TensorBoard 功能?
  • 此实现不允许添加/删除节点。一些交互确实有效,但并非如此。
  • 有没有办法对标量摘要执行此操作?
  • 这很棒。谢谢!
【解决方案2】:

我写了一个简单的助手,它从 jupyter notebook 启动一个张量板。只需在笔记本顶部的某处添加此功能

def TB(cleanup=False):
    import webbrowser
    webbrowser.open('http://127.0.1.1:6006')

    !tensorboard --logdir="logs"

    if cleanup:
        !rm -R logs/

然后在生成摘要时运行它TB()。它不是在同一个 jupyter 窗口中打开图表,而是:

  • 启动张量板
  • 使用张量板打开一个新标签
  • 导航到此标签

完成探索后,只需单击选项卡,然后停止中断内核。如果你想清理你的日志目录,运行后,只需运行TB(1)

【讨论】:

  • @AjaySinghNegi 您必须确保 tensorboard 已安装并且在您启动 Jupyter 的环境中可用。如果仍然无法正常工作,请尝试将 !tensorboard 替换为 tensorboard 二进制文件的完整路径。跨度>
【解决方案3】:

这种可视化的 Tensorboard/iframe 免费版本,可以承认很快就会变得混乱

import pydot
from itertools import chain
def tf_graph_to_dot(in_graph):
    dot = pydot.Dot()
    dot.set('rankdir', 'LR')
    dot.set('concentrate', True)
    dot.set_node_defaults(shape='record')
    all_ops = in_graph.get_operations()
    all_tens_dict = {k: i for i,k in enumerate(set(chain(*[c_op.outputs for c_op in all_ops])))}
    for c_node in all_tens_dict.keys():
        node = pydot.Node(c_node.name)#, label=label)
        dot.add_node(node)
    for c_op in all_ops:
        for c_output in c_op.outputs:
            for c_input in c_op.inputs:
                dot.add_edge(pydot.Edge(c_input.name, c_output.name))
    return dot

后面可以跟

from IPython.display import SVG
# Define model
tf_graph_to_dot(graph).write_svg('simple_tf.svg')
SVG('simple_tf.svg')

将图形呈现为静态 SVG 文件中的记录

【讨论】:

  • 不错的代码,虽然我想知道为什么 for c_node in all_tens_dict.keys() 循环的元素多于最终图中的节点。
【解决方案4】:

我为张量板集成编写了一个 Jupyter 扩展。它可以:

  1. 只需单击 Jupyter 中的按钮即可启动 tensorboard
  2. 管理多个张量板实例。
  3. 与 Jupyter 界面无缝集成。

Github:https://github.com/lspvic/jupyter_tensorboard

【讨论】:

  • 在此处粘贴答案的基本部分。并且仅使用链接作为参考。
【解决方案5】:

代码

def tb(logdir="logs", port=6006, open_tab=True, sleep=2):
    import subprocess
    proc = subprocess.Popen(
        "tensorboard --logdir={0} --port={1}".format(logdir, port), shell=True)
    if open_tab:
        import time
        time.sleep(sleep)
        import webbrowser
        webbrowser.open("http://127.0.0.1:{}/".format(port))
    return proc

用法

tb()               # Starts a TensorBoard server on the logs directory, on port 6006
                   # and opens a new tab in your browser to use it.

tb("logs2", 6007)  # Starts a second server on the logs2 directory, on port 6007,
                   # and opens a new tab to use it.

启动服务器不会阻止 Jupyter(除了 2 秒以确保服务器在打开选项卡之前有时间启动)。当您中断内核时,所有 TensorBoard 服务器都将停止。

高级用法

如果您想要更多控制权,您可以像这样以编程方式终止服务器:

server1 = tb()
server2 = tb("logs2", 6007)
# and later...
server1.kill()  # stops the first server
server2.kill()  # stops the second server

如果您不想打开新标签页,可以设置open_tab=False。如果 2 秒在您的系统上过多或不够,您还可以将 sleep 设置为其他值。

如果您希望在 TensorBoard 运行时暂停 Jupyter,那么您可以调用任何服务器的 wait() 方法。这将阻止 Jupyter,直到您中断内核,这将停止此服务器和所有其他服务器。

server1.wait()

先决条件

此解决方案假定您已安装 TensorBoard(例如,使用 pip install tensorboard)并且它在您启动 Jupyter 的环境中可用。

致谢

这个答案的灵感来自@SalvadorDali 的答案。他的解决方案既好又简单,但我希望能够在不阻塞 Jupyter 的情况下启动多个 tensorboard 实例。另外,我不喜欢删除日志目录。相反,我在根日志目录上启动 tensorboard,每个 TensorFlow 运行日志都在不同的子目录中。

【讨论】:

  • 我喜欢这个答案。我希望我能投两次票。
【解决方案6】:

TensorBoard Visualize Nodes - Architecture Graph

&lt;img src="https://www.tensorflow.org/images/graph_vis_animation.gif" width=1300 height=680&gt;

【讨论】:

    【解决方案7】:

    TensorFlow 2.0 现在支持TensorBoardinJupyter通过魔术命令(例如%tensorboard --logdir logs/train)。这是教程和示例的link

    [编辑 1、2]

    正如@MiniQuark 在评论中提到的,我们需要先加载扩展(%load_ext tensorboard.notebook)。

    以下是使用图形模式@tf.functiontf.keras(在tensorflow==2.0.0-alpha0中)的使用示例:

    1。在 TF2 中使用 图形模式 的示例(通过 tf.compat.v1.disable_eager_execution()

    %load_ext tensorboard.notebook
    import tensorflow as tf
    tf.compat.v1.disable_eager_execution()
    from tensorflow.python.ops.array_ops import placeholder
    from tensorflow.python.training.gradient_descent import GradientDescentOptimizer
    from tensorflow.python.summary.writer.writer import FileWriter
    
    with tf.name_scope('inputs'):
       x = placeholder(tf.float32, shape=[None, 2], name='x')
       y = placeholder(tf.int32, shape=[None], name='y')
    
    with tf.name_scope('logits'):
       layer = tf.keras.layers.Dense(units=2)
       logits = layer(x)
    
    with tf.name_scope('loss'):
       xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)
       loss_op = tf.reduce_mean(xentropy)
    
    with tf.name_scope('optimizer'):
       optimizer = GradientDescentOptimizer(0.01)
       train_op = optimizer.minimize(loss_op)
    
    FileWriter('logs/train', graph=train_op.graph).close()
    %tensorboard --logdir logs/train
    

    2。与上面的示例相同,但现在使用 @tf.function 装饰器进行前后传递并且不禁用急切执行:

    %load_ext tensorboard.notebook
    import tensorflow as tf
    import numpy as np
    
    logdir = 'logs/'
    writer = tf.summary.create_file_writer(logdir)
    tf.summary.trace_on(graph=True, profiler=True)
    
    @tf.function
    def forward_and_backward(x, y, w, b, lr=tf.constant(0.01)):
    
        with tf.name_scope('logits'):
            logits = tf.matmul(x, w) + b
        
        with tf.name_scope('loss'):
            loss_fn = tf.nn.sparse_softmax_cross_entropy_with_logits(
                labels=y, logits=logits)
            reduced = tf.reduce_sum(loss_fn)
            
        with tf.name_scope('optimizer'):
            grads = tf.gradients(reduced, [w, b])
            _ = [x.assign(x - g*lr) for g, x in zip(grads, [w, b])]
        return reduced
    
    # inputs
    x = tf.convert_to_tensor(np.ones([1, 2]), dtype=tf.float32)
    y = tf.convert_to_tensor(np.array([1]))
    # params
    w = tf.Variable(tf.random.normal([2, 2]), dtype=tf.float32)
    b = tf.Variable(tf.zeros([1, 2]), dtype=tf.float32)
    
    loss_val = forward_and_backward(x, y, w, b)
    
    with writer.as_default():
        tf.summary.trace_export(
            name='NN',
            step=0,
            profiler_outdir=logdir)
    
    %tensorboard --logdir logs/
    

    3。使用tf.keras API:

    %load_ext tensorboard.notebook
    import tensorflow as tf
    import numpy as np
    x_train = [np.ones((1, 2))]
    y_train = [np.ones(1)]
    
    model = tf.keras.models.Sequential([tf.keras.layers.Dense(2, input_shape=(2, ))])
                                        
    model.compile(
        optimizer='sgd',
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'])
    
    logdir = "logs/"
    
    tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=logdir)
    
    model.fit(x_train,
              y_train,
              batch_size=1,
              epochs=1,
              callbacks=[tensorboard_callback])
    
    %tensorboard --logdir logs/
    

    这些示例将在单元格下方生成如下内容:

    【讨论】:

    • 或许补充一下需要先加载扩展:%load_ext tensorboard.notebook
    • @MiniQuark - 在这个时间点,你会建议采用这个公认的答案VS下面的选项(包括你的?)
    • 是的,我绝对推荐这个解决方案,尤其是选项 3,使用 tf.keras(有时选项 2,使用 tf.function)。我认为@Vlad 将选项 1(禁用急切模式)首先显示看起来像 TF 1.x 的代码,然后他使用 tf.function 对其进行了简化,然后使用 tf.keras 对其进行了进一步简化。关键是:tf.keras 让它变得非常简单。
    【解决方案8】:

    TF 2.x 的另一个快速选项是通过plot_model() 函数。它已经内置到更新版本的 TF 实用程序中。例如:

    import tensorflow
    from tensorflow.keras.utils import plot_model
    
    plot_model(model, to_file=('output_filename.png'))
    

    这个功能很好,因为您可以让它显示图层名称,以高 DPI 输出,将其配置为水平绘图,以及任何其他选项。这是该函数的文档:https://www.tensorflow.org/api_docs/python/tf/keras/utils/plot_model

    即使对于大型模型,绘图也非常快,即使对于具有多个进出连接的复杂模型也能很好地工作。

    【讨论】:

      猜你喜欢
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 2021-01-11
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多