簡體   English   中英

如何恢復多個模型並在張量流中平均?

[英]How to restore multiple models and average them in tensorflow?

我已經分別訓練了兩個模型,我想加載它們的變量並取平均值。 但是tf.get_default graph()出錯

這是我的代碼結構(我知道這是錯誤的,但是如何正確編寫?)

sess = tf.session()
saver_one = tf.train.import_meta_graph('./model1.ckpt.meta')
saver_one.restore(sess,'./model1.ckpt')
graph_one = tf.get_default_graph()
wc1 = graph_one.get_tensor_by_name('wc1:0')
……
saver_two = tf.train.import_meta_graph('./model2.ckpt.meta')
saver_two.restore(sess,'./model2.ckpt')
graph_two = tf.get_default_graph()
wc1_two = graph_two.get_tensor_by_name('wc1:0')
……

錯誤是:

 Traceback (most recent call last): File "/home/dan/Documents/deep-prior-master/src/ESB_ICVL_TEST_ALL.py", line 143, in <module> saver_two.restore(sess,'./cache/cnn_shallow/model2.ckpt') File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1548, in restore {self.saver_def.filename_tensor_name: save_path}) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 789, in run run_metadata_ptr) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 997, in _run feed_dict_string, options, run_metadata) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1132, in _do_run target_list, options, run_metadata) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1152, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [27] rhs shape= [9] [[Node: save/Assign_6 = Assign[T=DT_FLOAT, _class=["loc:@outb"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/gpu:0"](outb, save/RestoreV2_6/_1)]] 

非常感謝您給我任何建議。 =(^。^)=

您試圖覆蓋圖形,但會遇到不匹配的情況(某些尺寸不匹配)。 將它們分開可能更好。

graph_one = tf.Graph()
with graph_one.as_default():
  session_one = tf.Session()
  with session_one.as_default():
    saver_one = tf.train.import_meta_graph('./model1.ckpt.meta')
    wc1_one_value = session_one.run([graph_one.get_tensor_by_name('wc1:0')])

# Similar for graph_two
...

print (wc1_one_value + wc1_two_value) / 2  # Or whatever you want

要將它們分配回會話中,請構造圖形,然后執行tf.assign操作。

with graph_one.as_default(), session_one.as_default():
   session_one.run([tf.assign(<variable>, (wc1_one_value + wc1_two_value) / 2 )])

要獲取變量,您可以使用get_trainable_variables或使用get_trainable_variables reuse=True再次定義它。 然后再次導出模型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM