加載幾個相同的模型爲一個會議在我主要的代碼中,我創建一個基於配置文件中像這樣如何從保存文件Tensorflow
with tf.variable_scope('MODEL') as topscope:
model = create_model(config_file)#returns input node, output node, and some other placeholders
這個範圍的名稱模型是一樣的所有撲救。
然後,我定義了一個優化和成本函數等。(他們是這個範圍之外)
然後我創建了一個保護和保存:
saver = tf.train.Saver(max_to_keep=10)
saver.save(sess, 'unique_name', global_step=t)
現在我已經創建和保存10個不同的車型,我想在一次加載它們都像這樣也許:
models = []
for config, save_path in zip(configs, save_paths):
models.append(load_model(config, save_path))
,並能夠運行它們,並比較其結果,將它們混合,平均等我不需要優化插槽變量爲這些加載的模型。我只需要那些在'MODEL'範圍內的變量。
我是否需要創建多個會話?
我該怎麼辦?我不知道從哪裏開始。我可以從我的配置文件中創建一個模型,然後使用相同的配置文件加載此相同的模型和保存這樣的:
saver.restore(sess, save_path)
但我怎麼加載一個以上?
編輯:我不知道這個詞。我想製作一個整體的網絡。 問題,要求它,還是不回答:How to create ensemble in tensorflow?
編輯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)
然後我運行這段代碼創建相同的模型,加載其檢查站保存和重命名變量:
#RENAMING PART, different file
#create the same model as above here
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))
#assuming that we change top scope, not something in the middle, functionality can be added without much trouble I think
#not sure why I need to remove ':0' part, but it seems to work okay
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)
則T O此模型加載到含有額外變量的文件,並用一個新名稱:
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):
#scope is a top scope here, otherwise change startswith part
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')#no need to save this
return y
你能回答我在這裏的類似問題:https://stackoverflow.com/questions/47690257/exporting-meta-graph-from-a-checkpoint-file-tensorflow – Amir