如何排除层权重,只保留我想要的权重,并使用keras预测模型



我想提取一些选定层的权重,并将它们保存为一个名为encoded_weight.h5的Hdf5文件,然后预测模型以获得输出。

原始模型中包含了我不需要的所有权重。

model =  Autoencoder(input_shape=x_train.shape[1:])  #this is the original model
model.summary()
layer_name_list = ['dense2048','batch2048','act2048',
'dense1024','batch1024','act1024',
'dense512','batch512','act512']
layer_dict = dict([(layer.name, layer) for layer in model.layers])
for i in (layer_name_list):
layer_name = i
layer_output = layer_dict[layer_name].get_weights()

上面的代码可以得到我想要的权重作为数组列表,但我不知道如何将它们保存为";encoded_weight.h5";这样我就可以在下面的代码中使用它来预测原始模型。

model.load_weights(‘encoded_weight.h5’, by_name=True) 
model.compile(optimizer = Adam(), loss = 'mean_squared_error' , metrics = ['mae'])
z_train = model.predict(x= x_train_z,verbose=2)

您可以使用tensorflow的save_weights方法保存模型的权重。

model.save_weights(
'encoded_weight.h5', overwrite=True, save_format=None, options=None
)

您可以将这些权重加载为

model.load_weights('encoded_weight.h5')

如果要访问各个层的各个权重。你可以做到。

代码:

# A recursive function to get path of dataset element inside the 'encoded_weight.h5'
def traverse_datasets(hdf_file):
def h5py_dataset_iterator(g, prefix=''):
for key in g.keys():
item = g[key]
path = f'{prefix}/{key}'
if isinstance(item, h5py.Dataset): # test for dataset
yield (path, item)
elif isinstance(item, h5py.Group): # test for group (go down)
yield from h5py_dataset_iterator(item, path)
for path, _ in h5py_dataset_iterator(hdf_file):
yield path
import h5py
filename = "encoded_weight.h5"
hf = h5py.File(filename, "r")
for dset in traverse_datasets(hf):
print('Path:', dset)
print(hf[dset])
#     print(np.array(hf[dset]))   # Contains you array
print('-----------------------')

输出:

Path: /conv1d/conv1d/bias:0
<HDF5 dataset "bias:0": shape (64,), type "<f4">
-----------------------
Path: /conv1d/conv1d/kernel:0
<HDF5 dataset "kernel:0": shape (3, 1, 64), type "<f4">
-----------------------
Path: /dense/dense/bias:0
<HDF5 dataset "bias:0": shape (128,), type "<f4">
-----------------------
Path: /dense/dense/kernel:0
<HDF5 dataset "kernel:0": shape (3712, 128), type "<f4">
-----------------------
Path: /dense_1/dense_1/bias:0
<HDF5 dataset "bias:0": shape (5,), type "<f4">
-----------------------
Path: /dense_1/dense_1/kernel:0
<HDF5 dataset "kernel:0": shape (128, 5), type "<f4">
-----------------------

使用此选项,可以使用set_weights方法更新各个层的权重。

我的模型层:

model.layers

输出:

[<tensorflow.python.keras.layers.convolutional.Conv1D at 0x209a3b41e08>,
<tensorflow.python.keras.layers.pooling.MaxPooling1D at 0x209a9e40cc8>,
<tensorflow.python.keras.layers.core.Flatten at 0x209a9e49708>,
<tensorflow.python.keras.layers.core.Dense at 0x209a9e49588>,
<tensorflow.python.keras.layers.core.Dropout at 0x209a9e4fa48>,
<tensorflow.python.keras.layers.core.Dense at 0x209a9e56f08>]

正在更新conv1d层的权重。

代码:

w = [tf.constant(hf['/conv1d/conv1d/kernel:0']),tf.constant(hf['/conv1d/conv1d/bias:0'])]
model.layers[0].set_weights(w)

相关内容

  • 没有找到相关文章

最新更新