Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何生成tensorflow变量';本地';?_Python_Tensorflow - Fatal编程技术网

Python 如何生成tensorflow变量';本地';?

Python 如何生成tensorflow变量';本地';?,python,tensorflow,Python,Tensorflow,似乎由tf.get_variable()或tf.variable()生成的tensorflow变量都是全局变量。发生在我身上的情况如下:假设我创建了以下两个文件: main.py from prac_var import defineVar for i in range(1000): defineVar() from prac_var import defineVar for i in range(1000): defineVar() prac_var.py import

似乎由
tf.get_variable()
tf.variable()
生成的tensorflow变量都是全局变量。发生在我身上的情况如下:假设我创建了以下两个文件:

  • main.py

    from prac_var import defineVar
    for i in range(1000):
        defineVar()
    
    from prac_var import defineVar
    for i in range(1000):
        defineVar()
    
  • prac_var.py

    import tensorflow as tf
    def defineVar():
        with tf.variable_scope('weight'):    
            W = tf.Variable(tf.zeros([1,1]),name='W')
            print('\n',tf.trainable_variables())
    
    import tensorflow as tf
    def defineVar():
        tf.reset_default_graph()
        with tf.variable_scope('weight'):    
            W = tf.Variable(tf.zeros([1,1]),name='W')
            print('\n',tf.trainable_variables())
    
  • 现在,如果我运行main.py,它将生成

    [<tf.Variable 'weight/W:0' shape=(1, 1) dtype=float32_ref>]
    
    [<tf.Variable 'weight/W:0' shape=(1, 1) dtype=float32_ref>, <tf.Variable 'weight_1/W:0' shape=(1, 1) dtype=float32_ref>]
    
    [<tf.Variable 'weight/W:0' shape=(1, 1) dtype=float32_ref>, <tf.Variable 'weight_1/W:0' shape=(1, 1) dtype=float32_ref>, <tf.Variable 'weight_2/W:0' shape=(1, 1) dtype=float32_ref>]
    ...
    
    []
    [, ]
    [, ]
    ...
    
    而我真正想要的是

    [<tf.Variable 'weight/W:0' shape=(1, 1) dtype=float32_ref>]
    [<tf.Variable 'weight/W:0' shape=(1, 1) dtype=float32_ref>]
    [<tf.Variable 'weight/W:0' shape=(1, 1) dtype=float32_ref>]
    ...
    
    []
    []
    []
    ...
    

    我如何才能以一种非平凡的方式解决这个问题?

    首先,我想知道您是否了解Tensorflow首先构造一个计算图,然后使用您定义的图进行所有计算。使用
    tf.get_variable()
    可以从任何地方访问所有变量,如果您知道变量的名称

    如果两次都在两个不同的位置获取权重,试图获取变量
    W
    ,则“重用”这些权重。这就是引入变量作用域的原因:

    如果你想拥有两种不同的权重,你可以说:

    with tf.variable_scope('mnistweights'):
      Wmnist = tf.get_variable('W',...)
    with tf.variable_scope('exampleweights'):
      Wtest = tf.get_variable('W',...)
    
    现在有了名为mnistweights/W和examplewweights/W的变量


    希望你能更好地理解它

    我意外地找到了解决问题的办法。只需添加一行
    tf.reset\u default\u graph()

  • main.py

    from prac_var import defineVar
    for i in range(1000):
        defineVar()
    
    from prac_var import defineVar
    for i in range(1000):
        defineVar()
    
  • prac_var.py

    import tensorflow as tf
    def defineVar():
        with tf.variable_scope('weight'):    
            W = tf.Variable(tf.zeros([1,1]),name='W')
            print('\n',tf.trainable_variables())
    
    import tensorflow as tf
    def defineVar():
        tf.reset_default_graph()
        with tf.variable_scope('weight'):    
            W = tf.Variable(tf.zeros([1,1]),name='W')
            print('\n',tf.trainable_variables())
    

  • 我想我还没有向你清楚地表达我的观点。你能再看一次吗?你到底想要什么?现在移除权重并再次添加1000次。。。