Tensorflow可以';t从检查点还原全局_步骤
我似乎无法从保存的检查点检索Tensorflow可以';t从检查点还原全局_步骤,tensorflow,Tensorflow,我似乎无法从保存的检查点检索全局\u步骤。我的代码: //(...) checkpoint_file = tf.train.latest_checkpoint(checkpoint_dir) saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file), clear_devices=True) saver.restore(sess, checkpoint_file) for v in tf.global_variable
全局\u步骤
。我的代码:
//(...)
checkpoint_file = tf.train.latest_checkpoint(checkpoint_dir)
saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file), clear_devices=True)
saver.restore(sess, checkpoint_file)
for v in tf.global_variables():
print(v)
test = tf.get_variable("global_step")
print(test)
结果:
//(...)
Tensor("global_step/read:0", shape=(), dtype=int32)
//(...)
Traceback (most recent call last):
File "train.py", line XXX, in <module>
test = tf.get_variable("global_step")
File "(...)/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 988, in get_variable
custom_getter=custom_getter)
File "(...)/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 890, in get_variable
custom_getter=custom_getter)
File "(...)/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 348, in get_variable
validate_shape=validate_shape)
File "(...)/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 333, in _true_getter
caching_device=caching_device, validate_shape=validate_shape)
File "(...)/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 660, in _get_single_variable
"but instead was +1ms." % (name, shape))
ValueError: Shape of a new variable (global_step) must be fully defined, but instead was <unknown>.
/(…)
张量(“全局步进/读取:0”,形状=(),数据类型=int32)
//(...)
回溯(最近一次呼叫最后一次):
文件“train.py”,第XXX行,在
test=tf.get_变量(“全局_步”)
文件“(…)/python3.6/site packages/tensorflow/python/ops/variable_scope.py”,第988行,在get_变量中
custom\u getter=custom\u getter)
文件“(…)/python3.6/site packages/tensorflow/python/ops/variable_scope.py”,第890行,在get_变量中
custom\u getter=custom\u getter)
文件“(…)/python3.6/site packages/tensorflow/python/ops/variable_scope.py”,第348行,在get_变量中
验证形状=验证形状)
文件“(…)/python3.6/site packages/tensorflow/python/ops/variable_scope.py”,第333行,在_true_getter中
缓存\设备=缓存\设备,验证\形状=验证\形状)
文件“(…)/python3.6/site packages/tensorflow/python/ops/variable_scope.py”,第660行,位于单变量中
“但实际上是+1ms。”%(名称、形状))
ValueError:必须完全定义新变量(全局_步骤)的形状,但实际上是。
我也尝试过global\u step:0
和global\u step/read:0
,但结果相同。有什么建议吗?或者我不应该使用tf.get_变量
谢谢如果一个现有变量是使用
tf.get\u variable
创建的,那么您只能使用tf.get\u variable
来检索该变量。此外,必须适当设置变量范围。在这里,它似乎试图创建一个名为'global\u step'
的新变量,表明它还不存在。有关如何使用tf.get\u变量的详细信息
我通常会这样处理全局步骤:
# to create
global_step = tf.Variable(tf.constant(0), trainable=False, name='global_step')
tf.add_to_collection('global_step', global_step)
# to load
global_step = tf.get_collection_ref('global_step')[0]
# get the current value
gs = sess.run(global_step)
with tf.Session() as sess:
predict_top_5 = tf.nn.top_k(scores, k=5)
label_top_5 = tf.nn.top_k(input_y, k=5)
ckpt = tf.train.get_checkpoint_state('models')
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess,ckpt.model_checkpoint_path)
global_step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
编辑:如果无法更改保存全局步骤的方式,则以下操作应有效:
global_step = tf.get_default_graph().get_tensor_by_name('global_step:0')
您可以这样做:
# to create
global_step = tf.Variable(tf.constant(0), trainable=False, name='global_step')
tf.add_to_collection('global_step', global_step)
# to load
global_step = tf.get_collection_ref('global_step')[0]
# get the current value
gs = sess.run(global_step)
with tf.Session() as sess:
predict_top_5 = tf.nn.top_k(scores, k=5)
label_top_5 = tf.nn.top_k(input_y, k=5)
ckpt = tf.train.get_checkpoint_state('models')
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess,ckpt.model_checkpoint_path)
global_step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])