g1 = tf.Graph() with g1.as_default(): v = tf.get_variable("v", initializer=tf.zeros_initializer(shape=[1])) g2 = tf.Graph() with g2.as_default(): v = tf.get_variable("v", initializer=tf.ones_initializer(shape=[1])) with tf.Session(graph = g1) as sess: tf.initialize_all_variables().run() with tf.variable_scope("", reuse = True): print(sess.run(tf.get_variable("v"))) with tf.Session(graph = g2) as sess: tf.initialize_all_variables().run() with tf.variable_scope("", reuse = True): print(sess.run(tf.get_variable("v")))
TypeError: __init__() got an unexpected keyword argument 'shape'
g1 = tf.Graph() with g1.as_default(): v = tf.get_variable("v", initializer=tf.zeros_initializer()(shape=[1]))
g2 = tf.Graph() with g2.as_default(): v = tf.get_variable("v", initializer=tf.zeros_initializer()(shape=[1]))
with tf.Session(graph = g1) as sess: tf.initialize_all_variables().run() with tf.variable_scope("", reuse = True): print(sess.run(tf.get_variable("v"))) with tf.Session(graph = g2) as sess: tf.initialize_all_variables().run() with tf.variable_scope("", reuse = True): print(sess.run(tf.get_variable("v")))
这里抛出了一个警告
1 2 3
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/python/util/tf_should_use.py:118: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02. Instructions for updating: Use `tf.global_variables_initializer` instead.
替换掉initialize_all_variables即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
g1 = tf.Graph() with g1.as_default(): v = tf.get_variable("v", initializer=tf.zeros_initializer()(shape=[1]))
g2 = tf.Graph() with g2.as_default(): v = tf.get_variable("v", initializer=tf.zeros_initializer()(shape=[1]))
with tf.Session(graph = g1) as sess: tf.global_variables_initializer().run() with tf.variable_scope("", reuse = True): print(sess.run(tf.get_variable("v"))) with tf.Session(graph = g2) as sess: tf.global_variables_initializer().run() with tf.variable_scope("", reuse = True): print(sess.run(tf.get_variable("v")))