gpt4 book ai didi

python - 如何在 Tensorflow 中使用多个模型

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:53 24 4
gpt4 key购买 nike

我有两个模型 m1m2 分别训练。现在我想保持 m1 不变,并根据 m2 的输出微调 m1m1 的所有变量都在变量作用域"m1/" 下,m2 的变量在"m2/"。这基本上是我所做的:

# 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...

能否请您告诉我为什么会出现此错误以及如何解决?提前致谢!

最佳答案

对单独的模型使用单独的图表;在这种情况下,supervisor 是用 m1_vars 定义的,但它与 m2_vars 也驻留的默认图形一起工作,当尝试初始化 m2_vars 时,它会导致问题。因为 m2_vars 是用另一个 session 初始化的。

function build_graph() should be defined as
gi = tf.Graph()
with gi.as_default():
...
rest of the code
return gi
with tf.device("/cpu:0"):
g1 = m1.build_graph()
g2 = m2.build_graph()

...
m2_sess = tf.Session(graph=g2)
...
init_op = tf.variables_initializer(m2_vars)
m1_sv = tf.train.Supervisor(graph=g1, is_chief=True, init_op=init_op, saver=m1_saver)

关于python - 如何在 Tensorflow 中使用多个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45500378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com