2017-04-04 189 views
2

我似乎無法從保存的檢查點檢索global_step。我的代碼:Tensorflow無法從檢查點恢復global_step

//(...) 
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>. 

我也global_step:0和​​但同樣的結果嘗試。有小費嗎?或者我不應該使用tf.get_variable

謝謝

回答

1

你只能使用tf.get_variable如果該變量在首位,tf.get_variable創建檢索現有變量。而且,變量範圍必須適當設置。它似乎在嘗試創建一個名爲'global_step'的新變量,表明它尚不存在。 Here是關於如何使用tf.get_variable的更多信息。

我通常處理這樣的全局步:

# 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) 

編輯:如果你不能改變你保存全局步的方式,下面應該工作:

global_step = tf.get_default_graph().get_tensor_by_name('global_step:0') 
0

可以這樣做:

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]) 
相關問題