In my main code, I create a model based on a configuration file like this
with tf.variable_scope('MODEL') as topscope:
model = create_model(config_file)
The name of this area is the same for all saves.
Then I define the optimizer and cost function, etc. (they are outside this area)
Then I create a splash screen and save it:
saver = tf.train.Saver(max_to_keep=10)
saver.save(sess, 'unique_name', global_step=t)
Now I have created and saved 10 different models, and I want to download them immediately, as possible:
models = []
for config, save_path in zip(configs, save_paths):
models.append(load_model(config, save_path))
and be able to run them and compare their results, mix them, averages, etc. I do not need optimizer slot variables for these loaded models. I need only those variables that are inside the "MODEL" area.
Do I need to create multiple sessions?
? , . , :
saver.restore(sess, save_path)
?
: . .
, : ?
2: , :
, , :
import tensorflow as tf
from util import *
OLD_SCOPE_NAME = 'scope1'
sess = tf.Session()
with tf.variable_scope(OLD_SCOPE_NAME) as topscope:
model = create_model(tf, 6.0, 7.0)
sc_vars = get_all_variables_from_top_scope(tf, topscope)
print([v.name for v in sc_vars])
sess.run(tf.initialize_all_variables())
print(sess.run(model))
saver = tf.train.Saver()
saver.save(sess, OLD_SCOPE_NAME)
, , :
import tensorflow as tf
from util import *
OLD_SCOPE_NAME = 'scope1'
NEW_SCOPE_NAME = 'scope2'
sess = tf.Session()
with tf.variable_scope(OLD_SCOPE_NAME) as topscope:
model = create_model(tf, 6.0, 7.0)
sc_vars = get_all_variables_from_top_scope(tf, topscope)
print([v.name for v in sc_vars])
saver = tf.train.Saver()
saver.restore(sess, OLD_SCOPE_NAME)
print(sess.run(model))
print([NEW_SCOPE_NAME + v.name[len(OLD_SCOPE_NAME):v.name.rfind(':')] for v in sc_vars])
new_saver = tf.train.Saver(var_list={NEW_SCOPE_NAME + v.name[len(OLD_SCOPE_NAME):v.name.rfind(':')]:v for v in sc_vars})
new_saver.save(sess, NEW_SCOPE_NAME)
, :
import tensorflow as tf
from util import *
NEW_SCOPE_NAME = 'scope2'
sess = tf.Session()
with tf.variable_scope(NEW_SCOPE_NAME) as topscope:
model = create_model(tf, 5.0, 4.0)
sc_vars = get_all_variables_from_top_scope(tf, topscope)
q = tf.Variable(tf.constant(0.0, shape=[1]), name='q')
print([v.name for v in sc_vars])
saver = tf.train.Saver(var_list=sc_vars)
saver.restore(sess, NEW_SCOPE_NAME)
print(sess.run(model))
util.py:
def get_all_variables_from_top_scope(tf, scope):
return [v for v in tf.all_variables() if v.name.startswith(scope.name)]
def create_model(tf, param1, param2):
w = tf.get_variable('W', shape=[1], initializer=tf.constant_initializer(param1))
b = tf.get_variable('b', shape=[1], initializer=tf.constant_initializer(param2))
y = tf.mul(w, b, name='mul_op')
return y