TensorFlow: varscope.reuse_variables ()

How to reuse variables in TensorFlow? I want to reusetf.contrib.layers.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)
    varscope.reuse_variables()
    outputs_2 = tf.contrib.layers.linear(inputs_2, 5)

But this gives me the following result:

---------------------------------------------------------------------------
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
source share
2 answers

The problem is that tf.contrib.layers.linear automatically creates a new set of line layers with its own area. When calling scope.reuse () there is nothing to reuse there, because these are new variables.

Try to do something like this

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
+4
source

you just need to change linear(inputs_1, 5)tolinear(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")
0
source

Source: https://habr.com/ru/post/1660439/


All Articles