2016-08-16 199 views
0

在以下幾行中,是否有人可以確認Tensorflow添加到單個loss張量,而不是創建多個張量(全部命名爲loss)?Tensorflow變量 - 添加到相同名稱

loss = tf.nn.l2_loss(a)  
loss = tf.add(loss, tf.nn.l2_loss(b)) 
loss = tf.add(loss, tf.nn.l2_loss(c)) 

謝謝!

回答

2

下面是您要創建的圖表。每次您執行tf.<something>時,它都會附加到默認圖表。這就是說,從上圖可以看到,它實際上有三個loss節點enter image description here

與此代碼

from IPython.display import clear_output, Image, display, HTML 

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

import tensorflow as tf 
import numpy as np 
tf.reset_default_graph() 
a = tf.placeholder(tf.float32) 
b = tf.placeholder(tf.float32) 
c = tf.placeholder(tf.float32) 

loss = tf.nn.l2_loss(a)  
loss = tf.add(loss, tf.nn.l2_loss(b)) 
loss = tf.add(loss, tf.nn.l2_loss(c)) 

show_graph(tf.get_default_graph().as_graph_def()) 
+0

謝謝雅羅斯拉夫生成總結的效果。 –