使用Keras 實現(xiàn)查看model weights .h5 文件的內(nèi)容
Keras的模型是用hdf5存儲的,如果想要查看模型,keras提供了get_weights的函數(shù)可以查看:
for layer in model.layers: weights = layer.get_weights() # list of numpy array
而通過hdf5模塊也可以讀取:hdf5的數(shù)據(jù)結(jié)構(gòu)主要是File - Group - Dataset三級,具體操作API可以看官方文檔。weights的tensor保存在Dataset的value中,而每一集都會有attrs保存各網(wǎng)絡(luò)層的屬性:
import h5py def print_keras_wegiths(weight_file_path): f = h5py.File(weight_file_path) # 讀取weights h5文件返回File類 try: if len(f.attrs.items()): print("{} contains: ".format(weight_file_path)) print("Root attributes:") for key, value in f.attrs.items(): print(" {}: {}".format(key, value)) # 輸出儲存在File類中的attrs信息,一般是各層的名稱 for layer, g in f.items(): # 讀取各層的名稱以及包含層信息的Group類 print(" {}".format(layer)) print(" Attributes:") for key, value in g.attrs.items(): # 輸出儲存在Group類中的attrs信息,一般是各層的weights和bias及他們的名稱 print(" {}: {}".format(key, value)) print(" Dataset:") for name, d in g.items(): # 讀取各層儲存具體信息的Dataset類 print(" {}: {}".format(name, d.value.shape)) # 輸出儲存在Dataset中的層名稱和權(quán)重,也可以打印dataset的attrs,但是keras中是空的 print(" {}: {}".format(name. d.value)) finally: f.close()
而如果想修改某個值,則需要通過新建File類,然后用create_group, create_dataset函數(shù)將信息重新寫入,具體操作可以查看這篇文章
補充知識:keras load model 并保存特定層 (pop) 的權(quán)重save new_model
有時候我們保存模型(save model),會保存整個模型輸入到輸出的權(quán)重,如果,我們不想保存后幾層的參數(shù),保存成新的模型。
import keras from keras.models import Model, load_model from keras.layers import Input, Dense from keras.optimizers import RMSprop import numpy as np
創(chuàng)建原始模型并保存權(quán)重
inputs = Input((1,)) dense_1 = Dense(10, activation='relu')(inputs) dense_2 = Dense(10, activation='relu')(dense_1) dense_3 = Dense(10, activation='relu')(dense_2) outputs = Dense(10)(dense_3) model = Model(inputs=inputs, outputs=outputs) model.compile(optimizer=RMSprop(), loss='mse') model.save('test.h5')
加載模型并對模型進(jìn)行調(diào)整
loaded_model = load_model('test.h5') loaded_model.layers.pop() loaded_model.layers.pop()
此處去掉了最后兩層--dense_3, dense_2。
創(chuàng)建新的model并加載修改后的模型
new_model = Model(inputs=inputs, outputs=dense_1) new_model.compile(optimizer=RMSprop(), loss='mse') new_model.set_weights(loaded_model.get_weights()) new_model.summary() new_model.save('test_complete.h5')
以上這篇使用Keras 實現(xiàn)查看model weights .h5 文件的內(nèi)容就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python3.7在anaconda里面使用IDLE編譯器的步驟詳解
這篇文章主要介紹了Python3.7在anaconda里面使用IDLE編譯器的步驟,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-04-04關(guān)于Python?Tkinter?復(fù)選框?->Checkbutton
這篇文章主要介紹了關(guān)于Python?Tkinter復(fù)選框Checkbutton,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09Pytho爬蟲中Requests設(shè)置請求頭Headers的方法
這篇文章主要介紹了Pytho爬蟲中Requests設(shè)置請求頭Headers的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09python?matplotlib繪畫十一種常見數(shù)據(jù)分析圖
這篇文章主要介紹了python?matplotlib繪畫十一種常見數(shù)據(jù)分析圖,文章主要繪制折線圖、散點圖、直方圖、餅圖等需要的小伙伴可以參考一下文章具體內(nèi)容2022-06-06TensorFlow tf.nn.max_pool實現(xiàn)池化操作方式
今天小編就為大家分享一篇TensorFlow tf.nn.max_pool實現(xiàn)池化操作方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01