修改VGG-16的权重文件(.h5)中的某些值 [英] Modify some values in the weight file (.h5) of VGG-16
问题描述
我将VGG模型每一层的权重和偏差值保存为.h5文件。我从以下文件中获取文件: https://github.com/ fchollet / deep-learning-models / releases / tag / v0.1
I have the weight and bias values for each layer of the VGG model saved as a .h5 file. I got the file from: https://github.com/fchollet/deep-learning-models/releases/tag/v0.1
现在,假设我要更改该文件中的一些值。借助如何使用h5py覆盖h5文件中的数组,我尝试执行以下操作:
Now let's say I want to change a few values in that file. With help from How to overwrite array inside h5 file using h5py, I am trying to do the same as follows:
import h5py
file_name = "vgg.h5"
f = h5py.File(file_name, 'r+')
# List all groups
print("Keys: %s" % f.keys())
# Get the data
data = (f['block2_conv1']['block2_conv1_W_1:0'])
print(data[0][0][0][0]) #prints some value, lets say X
data[0][0][0][0] = 0 #change it to zero
print(data[0][0][0][0]) #prints the same value X
f.close()
即使尝试为该索引分配其他内容,我也得到相同的原始值。
I get the same original value even after trying to assign something else to that index.
我不确定如何更改/修改权重值并将其保存(在同一文件或其他文件)。任何帮助或建议,将不胜感激。谢谢!
I am not sure how to change/modify the weight value and save it (in the same file or maybe a different one). Any help or suggestions on this will be highly appreciated. Thank you!
推荐答案
尝试一下。
data[0,0,0,0]=0
ndarray对象需要以这种方式而不是普通数组进行更新-列表。
ndarray objects need to update in this way rather than normal array-list.
这篇关于修改VGG-16的权重文件(.h5)中的某些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!