当节点多的时候就需要使用数据流图来控制op
# coding: utf-8
# In[1]:
import tensorflow as tf
# In[4]:
with tf.name_scope("Scope_A"):#名称作用域的基本用法是将OP添加到这个语句块里
a = tf.add(1,2,name="A_add")
b = tf.mul(a,3,name="A_mul")
# In[5]:
with tf.name_scope("Score_B"):
c = tf.add(4,5,name="B_add")
d = tf.mul(c,6,name="B_mul")
e = tf.add(b,d,name="output")
# In[6]:
#以上定义了两个名称作用域
# In[8]:
writer = tf.train.SummaryWriter('./name_score_1',
graph=tf.get_default_graph())
在name_scope_1目录下,
键入
tensorboard--logdir='./name_scope_1'
在浏览器输入localhost:6006就可以看到数据流图了
另外,名称区域也可以包含名称区域,同一颜色的名称区域代表这些名称区域有相同的OP设置
如:
graph = tf.Graph()
with graph.as_default():
in_1 = tf.placeholder(tf.float32,shape=[],name="input_a")
in_2 = tf.placeholder(tf.float32,shape=[],name="input_b")
const = tf.constant(3,dtype=tf.float32,name="statice_value")
with tf.name_scope("Transformation"):
with tf.name_scope("A"):
A_mul = tf.mul(in_1,const)
A_out = tf.sub(A_mul,in_1)
with tf.name_scope("B"):
B_mul = tf.mul(in_2,const)
B_out = tf.sub(B_mul,in_2)
with tf.name_scope("C"):
c_div = tf.div(A_out,B_out)
c_out = tf.add(c_div,const)
with tf.name_scope("D"):
D_div = tf.div(B_out,A_out)
D_out = tf.add(D_div,const)
out = tf.maximum(c_out,D_out)
writer = tf.train.SummaryWriter('./name_score_2',graph=graph)
writer.close()