使用Keras 实现查看model weights .h5 文件的内容
作者:weixin_33671935 时间:2023-10-18 03:07:16
Keras的模型是用hdf5存储的,如果想要查看模型,keras提供了get_weights的函数可以查看:
for layer in model.layers: weights = layer.get_weights() # list of numpy array
而通过hdf5模块也可以读取:hdf5的数据结构主要是File - Group - Dataset * ,具体操作API可以看官方文档。weights的tensor保存在Dataset的value中,而每一集都会有attrs保存各网络层的属性:
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中的层名称和权重,也可以打印dataset的attrs,但是keras中是空的
print(" {}: {}".format(name. d.value))
finally:
f.close()
而如果想修改某个值,则需要通过新建File类,然后用create_group, create_dataset函数将信息重新写入,具体操作可以查看这篇文章
补充知识:keras load model 并保存特定层 (pop) 的权重save new_model
有时候我们保存模型(save model),会保存整个模型输入到输出的权重,如果,我们不想保存后几层的参数,保存成新的模型。
import keras
from keras.models import Model, load_model
from keras.layers import Input, Dense
from keras.optimizers import RMSprop
import numpy as np
创建原始模型并保存权重
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')
加载模型并对模型进行调整
loaded_model = load_model('test.h5')
loaded_model.layers.pop()
loaded_model.layers.pop()
此处去掉了最后两层--dense_3, dense_2。
创建新的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')
来源:https://blog.csdn.net/weixin_33671935/article/details/85956459
标签:Keras,model,weights,h5
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Python中使用Mako模版库的简单教程
2021-11-08 12:33:45
MS SQL Server中的CONVERT日期格式化大全
2010-08-07 11:31:00
seatunnel 2.3.1全流程部署使用教程
2022-02-01 02:38:51
在 Jupyter 中重新导入特定的 Python 文件(场景分析)
2021-01-30 01:16:57
![](https://img.aspxhome.com/file/2023/1/120311_0s.jpg)
使用documentElement正确取得当前可见区域的大小
2024-04-18 09:34:06
Go语言struct要使用 tags的原因解析
2023-08-31 09:25:19
![](https://img.aspxhome.com/file/2023/6/93666_0s.jpg)
我放弃Python转Go语言的9大理由(附优秀书籍推荐)
2022-05-27 04:46:17
![](https://img.aspxhome.com/file/2023/6/86296_0s.jpg)
php动态生成函数示例
2024-05-02 17:19:00
python访问mysql数据库的实现方法(2则示例)
2024-01-23 05:58:41
matplotlib.pyplot画图并导出保存的实例
2022-10-02 13:32:26
使用Python中的pytesseract模块实现抓取图片中文字
2021-05-06 18:10:28
![](https://img.aspxhome.com/file/2023/0/125890_0s.png)
简单介绍Python的Django框架的dj-scaffold项目
2021-11-01 07:18:05
网页模式化窗口
2008-04-27 20:52:00
![](/images/sg_trans.gif)
一行CSS代码为网站加上奥运主题
2008-07-20 12:33:00
![](https://img.aspxhome.com/file/UploadPic/20087/20/2008720123826347s.png)
Python爬虫获取基金净值信息详情
2022-04-23 07:53:42
![](https://img.aspxhome.com/file/2023/3/127013_0s.webp)
DataGrid使用心得(调用及连接数据库等等)
2024-01-23 17:31:15
![](https://img.aspxhome.com/file/2023/7/82207_0s.jpg)
python中Task封装协程的知识点总结
2022-10-25 13:25:02
pyenv与virtualenv安装实现python多版本多项目管理
2022-12-19 23:50:04
![](https://img.aspxhome.com/file/2023/8/112108_0s.png)
Python中使用sklearn进行特征降维的方法
2021-09-20 06:23:50
![](https://img.aspxhome.com/file/2023/9/118009_0s.png)
对Python random模块打乱数组顺序的实例讲解
2023-03-24 16:51:57
![](https://img.aspxhome.com/file/2023/1/68401_0s.jpg)