生成*简单* TensorFlow图说明
我在一家Jupyter笔记本使用TensorFlow我的第一次深度学习模型时,我想,以产生说明了网络的各个层的简化图。具体来说,图表如在this answer图为:生成*简单* TensorFlow图说明
这个很简单,干净,我能理解这是怎么回事。这比捕捉100%的细节更重要。
如何可以采取tf.Graph
对象并自动生成类似于上面的一个的曲线图:与由TensorBoard产生的曲线图,其是一个完整的fustercluck对比?如果它也可以显示在Jupyter笔记本中,则为奖励积分。
总之 - 你不能。 TF是一个低层次的图书馆,它没有“高层次操作”的概念,它具有操作性,而且这是它能够以您想的方式可视化的唯一东西。特别是,从数学的角度来看,图中没有“神经元”,只有张量彼此相乘,这种额外的“语义”只是为了让人类更容易谈论这一点,但它并不是真的编码在你的图表中。
你可以做的是自己组节点通过specifing您的图形部分variable_scope,然后,在TB显示后,他们将被显示为一个节点。它不会给你这种“每神经元样”的可视化风格,但至少它会隐藏很多细节。创建神经网络的一个很好的,视觉上吸引人的可视化对于自己的权利来说是一种“艺术”,并且一般来说是一项艰巨的任务。
这里的代码片段,我们在我们的笔记本电脑PipelineAI使用我们的Jupyter笔记本电脑内内嵌显示我们TensorFlow图:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import re
from google.protobuf import text_format
from tensorflow.core.framework import graph_pb2
def convert_graph_to_dot(input_graph, output_dot, is_input_graph_binary):
graph = graph_pb2.GraphDef()
with open(input_graph, "rb") as fh:
if is_input_graph_binary:
graph.ParseFromString(fh.read())
else:
text_format.Merge(fh.read(), graph)
with open(output_dot, "wt") as fh:
print("digraph graphname {", file=fh)
for node in graph.node:
output_name = node.name
print(" \"" + output_name + "\" [label=\"" + node.op + "\"];", file=fh)
for input_full_name in node.input:
parts = input_full_name.split(":")
input_name = re.sub(r"^\^", "", parts[0])
print(" \"" + input_name + "\" -> \"" + output_name + "\";", file=fh)
print("}", file=fh)
print("Created dot file '%s' for graph '%s'." % (output_dot, input_graph))
input_graph='/root/models/optimize_me/linear/cpu/unoptimized_cpu.pb'
output_dot='/root/notebooks/unoptimized_cpu.dot'
convert_graph_to_dot(input_graph=input_graph, output_dot=output_dot, is_input_graph_binary=True)
使用Graphviz的,你可以转换.DOT使用%%来.png格式你的笔记本电池内的bash魔法:
%%bash
dot -T png /root/notebooks/unoptimized_cpu.dot \
-o /root/notebooks/unoptimized_cpu.png > /tmp/a.out
最后,在你的笔记本显示图表:
from IPython.display import Image
Image('/root/notebooks/unoptimized_cpu.png', width=1024, height=768)
这里是在TensorFlow实现一个简单的线性回归模型的一个例子:
下面是用于部署和生产服务TensorFlow模型(使用上述代码段也呈现)的优化版本:
更多的例子和这些类型的优化的细节在http://pipeline.ai