gpt4 book ai didi

python - 在tensorflow中初始化变量、变量范围和import_graph_def

转载 作者:行者123 更新时间:2023-11-30 08:34:02 26 4
gpt4 key购买 nike

在尝试使用 import_graph_def 进行图形手术时,我有许多关于 tensorflow 行为的相关问题。 2 different graph surgeries

在上图中,我用粗体红色箭头表示 2 个不同的图形手术。左边有 2 个图,g1 和 g2,手术包括用图 g1 中的节点(及其下面的所有内容)替换图 g2 中的节点。 this post 中解释了如何做到这一点。右侧的手术涉及替换属于同一图的节点,我无法弄清楚如何执行,或者即使它完全可能。我最终得到了这个最小的例子

with tf.Graph().as_default() as g1:
with tf.variable_scope('foo', reuse=tf.AUTO_REUSE):
x = tf.placeholder(dtype=tf.float64, shape=[2], name='x')
c = tf.get_variable('c', initializer=tf.cast(1.0, tf.float64))
y = tf.identity(2*x, 'y')

z = tf.identity(3*x*c, 'z')

g1_def = g1.as_graph_def()
z1, = tf.import_graph_def(g1_def, input_map={'foo/x:0' : y}, return_elements=["foo/z:0"],
name='z1')
init_op = tf.global_variables_initializer()
print(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='foo'))


with tf.Session(graph=g1) as sess:
sess.run(init_op)
print(sess.run(z, feed_dict={'foo/x:0' : np.array([1.0, 2.0])}) )
print(sess.run(tf.report_uninitialized_variables()))
# z1 = sess.run(z1, feed_dict={'foo/x:0' : np.array([1.0, 2.0])})

此代码按原样运行。 3次打印产量分别为:

[<tf.Variable 'foo/c:0' shape=() dtype=float64_ref>]
[ 3. 6.]
[]

特别是,最后一次打印通知没有未初始化的变量。但是,取消注释最后一行会产生错误

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value foo/z1/foo/c

请注意,如果我从上面的 z 定义中删除 c,这可以工作。但是,我想了解这个错误。首先,为什么变量报告为 foo/z1/foo/c?为什么范围 foo 出现两次?为什么打印未初始化的变量时没有报告任何内容?当我在 foo 范围下打印 GLOBAL_VARIABLES 集合时,为什么只报告 foo/c?

PS:我想有一个更简单的方法来问这个问题,即 tensorflow 的类似物是什么

theano.clone(some_tensor, replace={input_var : replace_var})

最佳答案

To begin with, why is the variable reported as foo/z1/foo/c? Why does the scope foo appear twice?

调用tf.import_graph_def(...)后,图表被复制。第一个图在 foo Score 中定义。第二个子图已在 foo/z1 范围内导入(因为 name='z1',加上 foo 是从上面的范围中保留的) 。因此图 g1 现在包含以下张量:

foo/x
foo/y
foo/c
...
foo/z1/foo/x
foo/z1/foo/y
foo/z1/foo/c
...

第一个 foo/c 已初始化,但第二个 foo/z1/foo/c 未初始化(见下文)。

Why is nothing reported when I print the uninitialized variables? Why is only foo/c reported when I print the GLOBAL_VARIABLES collection under the scope foo?

由于report_uninitialized_variables()默认扫描LOCAL_VARIABLESGLOBAL_VARIABLES,所以这基本上是同一个问题。

可能是一个错误:GLOBAL_VARIABLES集合在tf.import_graph_def调用后没有更新。我说可能是因为 GLOBAL_VARIABLES 被设计为纯粹的便利集合。 Tensorflow 尝试使其保持最新状态,但可能并不能保证它始终具有所有变量。事实是tf.add_to_collection存在公开支持这一想法——如果他们想要的话,人们可以为任何收藏添加任何值(value)。底线:此行为在未来版本中可能会或可能不会改变,但从 1.5 开始,客户端负责在图形导入后更新全局变量。

In particular, the last print informs that there are no unintialized variables. However, uncommenting the last line, yields the error

要修复此错误,您只需运行 z1 子图的初始化程序即可。像这样:

# note that it's defined before `g1.as_graph_def()` to be a part of graph def
init_op = tf.global_variables_initializer()

g1_def = g1.as_graph_def()
z1, = tf.import_graph_def(g1_def, input_map={'foo/x:0': y}, return_elements=["foo/z:0"],
name='z1')

# find the init op
z1_init_op = tf.get_default_graph().get_operation_by_name('foo/z1/foo/init')

...

sess.run(z1_init_op)

瞧!您拥有重复的图表,就像您想要的那样。

关于python - 在tensorflow中初始化变量、变量范围和import_graph_def,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48715866/

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