gpt4 book ai didi

python - TensorFlow 主管阻止变量分配 : Graph is finalized and cannot be modified

转载 作者:太空狗 更新时间:2023-10-30 02:08:27 25 4
gpt4 key购买 nike

这段代码工作正常:

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 的实例化。请注意,我们甚至不使用主管来创建托管 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) 禁用某些主管的服务,错误仍然存​​在。

此问题是由其他人在 Github 上提出的但那里没有答案;该请求是改为在 StackOverflow 上提出问题。唯一相关的StackOverflow question似乎没有解决这个具体案例。

最佳答案

正如报错所说,图定型后不能修改图。

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

当执行此代码 tf.train.Supervisor() 时,图表将最终确定。你可以在tensorflow 1.0版本的路径/tensorflow/python/training/supervisor.py中查看它的代码。

你会发现:

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

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

关于python - TensorFlow 主管阻止变量分配 : Graph is finalized and cannot be modified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43324923/

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