4
我有兩個型號分別訓練m1
和m2
。現在我想保留m1
固定並根據m2
的輸出微調m1
。 m1
的所有變量都在變量範圍"m1/"
之下,而m2
的變量在"m2/"
之下。這基本上就是我所做的:如何在Tensorflow中使用多個型號
# build m1 and m2
with tf.device("/cpu:0"):
m1.build_graph()
m2.build_graph()
# indicate the variables of m1 and m2
allvars = tf.global_variables()
m1_vars = [v for v in allvars if v.name.startswith('m1')]
m2_vars = [v for v in allvars if v.name.startswith('m2')]
# construct the saver
m1_saver = tf.train.Saver(m1_vars)
m2_saver = tf.train.Saver(m2_vars)
# Load m2 variables
m2_ckpt_state = tf.train.get_checkpoint_state(FLAGS.m2_log_root)
m2_sess = tf.Session()
m2_saver.restore(m2_sess, m2_ckpt_state.model_checkpoint_path)
# construct a train supervisor for m1
m1_sv = tf.train.Supervisor(is_chief=True, saver=m1_saver)
# construct a session for m1
m1_sess = m1_sv.prepare_or_wait_for_session()
...
但現在存在的最後一行代碼的錯誤:
Traceback (most recent call last):
File "run_summarization.py", line 407, in <module>
tf.app.run()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "run_summarization.py", line 401, in main run_fine_tune(model, ranker, batcher, vocab)
File "run_summarization.py", line 232, in run_fine_tune sess_context_manager = sv.prepare_or_wait_for_session(config=config)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/supervisor.py", line 719, in prepare_or_wait_for_session
init_feed_dict=self._init_feed_dict, init_fn=self._init_fn)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/session_manager.py", line 280, in prepare_session
self._local_init_op, msg))
RuntimeError: Init operations did not make model ready. Init op: init,
init fn: None, local_init_op: name: "group_deps"
op: "NoOp"
input: "^init_1"
input: "^init_all_tables", error: Variables not initialized: m2/var1, m2/var2, m2/var3...
你能告訴我爲什麼這個錯誤發生時,我該如何解決?提前致謝!
謝謝你的回答!我會盡快回復你! – southdoor
看起來很有希望。但是現在我對這種方法有些困惑。如果我想創建兩個圖表,用於相同模型的訓練和推理模式,「m1」。我該怎麼做才能讓他們分享變數?似乎不同的圖將保持不同的一組變量。 Thx – southdoor
推理和訓練可以使用相同的圖形完成。您在定義變量時只需要使用「重用」術語。如果你的圖形有'dropout' /'batch_norm',你可以使用另一個參數到你的模型構建函數'is_training'。基於'is_training',您可以重新使用所有現有變量進行推理,而無需創建用於推理的新圖形。同樣適用於禁用dropout以推斷併爲batch_norm返回'moving_mean'和'moving_var'。 –