[英]Keras Weights Saving
I am working with pretrained Keras model MobileNets. 我正在使用预训练的Keras模型MobileNets。 I am trying to save the weights of one of the layer in a text file.
我正在尝试将一个图层的权重保存在文本文件中。 The dimension of the weight matrix is as follows:
权重矩阵的维数如下:
layerr = model.layers[2].get_weights()
print(layerr.shape)
(1, 3, 3, 3, 32)
I am confused as to which is the 3's corresponds to the channel and which of them corresponds to height and width. 对于哪个3对应于通道,哪个对应于高度和宽度,我感到困惑。 I know that the 32 corresponds to the number of filters.
我知道32对应于过滤器的数量。
Also if you could help me in saving them as a linear matrix, that would be great! 另外,如果您可以帮助我将它们另存为线性矩阵,那将非常好!
Something is strange, get_weights()
should be returning a list, which is not your case in this code. 有点奇怪,
get_weights()
应该返回一个列表,在此代码中情况并非如此。 Anyway, assuming you're picking the correct array from the list, and assuming it's a 3D convolution... (otherwise something is not quite right and I'd ask you to share your exact layer definition). 无论如何,假设您从列表中选择了正确的数组,并假设它是3D卷积...(否则有些事情不太正确,我想请您分享确切的图层定义)。
Sounds like a 3D convolution filter with these numbers in sequence: 听起来像是3D卷积滤波器,其中这些数字按顺序排列:
There are several ways of saving a numpy array. 保存numpy数组有几种方法。 I like
numpy.save()
. 我喜欢
numpy.save()
。
np.save('filename.npy', layerr)
You can also create a text file and save it as text: 您还可以创建一个文本文件并将其另存为文本:
with open('filename.txt', 'w') as f:
f.write(str(layerr))
Not sure about what a "linear matrix" is, but if you want it with only one dimension, you can reshape: 不确定“线性矩阵”是什么,但是如果只需要一个维度,则可以调整形状:
flatWeights = layerr.reshape((-1,))
#then save
But if you're saving for using later, it's better to use model.save()
or model.save_weights()
. 但是,如果要保存以后使用,最好使用
model.save()
或model.save_weights()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.