Tensorflow 如何重置tf.estimator.estimator参数?

Tensorflow 如何重置tf.estimator.estimator参数?,tensorflow,Tensorflow,我尝试了tf.Graph(),但无法通过new重置变量。代码如下: with tf.Graph().as_default() as g: clf_ = tf.estimator.Estimator(model_fn=my_w2d.model_fn_wide2deep, params=param, model_dir="/Users/zhouliaoming/data/credit_dnn/model_retrain/rm_gene_v2_sall/") with tf.name_

我尝试了tf.Graph(),但无法通过new重置变量。代码如下:

with tf.Graph().as_default() as g:
    clf_ = tf.estimator.Estimator(model_fn=my_w2d.model_fn_wide2deep, params=param, model_dir="/Users/zhouliaoming/data/credit_dnn/model_retrain/rm_gene_v2_sall/")
    with tf.name_scope("rewrite"):
        clf2 = tf.estimator.Estimator(model_fn=my_w2d.model_fn_wide2deep, params=param, model_dir="/Users/zhouliaoming/data/credit_dnn/model_retrain/genev2_s0/")
    out_bias = tf.get_variable("output_0/bias")
    out_b_rew = tf.get_variable("rewrite/output_0/bias")
    vars_ = clf_.get_variable_names()   ## only has clf_.get_variable_values()
    print("vars: %r\n output_0/bias: %r\ntrain-vars: %r" % (vars_, clf_.get_variable_value('output_0/bias'), tf.contrib.framework.get_trainable_variables()))
    print("before rewrite: out_bias: %r, out_b_rew: %r" % (out_bias.eval(), out_b_rew.eval()))
    out_b_rew.assing(out_bias)
    print("after rewrite: out_bias: %r, out_b_rew: %r" % (out_bias.eval(), out_b_rew.eval()))
它只返回一个错误:

Traceback (most recent call last):
  File "tf_utils.py", line 31, in <module>
    out_bias = tf.get_variable("output_0/bias")
  File "/Users/zhouliaoming/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 1262, in get_variable
    constraint=constraint)
  File "/Users/zhouliaoming/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 1097, in get_variable
    constraint=constraint)
  File "/Users/zhouliaoming/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 435, in get_variable
    constraint=constraint)
  File "/Users/zhouliaoming/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 404, in _true_getter
    use_resource=use_resource, constraint=constraint)
  File "/Users/zhouliaoming/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 764, in _get_single_variable
    "but instead was %s." % (name, shape))
ValueError: Shape of a new variable (output_0/bias) must be fully defined, but instead was <unknown>.
回溯(最近一次呼叫最后一次):
文件“tf_utils.py”,第31行,在
out\u bias=tf.get\u变量(“输出\u 0/bias”)
文件“/Users/zhouliaming/anaconda3/envs/tensorflow/lib/python3.6/site packages/tensorflow/python/ops/variable_scope.py”,第1262行,在get_variable中
约束=约束)
文件“/Users/zhouliaming/anaconda3/envs/tensorflow/lib/python3.6/site packages/tensorflow/python/ops/variable_scope.py”,第1097行,在get_variable中
约束=约束)
文件“/Users/zhouliaming/anaconda3/envs/tensorflow/lib/python3.6/site packages/tensorflow/python/ops/variable_scope.py”,第435行,在get_variable中
约束=约束)
文件“/Users/zhouliaming/anaconda3/envs/tensorflow/lib/python3.6/site packages/tensorflow/python/ops/variable\u scope.py”,第404行,在“true\u getter”中
使用资源=使用资源,约束=约束)
文件“/Users/zhouliaming/anaconda3/envs/tensorflow/lib/python3.6/site packages/tensorflow/python/ops/variable\u scope.py”,第764行,在“get\u single\u variable”中
“而是%s.”%(名称、形状))
ValueError:必须完全定义新变量(输出0/偏差)的形状,但实际上是。
===================旧信息剖切线=========

我定义了一个tf.estimator.estimator模型a,由model_fn handler定义。 我想用和ckpt文件相同的旧模型参数更改模型A的参数。 我试图得到模型A的图,然后在图中得到参数的变量,然后用我的旧模型的参数赋值。 希望得到一些建议!
非常感谢

有很多方法可以做到这一点,具体取决于您有什么可用的方法。例如,如果两个模型都有代码和检查点,则可以创建两个单独的图(
,tf.Graph()为g
)将两个检查点加载到其中,从一个图中读取变量值并将其分配给另一个图中的变量


如果您确切地知道要在一个检查点中读取的变量,您可以只还原它(
Saver.restore
获取要还原的变量列表),或者您可以使用诸如
CheckpointReader

之类的工具来读取它,因为您的问题没有太多细节,所以我写了一个通用答案。如果你想要更具体的建议,我建议你先尝试一些方法。如果你在某件事上遇到困难,可以问这个具体的问题。非常感谢!我尝试了tf.Graph(),但它不起作用。我的代码显示是否做了一些更改?希望你能给我一些建议。我也想尝试SessionRunHook,但没有找到合适的代码。对不起,我不理解你的评论。你问题中的错误应该是很有描述性的。您正在使用与任何现有变量不匹配的名称调用
get_variable()
。因此,它试图创建一个新的。为此,它需要知道变量的形状,但您没有指定它。谢谢@iga给我的建议。那些日子我尝试了SessionRunHook,但它有一些错误。我可以问你这个问题吗?再次感谢。我在那里贴了答案。希望能有帮助。另外,当有人回答你的问题时,最好接受它。