deep learning - TensorFlow: varscope.reuse_variables() -
how reuse variables in tensorflow? want reuse tf.contrib.layers.linear
with tf.variable_scope("root") 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 gives me 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 not exist, or not created tf.get_variable(). did mean set reuse=none in varscope?
the problem tf.contrib.layers.linear automatically creates new set of linear layers own scope. when calling scope.reuse() there's nothing reused because new variables.
try instead
def function(): tf.variable_scope("root") 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()) = sess.run(result_1) b = sess.run(result_2) np.all(a == b) # ==> true
Comments
Post a Comment