tensorflow 實(shí)現(xiàn)從checkpoint中獲取graph信息
代碼:
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)
使用函數(shù)graph.get_operations()獲取ckpt.meta中保存的graph中的所有operation,而tensor_name為'op_name:0'。
然后使用graph.get_tensor_by_name('op_name:0') 獲取tensor信息。
代碼從ckpt文件中獲取保存的variable的數(shù)據(jù)(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的路徑,等價(jià)于
ckpt = tf.train.get_checkpoint_state(check_point_path) ckpt.model_checkpoint_path
不能將tf.train.latest_checkpoint與tf.train.get_checkpoint_state 搞混了
以上這篇tensorflow 實(shí)現(xiàn)從checkpoint中獲取graph信息就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
numpy array找出符合條件的數(shù)并賦值的示例代碼
本文主要介紹了numpy array找出符合條件的數(shù)并賦值的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05一文搞懂Python讀取text,CSV,JSON文件的方法
文件處理是一種用于創(chuàng)建文件、寫入數(shù)據(jù)和從中讀取數(shù)據(jù)的過程,Python 擁有豐富的用于處理不同文件類型的包,從而使得我們可以更加輕松方便的完成文件處理的工作,本文將來為大家詳細(xì)講講2022-06-06基于Python實(shí)現(xiàn)評(píng)論區(qū)抽獎(jiǎng)功能詳解
這篇文章主要為大家介紹了基于Python如何實(shí)現(xiàn)評(píng)論區(qū)抽獎(jiǎng)的功能,文章的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的朋友可以學(xué)習(xí)一下2021-12-12python+pandas+時(shí)間、日期以及時(shí)間序列處理方法
今天小編就為大家分享一篇python+pandas+時(shí)間、日期以及時(shí)間序列處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07Python?設(shè)計(jì)模式中的創(chuàng)建型建造者模式
本文介紹Python設(shè)計(jì)模式中的創(chuàng)建型建造者模式,其表現(xiàn)為復(fù)雜對(duì)象的創(chuàng)建與表現(xiàn)相分離,這樣,同一個(gè)過程就有不同的表現(xiàn),想要?jiǎng)?chuàng)建一個(gè)由多個(gè)部分組成的對(duì)象,而且它的構(gòu)成需要一步接一步的完成。只有當(dāng)各個(gè)部分都完成了,這個(gè)對(duì)象才完整,下文相關(guān)自來哦,需要的朋友可以參考下2022-02-02python函數(shù)指定默認(rèn)值的實(shí)例講解
在本篇內(nèi)容里小編給大家整理了一篇關(guān)于python函數(shù)指定默認(rèn)值的實(shí)例講解內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)參考下。2021-03-03