Working with multiple graphs in TensorFlow

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

    Your problem is that you are calling the latest variable product which points to a tensor created on g2 - you overwrote it in your second scope. Just relabel all your variables and you should be good to go. The working code is below.

    import tensorflow as tf
    
    g1 = tf.Graph() with g1.as_default() as g:
        with g.name_scope( "g1" ) as scope:
            matrix11 = tf.constant([[3., 3.]])
            matrix21 = tf.constant([[2.],[2.]])
            product1 = tf.matmul(matrix11, matrix21)
    
    tf.reset_default_graph()
    
    g2 = tf.Graph() with g2.as_default() as g:
        with g.name_scope( "g2" ) as scope:
            matrix12 = tf.constant([[4., 4.]])
            matrix22 = tf.constant([[5.],[5.]])
            product2 = tf.matmul(matrix12, matrix22)
    
    tf.reset_default_graph()
    
    with tf.Session( graph = g1 ) as sess:
        result = sess.run( product1 )
        print( result )
    

提交回复
热议问题