Skip to content

Instantly share code, notes, and snippets.

@InnerPeace-Wu
Created October 4, 2017 07:34
Show Gist options
  • Save InnerPeace-Wu/e32679fd633b6d43d6b4f3a988d22fd8 to your computer and use it in GitHub Desktop.
Save InnerPeace-Wu/e32679fd633b6d43d6b4f3a988d22fd8 to your computer and use it in GitHub Desktop.
set default settings with tf.variable_scope and ways to reuse variables.
import tensorflow as tf
#with default settings
def scope_defsetting():
with tf.variable_scope('qa', initializer=tf.zeros_initializer()):
#tf.get_variable_scope().reuse_variables()
# if without setting initializer in get_varaible, use the default one in the scope.
a = tf.get_variable('a', [2,4], tf.float32)
#tf.get_variable_scope().reuse == True
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(a))
# reuse variables 1
def scope_reuse():
with tf.variable_scope('qa'):
tf.get_variable_scope().reuse_variables()
#codes ....
with tf.variable_scope('qa', reuse = True):
#codes ....
pass
with tf.variable_scope('qa') as scope:
scope.reuse_variables()
#codes ....
if __name == '__main__':
# 1
scope_defsetting()
'''out:
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment