2016-04-24 24 views
3

加載幾個相同的模型爲一個會議在我主要的代碼中,我創建一個基於配置文件中像這樣如何從保存文件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 

回答

2

在概念水平:

  • 有兩個獨立的東西:圖形和會話
  • 圖形首先被創建。它定義了你的模型。沒有理由不能在一張圖中存儲多個模型。沒關係。它還定義了變量,但它實際上犯規包含它們的狀態
  • 一個會話
    • 它是從圖
    • ,只要你喜歡,你可以創建許多會話創建圖後創建從圖中
    • 其持有圖中的不同變量的狀態,即權重的各種型號

所以:

  • 當您加載只是模型定義,所有你需要的是:一個或多個圖形。一張圖就足夠了
  • 當你加載模型的實際權重,學習權重/參數時,你需要爲圖創建一個會話。一個會話就足夠了

請注意,變量都有名稱,並且它們需要是唯一的。你可以給他們唯一的名稱,在圖中,通過使用可變範圍,如:

with tf.variable_scope("some_scope_name"): 
    # created model nodes here... 

這將組的節點連接在一起很好地在Tensorboard圖。好的,重新讀一下你的問題。看起來您想要一次保存/加載單個模型。

保存/加載模型的參數/權重發生在會話中,該會話包含圖中定義的每個變量的權重/參數。

您可以參考這些變量的名稱,例如,通過上面創建的範圍,並且只保存這些變量的一個子集,放到不同的文件,等

順便說一句,它也可以使用session.run(...)獲得重量/參數的值,作爲numpy張量,然後你可以醃製,或者其他,如果你選擇的話。

+0

你能回答我在這裏的類似問題:https://stackoverflow.com/questions/47690257/exporting-meta-graph-from-a-checkpoint-file-tensorflow – Amir