tensorflow 实现从checkpoint中获取graph信息

作者:qq_28808697 时间:2023-01-05 09:38:41 

代码:


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 搞混了

来源:https://blog.csdn.net/qq_28808697/article/details/79884309

标签:tensorflow,checkpoint,graph
0
投稿

猜你喜欢

  • SQL Server 2008打开输入sa密码提示无法登陆数据库的解决方法

    2024-01-29 09:39:04
  • Python爬虫爬取微信朋友圈

    2021-11-12 17:38:14
  • 基于jquery自己写tab滑动门(通用版)

    2024-04-22 22:21:09
  • JavaScript利用多彩线条摆出心形效果的示例代码

    2024-04-16 10:29:49
  • 从创建数据库到存储过程与用户自定义函数的小感

    2024-01-16 23:42:05
  • 基于Django框架的权限组件rbac实例讲解

    2022-09-27 17:11:51
  • Spring boot 和Vue开发中CORS跨域问题解决

    2024-04-28 09:32:54
  • 基于vue实现微博三方登录流程解析

    2024-05-03 15:08:42
  • golang正则之命名分组方式

    2024-05-22 10:17:10
  • sql server 临时表 查找并删除的实现代码

    2024-01-17 18:49:56
  • 详解Node.js读写中文内容文件操作

    2024-05-03 15:55:07
  • python 等差数列末项计算方式

    2022-01-29 10:12:57
  • mysql5.7.19 安装配置方法图文教程(win10)

    2024-01-16 03:52:10
  • 如何取得MYSQL中ENUM列的全部可能值

    2009-07-30 08:53:00
  • PyTorch中view()与 reshape()的区别详析

    2023-11-16 05:45:23
  • 可以举出一个最简单的计数器吗?

    2009-11-01 15:37:00
  • vue项目两种方式实现竖向表格的思路分析

    2024-06-05 15:31:13
  • python库-dotenv包 及 .env配置文件详解

    2023-09-08 20:22:38
  • pycharm new project变成灰色的解决方法

    2023-10-29 20:29:23
  • go语言算法题解二叉树的拷贝、镜像和对称

    2024-04-28 10:45:18
  • asp之家 网络编程 m.aspxhome.com