Working with multiple graphs in TensorFlow

前端 未结 6 716
一个人的身影
一个人的身影 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:09

    I was having similar difficulties dealing with multiple graphs on a IPython notebook. What works for my purposes is to encapsulate each graph and its session in a function. I realize this is more of a hack I guess, I don't know anything about namespaces and I know OP wanted something along those lines. Maybe it will help someone I dunno, you can also pass results between computations.

    import tensorflow as tf
    
    def Graph1():
        g1 = tf.Graph()
        with g1.as_default() as g:
            matrix1 = tf.constant([[3., 3.]])
            matrix2 = tf.constant([[2.],[2.]])
            product = tf.matmul( matrix1, matrix2, name = "product")
    
        with tf.Session( graph = g ) as sess:
            tf.initialize_all_variables().run()
            return product
    
    
    def Graph2(incoming):
        i = incoming
        g2 = tf.Graph()
        with g2.as_default() as g:
            matrix1 = tf.constant([[4., 4.]])
            matrix2 = tf.constant([[5.],[5.]])
            product = tf.matmul( matrix1, matrix2, name = "product" )
    
        with tf.Session( graph = g ) as sess:
            tf.initialize_all_variables().run()
            print product
            print i
    
    print Graph1()
    
    Graph2(Graph1())
    

提交回复
热议问题