2016-11-21 178 views
0

在我從ckpt文件恢復變量的值之前,我需要在恢復變量的文件中創建這些變量。然而,在這個新文件中,可能有一些其他變量不在ckpt文件中。是否有可能只打印一個恢復的變量列表(在這種情況下tf.all_variables不起作用)?TensorFlow中恢復的變量列表

+0

你能澄清你在新文件中究竟做了什麼嗎?你是否添加新變量並再次保存? – Neal

+0

在這裏,我只想獲得保存在給定ckpt文件中的變量列表。 – cerebrou

回答

1

如果你想要的是可以保存的變量列表,我相信你可以使用這個:

tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) + tf.get_collection(tf.GraphKeys.SAVEABLE_OBJECTS)

2

您可以使用inspect_checkpoint.py工具列出你的關卡要恢復的變量。

from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file 

# List ALL tensors. 
print_tensors_in_checkpoint_file(file_name='./model.ckpt', tensor_name='') 

# List contents of a specific tensor. 
print_tensors_in_checkpoint_file(file_name='./model.ckpt', tensor_name='conv1_w') 

另一種方法:

from tensorflow.python import pywrap_tensorflow 
reader = pywrap_tensorflow.NewCheckpointReader('./model.ckpt') 
var_to_shape_map = reader.get_variable_to_shape_map() 
for key in var_to_shape_map: 
    print("tensor_name: ", key) 
    print(reader.get_tensor(key)) # Remove this is you want to print only variable names 

列表從當前圖形中所有的全局變量:

for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES): 
    print(v) 

希望它能幫助。

0

使用 print_tensors_in_checkpoint_file(file_name, tensor_name, all_tensors)

參數數量: FILE_NAME:檢查點文件的名稱。 tensor_name:要打印的檢查點文件中張量的名稱。 all_tensors:指示是否打印所有張量的布爾值。