Tensorflow如何从checkpoint文件中提取特定tensor值?
- 内容介绍
- 文章标签
- 相关推荐
本文共计250个文字,预计阅读时间需要1分钟。
在使用预训练模型时,我们需要从检查点文件中恢复变量。经常遇到的问题是检查点中找不到Tensor name not found。这时,需要检查ckpt文件中到底有哪些变量。可以使用以下代码:
pythonimport osfrom tensorflow.python import pywrap_tensorflow
def list_variables(tensor_name): with tf.Session() as sess: reader=pywrap_tensorflow.NewCheckpointReader(os.path.join(model_dir, 'checkpoint')) var_to_shape_map=reader.get_variable_to_shape_map() print(Variables in checkpoint:) for key in var_to_shape_map: print(key)
list_variables(Tensor_name)
在使用pre-train model时候,我们需要restore variables from checkpoint files.
经常出现在checkpoint 中找不到”Tensor name not found”.
这时候需要查看一下ckpt中到底有哪些变量
import os from tensorflow.python import pywrap_tensorflow checkpoint_path = os.path.join(model_dir, "model.ckpt") # Read data from checkpoint file reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path) var_to_shape_map = reader.get_variable_to_shape_map() # Print tensor name and values for key in var_to_shape_map: print("tensor_name: ", key) print(reader.get_tensor(key))
可以显示ckpt中的tensor名字和值,当然也可以用pycharm调试。
以上这篇Tensorflow: 从checkpoint文件中读取tensor方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
本文共计250个文字,预计阅读时间需要1分钟。
在使用预训练模型时,我们需要从检查点文件中恢复变量。经常遇到的问题是检查点中找不到Tensor name not found。这时,需要检查ckpt文件中到底有哪些变量。可以使用以下代码:
pythonimport osfrom tensorflow.python import pywrap_tensorflow
def list_variables(tensor_name): with tf.Session() as sess: reader=pywrap_tensorflow.NewCheckpointReader(os.path.join(model_dir, 'checkpoint')) var_to_shape_map=reader.get_variable_to_shape_map() print(Variables in checkpoint:) for key in var_to_shape_map: print(key)
list_variables(Tensor_name)
在使用pre-train model时候,我们需要restore variables from checkpoint files.
经常出现在checkpoint 中找不到”Tensor name not found”.
这时候需要查看一下ckpt中到底有哪些变量
import os from tensorflow.python import pywrap_tensorflow checkpoint_path = os.path.join(model_dir, "model.ckpt") # Read data from checkpoint file reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path) var_to_shape_map = reader.get_variable_to_shape_map() # Print tensor name and values for key in var_to_shape_map: print("tensor_name: ", key) print(reader.get_tensor(key))
可以显示ckpt中的tensor名字和值,当然也可以用pycharm调试。
以上这篇Tensorflow: 从checkpoint文件中读取tensor方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

