tf.Graph()

part1


In [1]:
import tensorflow as tf

In [4]:
x = tf.placeholder(tf.float32, shape=[784, 10]) # x belong to default_graph
g1 = tf.Graph()
with g1.as_default():
    x1 = tf.constant([1., 2.])                     # x1 belong to "g1"

with tf.Graph().as_default() as g2:                # x2 belong to 'g2'
    x2 = tf.placeholder(tf.float32, shape=[2, 2])

In [5]:
print (x.graph is tf.get_default_graph())
print (x1.graph is g1)
print (x2.graph is g2)
##---------    
print (x.graph is g1)    
print (x.graph is g2)


True
True
True
False
False

part2 Graph


In [7]:
g1 = tf.Graph()
with g1.as_default():
    c = tf.constant(10., name='c')                ## outtest scope
    print (c.op.name == 'c')
    with g1.name_scope("scope_1") as scope_1:     ## scope_1
        scope1_c = tf.constant(10., name='c')
        print (scope1_c.op.name == 'scope_1/c')
    
    with g1.name_scope('scope_2') as scope_2:     ## scope_2
        scope2_c = tf.constant(20., name='c')
        print (scope2_c.op.name == 'scope_2/c')
    
    c1 = tf.constant(10., name='c')               ## outtest scope
    print (c1.op.name == 'c_1')
    
    with g1.name_scope(scope_1):                  ## scope_1
        scope1_c1 = tf.constant(10., name='c')
        scope1_c2 = tf.constant(10., name='c')
        print (scope1_c1.op.name == 'scope_1/c_1')
        print (scope1_c2.op.name == 'scope_1/c_2')
        
        with g1.name_scope('scope_11') as scope_11:   ## scope_1/scope_11
            scope11_c = tf.constant(40, name='c')
            print (scope11_c.op.name == 'scope_1/scope_11/c')
        
        with g1.name_scope(scope_2):                  ## scope_2
            scope2_c1 = tf.constant(10., name='c')
            print (scope2_c1.op.name == 'scope_2/c_1')
        
        scope1_c3 = tf.constant(10., name='c')        ## scope_1
        print (scope1_c3.op.name == 'scope_1/c_3')
        
        with g1.name_scope(""):                       ## outtest
            c2 = tf.constant(10., name='c')
            print (c2.op.name == 'c_2')
    

    print (g1.get_all_collection_keys())


True
True
True
True
True
True
True
True
True
True
[]

part3 Operation


In [88]:
with tf.Graph().as_default() as g3:
    input = basics.input_tensor(shape=[None, 784], name='input')
    weights = basics.weights_tensor(shape=[784, 10], name='weights')
    biases = basics.biases_tensor(shape=[10], name='biases')
    z = tf.matmul(input, weights) + biases
    print (weights.name, weights.dtype)
    print (z.name, z.dtype)
    print (z.value_index)


(u'weights:0', tf.float32_ref)
(u'add:0', tf.float32)
0

In [93]:
print (tf.GraphKeys)


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-93-0713352a9d89> in <module>()
----> 1 print (tf.GraphKeys().keys())

AttributeError: 'GraphKeys' object has no attribute 'keys'

In [ ]: