2016-11-10 26 views
4

如何在TensorFlow中重用变量?我想重用tf.contrib.layers.linearTensorFlow:varscope.reuse_variables()

with tf.variable_scope("root") as varscope: 
    inputs_1 = tf.constant(0.5, shape=[2, 3, 4]) 
    inputs_2 = tf.constant(0.5, shape=[2, 3, 4]) 
    outputs_1 = tf.contrib.layers.linear(inputs_1, 5) 
    varscope.reuse_variables() 
    outputs_2 = tf.contrib.layers.linear(inputs_2, 5) 

但它给了我下面的结果

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-51-a40b9ec68e25> in <module>() 
     5  outputs_1 = tf.contrib.layers.linear(inputs_1, 5) 
     6  varscope.reuse_variables() 
----> 7  outputs_2 = tf.contrib.layers.linear(inputs_2, 5) 
... 
ValueError: Variable root/fully_connected_1/weights does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope? 

回答

4

问题是tf.contrib.layers.linear会自动创建一个新的设置有它自己的范围线性层。当调用scope.reuse()时,没有什么可以重用,因为这些是新变量。

尝试做这样的事情,而不是

def function(): 
    with tf.variable_scope("root") as varscope: 
    inputs = tf.constant(0.5, shape=[2, 3, 4]) 
    outputs = tf.contrib.layers.linear(inputs, 5) 
    return outputs 

result_1 = function() 
tf.get_variable_scope().reuse_variables() 
result_2 = function() 

sess = tf.InteractiveSession() 
sess.run(tf.initialize_all_variables()) 
a = sess.run(result_1) 
b = sess.run(result_2) 
np.all(a == b) # ==> True 
+0

我复制粘贴这段代码。它不起作用?与以前一样的错误。 –

+0

对不起,你是对的我有一个以前重用的错误应该是一个函数。我扩展它来展示一个例子。 – Steven

+0

很酷,现在可以使用!现在你的代码和OPs代码的区别究竟是什么,但是在'layers.linear'中增加'scope = varscope'?谢谢! –

0

你只需要修改linear(inputs_1, 5)linear(inputs_1, 5, scope="linear")

with tf.variable_scope("root") as varscope: 
    inputs_1 = tf.constant(0.5, shape=[2, 3, 4]) 
    inputs_2 = tf.constant(0.5, shape=[2, 3, 4]) 
    outputs_1 = tf.contrib.layers.linear(inputs_1, 5, scope="linear") 
    varscope.reuse_variables() 
    outputs_2 = tf.contrib.layers.linear(inputs_2, 5, scope="linear")