2017-12-14 228 views

Odpowiedz

1

Oto, w jaki sposób można wyświetlać modele w Internecie w Google Colab. Poniżej jest bardzo prosty przykład, który wyświetla zastępczy:

from IPython.display import clear_output, Image, display, HTML 
import tensorflow as tf 
import numpy as np 
from google.colab import files 

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


"""Create a sample tensor""" 
sample_placeholder= tf.placeholder(dtype=tf.float32) 
"""Show it""" 
graph_def = tf.get_default_graph().as_graph_def() 
show_graph(graph_def) 

Obecnie nie można uruchomić usługę Tensorboard w Google Colab sposób go uruchomić lokalnie. Nie możesz też wyeksportować całego dziennika na Dysk przez coś takiego, jak summary_writer = tf.summary.FileWriter('./logs', graph_def=sess.graph_def), dzięki czemu możesz go pobrać i obejrzeć lokalnie.

9

Obecnie używam ngrok do tunelowania ruchu do localhost.
Przykładowy colab można znaleźć pod numerem here.

Są to etapy (Fragmenty kodu reprezentują komórki typu "code" w colab):

  1. Get TensorBoard działających w tle.
    Zainspirowany przez this answer.

    LOG_DIR = '/tmp/log' 
    get_ipython().system_raw(
        'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &' 
        .format(LOG_DIR) 
    ) 
    
  2. Pobierz i rozpakuj ngrok.
    Zastąp link przekazany do wget odpowiednim łączem pobierania dla systemu operacyjnego.

    ! wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip 
    ! unzip ngrok-stable-linux-amd64.zip 
    
  3. proces uruchomić ngrok tło ...

    get_ipython().system_raw('./ngrok http 6006 &') 
    

    ... i pobrać url publicznego. Source

    ! curl -s http://localhost:4040/api/tunnels | python3 -c \ 
        "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"