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')
以上这篇使用Keras 实现查看model weights .h5 文件的内容就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]