gpt4 book ai didi

tensorflow - session 恢复后 get_variable() 不起作用

转载 作者:行者123 更新时间:2023-12-03 19:59:21 24 4
gpt4 key购买 nike

我尝试恢复 session 并调用 get_variable()获取类型的对象
tf.Variable(根据 this answer )。
它无法找到变量。重现该案例的最小示例是
如下。

首先,创建一个变量并保存 session 。

import tensorflow as tf

var = tf.Variable(101)

with tf.Session() as sess:
with tf.variable_scope(''):
scoped_var = tf.get_variable('scoped_var', [])

with tf.variable_scope('', reuse=True):
new_scoped_var = tf.get_variable('scoped_var', [])

assert scoped_var is new_scoped_var
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
print(sess.run(scoped_var))
saver.save(sess, 'data/sess')

这里 get_variables在具有 reuse=True 的范围内工作正常。
然后,从文件中恢复 session 并尝试获取变量。
import tensorflow as tf

with tf.Session() as sess:
saver = tf.train.import_meta_graph('data/sess.meta')
saver.restore(sess, 'data/sess')

for v in tf.get_collection('variables'):
print(v.name)

print(tf.get_collection(("__variable_store",)))
# Oops, it's empty!

with tf.variable_scope('', reuse=True):
# the next line fails
new_scoped_var = tf.get_variable('scoped_var', [])

print("new_scoped_var: ", new_scoped_var)

输出:
Variable:0
scoped_var:0
[]
Traceback (most recent call last):
...
ValueError: Variable scoped_var does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?

正如我们所见, get_variable()找不到变量。和 ("__variable_store",)集合,由 get_variable() 内部使用,
是空的。

为什么 get_variable失败?

最佳答案

您可以试试这个,而不是处理元图(如果您想修改图以及它的加载方式等,这可能会很有帮助)。

import tensorflow as tf

with tf.Session() as sess:
with tf.variable_scope(''):
scoped_var = tf.get_variable('scoped_var', [])

with tf.variable_scope('', reuse=True):
new_scoped_var = tf.get_variable('scoped_var', [])

assert scoped_var is new_scoped_var
saver = tf.train.Saver()
path = tf.train.get_checkpoint_state('data/sess')
if path is not None:
saver.restore(sess, path.model_checkpoint_path)
else:
sess.run(tf.global_variables_initializer())

print(sess.run(scoped_var))
saver.save(sess, 'data/sess')

#now continue to use as you normally would with a restored model

主要区别是您在调用 saver.restore 之前设置了模型

关于tensorflow - session 恢复后 get_variable() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42769435/

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