主管不会初始化参数成功

时间:2017-11-08 01:38:37

标签: tensorflow initialization supervisor

当我尝试使用tf.train.Supervisor初始化参数时,它会引发异常,其中net由tensorlayer构成。代码是这样的:

init_op = tf.global_variables_initializer()
saver = tf.train.Saver()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sv = tf.train.Supervisor(logdir=FLAGS.summary_dir, save_summaries_secs=0, saver=None)
with sv.managed_session(config=config) as sess:
       init_ = sess.run(init_op)
       net.print_params()
       net.print_layers()
        tl.layers.print_all_variables()

,例外是:

Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`
Traceback (most recent call last):
  File "/home/recsys/anaconda3/envs/tf1.2/lib/python3.6/site-packages/tensorlayer/layers.py", line 309, in print_params
    val = p.eval()
  File "/home/recsys/anaconda3/envs/tf1.2/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 463, in eval
    return self._variable.eval(session=session)
  File "/home/recsys/anaconda3/envs/tf1.2/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 606, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/home/recsys/anaconda3/envs/tf1.2/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3914, in _eval_using_default_session
    raise ValueError("Cannot evaluate tensor using `eval()`: No default "
ValueError: Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "MainEntryPtb.py", line 65, in <module>
    train_rnn(FLAGS)
  File "/home/recsys/wangjian/learntf/TFTemplate/examples/ptb.py", line 122, in train_rnn
    net.print_params()
  File "/home/recsys/anaconda3/envs/tf1.2/lib/python3.6/site-packages/tensorlayer/layers.py", line 313, in print_params
    raise Exception("Hint: print params details after tl.layers.initialize_global_variables(sess) or use network.print_params(False).")
Exception: Hint: print params details after tl.layers.initialize_global_variables(sess) or use network.print_params(False).

似乎当我运行初始化op init_ = sess.run(init_op)时,它实际上并不像参数初始化那样成功net.print_params()不会抛出异常。我还尝试sv = tf.train.Supervisor(logdir=FLAGS.summary_dir, save_summaries_secs=0, saver=None, init_op=tf.global_variables_initializer())来初始化参数,但它也失败了。

我使用tensorflow v1.2.1和python3.6

1 个答案:

答案 0 :(得分:0)

GitHub issue清楚地解释了这个问题。

这不是一个错误,也不是&#34;主管不会初始化参数成功&#34;。

您应该将您的sess更改为interactivesession或使用with sess.as_default,然后此问题就会得到解决。

如果你想知道为什么会出现这个问题,你应该参考张量层source code,在print_params的定义中有:

val = p.eval(session=session)

明确eval仅用于interactivesession或with sess.as_default