Working with multiple graphs in TensorFlow

前端 未结 6 715
一个人的身影
一个人的身影 2020-11-27 15:28

Can someone explain to me how name_scope works in TensorFlow?

Suppose I have the following code:



        
6条回答
  •  眼角桃花
    2020-11-27 16:10

    You have 3 graphs , not 2 graphs ( g1 or g2 ) .

    You can see 3 graphs by id()

    ...
    
    print 'g1           ', id(g1)
    print 'g2           ', id(g2)
    print 'current graph', id ( tf.get_default_graph() )
    
    with tf.Session( graph = g1 ) as sess:
        result = sess.run( product )
        print( result )
    

    Using "with g1.as_default(): " make the same error

    ...
    
    with g1.as_default() :
        with tf.Session( graph = g1 ) as sess:
            result = sess.run( product )
            print( result )
    

    Because, you have two 'product' , not one.

    g1 = tf.Graph()
    with g1.as_default() as g:
        ...
        print 'g1 product', id ( product )
    
    ...
    
    g2 = tf.Graph()
    with g2.as_default() as g:
        ...
        print 'g2 product', id ( product )
    
    with tf.Session( graph = g1 ) as sess:
        print 'last product', id(product)
        ...
    

    last product == g2 product

    ...
        product = tf.matmul(matrix1, matrix2, name='g1_product')
    ...
    with g1.as_default() as g:
        with tf.Session() as sess:
            product = g.get_tensor_by_name(  "g1_product:0" )
            result = sess.run( product )
            print( result )
    

    Above code work.

    But, two variables with the same name ( product )

    Encapsulating with class is good?

提交回复
热议问题