代码:
import tensorflow as tf sess = tf.Session() check_point_path = 'variables' saver = tf.train.import_meta_graph('variables/save_variables.ckpt.meta') saver.restore(sess, tf.train.latest_checkpoint(check_point_path)) graph = tf.get_default_graph() #print(graph.get_operations()) #with open('op.txt','a') as f: # f.write(str(graph.get_operations())) op1 = graph.get_tensor_by_name('fully_connected/biases:0') print(op1)
使用函数graph.get_operations()获取ckpt.meta中保存的graph中的所有operation,而tensor_name为'op_name:0'。
然后使用graph.get_tensor_by_name('op_name:0') 获取tensor信息。
代码从ckpt文件中获取保存的variable的数据(tensor的name和value):
import os import tensorflow as tf from tensorflow.python import pywrap_tensorflow check_point_path = 'variables' #checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') ckpt = tf.train.get_checkpoint_state(checkpoint_dir=check_point_path) checkpoint_path = os.path.join('.', ckpt.model_checkpoint_path) #print(ckpt.model_checkpoint_path) reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path) 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))
法二:
from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file print_tensors_in_checkpoint_file("variables/save_variables.ckpt",tensor_name='', all_tensors=False, all_tensor_names=False)
注意:tf.train.latest_checkpoint(check_point_path) 方法用来获取最后一次ckeckpoint的路径,等价于
ckpt = tf.train.get_checkpoint_state(check_point_path) ckpt.model_checkpoint_path
不能将tf.train.latest_checkpoint与tf.train.get_checkpoint_state 搞混了
以上这篇tensorflow 实现从checkpoint中获取graph信息就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“tensorflow 实现从checkpoint中获取graph信息”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。