2016-11-21 181 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:指示是否打印所有张量的布尔值。