Tensorflow1.x 和 2.x如何读取ckpt中保存了那些参数
生活随笔
收集整理的這篇文章主要介紹了
Tensorflow1.x 和 2.x如何读取ckpt中保存了那些参数
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
對(duì)于1.x的tf版本,網(wǎng)上已經(jīng)有很多介紹了,這里給出代碼:
import tensorflow as tf import os from tensorflow.python import pywrap_tensorflowmodel_dir='./model'#設(shè)置模型所在文件夾 checkpoint_path = os.path.join(model_dir, "fineturing_model.ckpt")#定位ckpt文件 # 從checkpoint中讀出數(shù)據(jù) reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path) # reader = tf.train.NewCheckpointReader(checkpoint_path) # 用tf.train中的NewCheckpointReader方法 var_to_shape_map = reader.get_variable_to_shape_map() # 輸出權(quán)重tensor名字和值 for key in var_to_shape_map:print("tensor_name: ", key,reader.get_tensor(key).shape)對(duì)于tensorflow2.0來(lái)說(shuō),這個(gè)代碼是不能正常運(yùn)行的,會(huì)報(bào)類似:
module 'tensorflow.python.pywrap_tensorflow' has no attribute 'NewCheckpointReader'
的錯(cuò)誤,這就是版本的問(wèn)題,有些博主建議降低tf版本,但是這有點(diǎn)代價(jià)太大,下面給出tf2.0的正確使用方式;
import tensorflow.compat.v1 as tf1tf1.disable_v2_behavior() checkpoint_path = 'xxx.ckpt' # Read data from checkpoint file reader = tf1.train.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).shape)總結(jié)
以上是生活随笔為你收集整理的Tensorflow1.x 和 2.x如何读取ckpt中保存了那些参数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: tensorflow实现梯度累计,再回传
- 下一篇: 【C++】字符串中运算符的重载问题