TensorFlow supervisor 阻止变量赋值:Graph 已完成且无法修改 [英] TensorFlow supervisor prevents variable assignment: Graph is finalized and cannot be modified

查看:26
本文介绍了TensorFlow supervisor 阻止变量赋值:Graph 已完成且无法修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码工作正常:

import tensorflow as tf
x = tf.Variable(initial_value=0)

with tf.Session() as session:
    print session.run(x.assign(1))

但是这段代码失败了:

import tensorflow as tf
x = tf.Variable(initial_value=0)
supervisor = tf.train.Supervisor(logdir="/tmp")

with tf.Session() as session:
    print session.run(x.assign(1))

唯一的区别是 tf.train.Supervisor 的实例化.请注意,我们甚至不使用主管来创建托管会话.

The only difference is the instantiation of a tf.train.Supervisor. Note that we don't even use the supervisor to create a managed session.

错误是:

python tf_supervisor_freeze.py
Traceback (most recent call last):
  File "tf_supervisor_freeze.py", line 6, in <module>
    print session.run(x.assign(1))
  File "<virtual_env_path>/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 522, in assign
    return state_ops.assign(self._variable, value, use_locking=use_locking)
  File "<virtual_env_path>/lib/python2.7/site-packages/tensorflow/python/ops/gen_state_ops.py", line 47, in assign
    use_locking=use_locking, name=name)
  File "<virtual_env_path>/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 491, in apply_op
    preferred_dtype=default_dtype)
  File "<virtual_env_path>/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 702, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "<virtual_env_path>/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 110, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "<virtual_env_path>/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 103, in constant
    attrs={"value": tensor_value, "dtype": dtype_value}, name=name).outputs[0]
  File "<virtual_env_path>/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2286, in create_op
    self._check_not_finalized()
  File "<virtual_env_path>/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2009, in _check_not_finalized
    raise RuntimeError("Graph is finalized and cannot be modified.")
RuntimeError: Graph is finalized and cannot be modified.

Process finished with exit code 1

如果 tf.train.Supervisor(logdir="/tmp", summary_op=None, saver=None) 用于禁用某些主管的服务,错误仍然存​​在.

Error remains if tf.train.Supervisor(logdir="/tmp", summary_op=None, saver=None) is used to disable some of the supervisor's services.

这个问题是由其他人在 Github 上提出的,但那里没有提供任何答案;请求是在 StackOverflow 上提出这个问题.唯一相关的 StackOverflow 问题 似乎没有解决这个特定问题案例.

This problem was raised by somebody else on Github but no answer was provided there; the request was to raise the issue on StackOverflow instead. The only relevant StackOverflow question does not appear to address this specific case.

推荐答案

正如错误所说,图定稿后不能修改图.

Just as the error says, you cannot modify the graph when the graph is finalized.

RuntimeError("图形已完成,无法修改.")

RuntimeError("Graph is finalized and cannot be modified.")

当执行这段代码tf.train.Supervisor()时,图形将被最终确定.您可以在 1.0 版的 tensorflow 中使用路径/tensorflow/python/training/supervisor.py 检查它的代码.

When execute this code tf.train.Supervisor(), the graph will be finalized. You can check code of it with path /tensorflow/python/training/supervisor.py in tensorflow of version 1.0.

你会发现:

# The graph is not allowed to change anymore.
graph.finalize()

所以你不能在Supervisor()之后修改图表.

So you cannot modify the graph after Supervisor().

这篇关于TensorFlow supervisor 阻止变量赋值:Graph 已完成且无法修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆