0

我在一家Jupyter筆記本使用TensorFlow我的第一次深度學習模型時,我想,以產生說明了網絡的各個層的簡化圖。具體來說,圖表如在this answer圖爲:生成*簡單* TensorFlow圖說明

enter image description here

這個很簡單,乾淨,我能理解這是怎麼回事。這比捕捉100%的細節更重要。

enter image description here

如何可以採取tf.Graph對象並自動生成類似於上面的一個的曲線圖:與由TensorBoard產生的曲線圖,其是一個完整的fustercluck對比?如果它也可以顯示在Jupyter筆記本中,則爲獎勵積分。

回答

1

總之 - 你不能。 TF是一個低層次的圖書館,它沒有「高層次操作」的概念,它具有操作性,而且這是它能夠以您想的方式可視化的唯一東西。特別是,從數學的角度來看,圖中沒有「神經元」,只有張量彼此相乘,這種額外的「語義」只是爲了讓人類更容易談論這一點,但它並不是真的編碼在你的圖表中。

你可以做的是自己組節點通過specifing您的圖形部分variable_scope,然後,在TB顯示後,他們將被顯示爲一個節點。它不會給你這種「每神經元樣」的可視化風格,但至少它會隱藏很多細節。創建神經網絡的一個很好的,視覺上吸引人的可視化對於自己的權利來說是一種「藝術」,並且一般來說是一項艱鉅的任務。

1

這裏的代碼片段,我們在我們的筆記本電腦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實現一個簡單的線性迴歸模型的一個例子:

enter image description here

下面是用於部署和生產服務TensorFlow模型(使用上述代碼段也呈現)的優化版本:

enter image description here

更多的例子和這些類型的優化的細節在http://pipeline.ai