Tensorflow 讀取ckpt文件中的tensor操作
在使用pre-train model時(shí)候,我們需要restore variables from checkpoint files.
經(jīng)常出現(xiàn)在checkpoint 中找不到”Tensor name not found”.
這時(shí)候需要查看一下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名字和值,當(dāng)然也可以用pycharm調(diào)試。
補(bǔ)充:tensorflow中讀取模型中保存的值, tf.train.NewCheckpointReader
使用tf.trian.NewCheckpointReader(model_dir)
一個(gè)標(biāo)準(zhǔn)的模型文件有一下文件, model_dir就是MyModel(沒(méi)有后綴)
checkpoint Model.meta Model.data-00000-of-00001 Model.index
import tensorflow as tf import pprint # 使用pprint 提高打印的可讀性 NewCheck =tf.train.NewCheckpointReader("model")
打印模型中的所有變量
print("debug_string:\n") pprint.pprint(NewCheck.debug_string().decode("utf-8"))
其中有3個(gè)字段, 分別是名字, 數(shù)據(jù)類型, shape
獲取變量中的值
print("get_tensor:\n") pprint.pprint(NewCheck.get_tensor("D/conv2d/bias"))
print("get_variable_to_dtype_map\n") pprint.pprint(NewCheck.get_variable_to_dtype_map()) print("get_variable_to_shape_map\n") pprint.pprint(NewCheck.get_variable_to_shape_map())
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
pytest用例執(zhí)行順序和跳過(guò)執(zhí)行詳解
本文主要介紹了pytest用例執(zhí)行順序和跳過(guò)執(zhí)行詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python解決%matplotlib inline標(biāo)紅底報(bào)錯(cuò)問(wèn)題
在使用非Jupyter環(huán)境如Spyder或PyCharm時(shí),%matplotlib inline會(huì)因?yàn)槭荍upyter特有的魔法命令而導(dǎo)致報(bào)錯(cuò),這條命令是用于Jupyter Notebook或Jupyter Qt Console中,主要作用是將matplotlib的圖表直接嵌入到Notebook中顯示2024-09-09python ndarray數(shù)組對(duì)象特點(diǎn)及實(shí)例分享
在本篇文章里小編給大家分享的是一篇關(guān)于python ndarray數(shù)組對(duì)象特點(diǎn)及實(shí)例相關(guān)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。2021-10-10python如何實(shí)現(xiàn)Dice系數(shù)
這篇文章主要介紹了python如何實(shí)現(xiàn)Dice系數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Python3.5編程實(shí)現(xiàn)修改IIS WEB.CONFIG的方法示例
這篇文章主要介紹了Python3.5編程實(shí)現(xiàn)修改IIS WEB.CONFIG的方法,涉及Python針對(duì)xml格式文件的讀寫以及節(jié)點(diǎn)操作相關(guān)技巧,需要的朋友可以參考下2017-08-08解決tensorflow打印tensor有省略號(hào)的問(wèn)題
今天小編就為大家分享一篇解決tensorflow打印tensor有省略號(hào)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02