tensorflow-名称作用域

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Sep  6 10:16:37 2018
@author: myhaspl
@email:[email protected]
"""

import tensorflow as tf

with tf.name_scope("Scope_A"):
    asub=tf.subtract(1,2,name="A_sub")
    amul=tf.multiply(asub,3,name="B_mul")

with tf.name_scope("Scope_B"):
    badd=tf.add(5,3,name="B_add")
    bmul=tf.multiply(badd,3,name="B_div")

result=tf.add(amul,bmul,name="result")
with tf.Session() as sess:  
    sess.run(result)
    writer=tf.summary.FileWriter("name_scope",graph=sess.graph)
    writer.close()

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Sep  6 10:16:37 2018
@author: myhaspl
@email:[email protected]
"""

import tensorflow as tf
g1=tf.Graph()

with g1.as_default():
    y=tf.Variable(0.)
    with tf.name_scope("Scope_C"):

        a=tf.placeholder(tf.float32,shape=(),name="input_a")
        b=tf.placeholder(tf.float32,shape=(),name="input_b")
        with tf.name_scope("Scope_A"):
            asub=tf.subtract(a,b,name="A_sub")
            amul=tf.multiply(asub,3,name="B_mul")

        with tf.name_scope("Scope_B"): 
            badd=tf.add(a,b,name="B_add")
            bmul=tf.multiply(badd,3,name="B_div")
    g1res=tf.add(amul,bmul,name="g1result")
    result=y.assign(y+g1res)

    init=tf.global_variables_initializer()
    writer=tf.summary.FileWriter("name_scope")

with tf.Session(graph=g1) as sess1:  
    sess1.run(init)
    print sess1.run(result,feed_dict={a:28,b:9})
    writer.add_graph(g1)
    writer.close()

168.0

猜你喜欢

转载自blog.51cto.com/13959448/2324225