【问题标题】:tf how to restore two variables from the same variabletf 如何从同一个变量中恢复两个变量
【发布时间】:2020-02-12 03:33:04
【问题描述】:

我已经保存了一个模型,现在我尝试在两个分支中恢复它,如下所示:

我写了这段代码,它引发了ValueError: The same saveable will be restored with two names。 如何从同一个变量中恢复两个变量?

restore_variables = {}
for varr in tf.global_variables()
    if varr.op.name in checkpoint_variables:
        restore_variables[varr.op.name.split("_red")[0]] = varr           
        restore_variables[varr.op.name.split("_blue")[0]] = varr
init_saver = tf.train.Saver(restore_variables, max_to_keep=0)

【问题讨论】:

  • 你能分享创建和保存变量的代码吗?还有哪个TF版本?

标签: tensorflow restore


【解决方案1】:

在 TF 1.15 上测试

基本上,错误是说它在restore_variables dict 中找到了对同一变量的多个引用。修复很简单。使用tf.Variable(varr) 为您的变量创建一个副本,如下所示作为参考之一。

我认为可以安全地假设您在这里不是在寻找对同一个变量的多个引用,而是两个单独的变量。 (我假设这是因为,如果你想多次使用同一个变量,你可以多次使用单个变量)。

with tf.Session() as sess:
    saver.restore(sess, './vars/vars.ckpt-0')
    restore_variables = {}
    checkpoint_variables=['b']
    for varr in tf.global_variables():
        if varr.op.name in checkpoint_variables:
            restore_variables[varr.op.name.split("_red")[0]] = varr           
            restore_variables[varr.op.name.split("_blue")[0]] = tf.Variable(varr)
    print(restore_variables)
    init_saver = tf.train.Saver(restore_variables, max_to_keep=0)

您可以在下面找到使用玩具示例复制问题的完整代码。从本质上讲,我们有两个变量 ab,除此之外,我们正在创建 b_redb_blue 变量。

# Saving the variables

import tensorflow as tf
import numpy as np
a = tf.placeholder(shape=[None, 3], dtype=tf.float64)
w1 = tf.Variable(np.random.normal(size=[3,2]), name='a')
out = tf.matmul(a, w1)
w2 = tf.Variable(np.random.normal(size=[2,3]), name='b')
out = tf.matmul(out, w2)

saver = tf.train.Saver([w1, w2])

with tf.Session() as sess:
  tf.global_variables_initializer().run()
  saved_path = saver.save(sess, './vars/vars.ckpt', global_step=0)
# Restoring the variables

with tf.Session() as sess:
    saver.restore(sess, './vars/vars.ckpt-0')
    restore_variables = {}
    checkpoint_variables=['b']
    for varr in tf.global_variables():
        if varr.op.name in checkpoint_variables:
            restore_variables[varr.op.name+"_red"] = varr  
            # Fixing the issue: Instead of varr, do tf.Variable(varr)
            restore_variables[varr.op.name+"_blue"] = varr
    print(restore_variables)
    init_saver = tf.train.Saver(restore_variables, max_to_keep=0)

【讨论】:

    【解决方案2】:

    我可能没有正确理解这个问题,但你不能只制作两个保护对象吗?像这样的:

    import tensorflow as tf
    
    # Make checkpoint
    with tf.Graph().as_default(), tf.Session() as sess:
        a = tf.Variable([1., 2.], name='a')
        sess.run(a.initializer)
        b = tf.Variable([3., 4., 5.], name='b')
        sess.run(b.initializer)
        saver = tf.train.Saver([a, b])
        saver.save(sess, 'tmp/vars.ckpt')
    
    # Restore checkpoint
    with tf.Graph().as_default(), tf.Session() as sess:
        # Red
        a_red = tf.Variable([0., 0.], name='a_red')
        b_red = tf.Variable([0., 0., 0.], name='b_red')
        saver_red = tf.train.Saver({'a': a_red, 'b': b_red})
        saver_red.restore(sess, 'tmp1/vars.ckpt')
        print(a_red.eval())
        # [1. 2.]
        print(b_red.eval())
        # [3. 4. 5.]
    
        # Blue
        a_blue = tf.Variable([0., 0.], name='a_blue')
        b_blue = tf.Variable([0., 0., 0.], name='b_blue')
        saver_blue = tf.train.Saver({'a': a_blue, 'b': b_blue})
        saver_blue.restore(sess, 'tmp/vars.ckpt')
        print(a_blue.eval())
        # [1. 2.]
        print(b_blue.eval())
        # [3. 4. 5.]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-28
      • 2011-02-26
      • 1970-01-01
      • 2018-09-26
      • 2019-09-13
      • 2022-10-22
      • 2019-06-25
      • 1970-01-01
      相关资源
      最近更新 更多