在 Keras 中修改图层参数 [英] Modify layer parameters in Keras
问题描述
我对更新 Keras 中现有的图层参数感兴趣(不是删除图层并插入一个新图层,而是修改现有参数).
I am interested in updating existing layer parameters in Keras (not removing a layer and inserting a new one instead, rather just modifying existing parameters).
我将举一个我正在编写的函数的例子:
I will give an example of a function I'm writing:
def add_filters(self, model):
conv_indices = [i for i, layer in enumerate(model.layers) if 'convolution' in layer.get_config()['name']]
random_conv_index = random.randint(0, len(conv_indices)-1)
factor = 2
conv_layer = model.layers[random_conv_index]
conv_layer.filters = conv_layer.filters * factor
print('new conv layer filters after transform is:', conv_layer.filters)
print('just to make sure, its:', model.layers[random_conv_index].filters)
return model
所以这里基本上发生的事情是我从我的网络中随机抽取一个卷积层(我所有的卷积层的名称中都有卷积")并尝试将过滤器加倍.据我所知,这在任何情况下都不会导致输入/输出大小兼容性的任何编译问题".
so what's basically happening here is me taking a random convolutional layer from my network (all my conv layers have 'convolution' in their name) and trying to double the filters. As far as I know this shouldn't cause any 'compilation issues' with input/output size compatibility in any case.
问题是,我的模型根本没有改变.我最后添加的 2 个打印输出打印了正确的数字(是之前过滤器数量的两倍).但是当我编译模型并打印model.summary()时,还是看到了之前的过滤量.
The thing is, my model doesn't change at all. The 2 print-outs I added in the end print the correct number (double the previous amount of filters). But when I compile the model and print model.summary(), I still see the previous filter amount.
顺便说一句,我并不局限于 Keras.例如,如果有人知道如何使用 PyTorch 实现这一点,我也会购买它:D
BTW, I'm not constricted to Keras. If anyone has an idea how to pull this off with PyTorch for example I'll also buy it :D
推荐答案
好吧,如果您想基于现有模型创建新模型的架构,尽管进行一些修改,您可以使用 to_json
和 model_from_json()
函数.下面是一个例子:
Well, if you would like to create the architecture of a new model based on an existing model, though with some modifications, you can use to_json
and model_from_json()
functions. Here is an example:
model = Sequential()
model.add(Conv2D(10, (3,3), input_shape=(100,100,3)))
model.add(Conv2D(40, (3,3)))
model.summary()
模型摘要:
Layer (type) Output Shape Param #
=================================================================
conv2d_12 (Conv2D) (None, 98, 98, 10) 280
_________________________________________________________________
conv2d_13 (Conv2D) (None, 96, 96, 40) 3640
=================================================================
Total params: 3,920
Trainable params: 3,920
Non-trainable params: 0
_________________________________________________________________
现在我们修改第一层的过滤器数量,并基于修改后的架构创建一个新模型:
Now we modify the number of filters of the first layer and create a new model based on the modified architecture:
from keras.models import model_from_json
model.layers[0].filters *= 2
new_model = model_from_json(model.to_json())
new_model.summary()
新模型摘要:
Layer (type) Output Shape Param #
=================================================================
conv2d_12 (Conv2D) (None, 98, 98, 20) 560
_________________________________________________________________
conv2d_13 (Conv2D) (None, 96, 96, 40) 7240
=================================================================
Total params: 7,800
Trainable params: 7,800
Non-trainable params: 0
_________________________________________________________________
也可以直接修改model.to_json()
的输出,无需修改模型实例.
You can also modify the output of model.to_json()
directly without modifying the model instance.
您可以轻松地使用 get_weights()
方法来获取卷积层的当前权重.它将返回两个 numpy 数组的列表.第一个对应于过滤器权重,第二个对应于偏置参数.然后你可以使用 set_weights()
方法来设置新的权重:
You can easily use get_weights()
method to get the current weights of the convolution layer. It would return a list of two numpy arrays. The first one corresponds to filter weights and the second one corresponds to bias parameters. Then you can use set_weights()
method to set the new weights:
conv_layer = model.layers[random_conv_index]
weights = conv_layer.get_weights()
weights[0] *= factor # multiply filter weights by `factor`
conv_layer.set_weights(weights)
作为旁注,您在代码中使用的卷积层的 filters
属性对应于该层中过滤器的数量,而不是它们的权重.
As a side note, the filters
attribute of a convolution layer which you have used in your code corresponds to the number of filters in this layer and not their weights.
这篇关于在 Keras 中修改图层参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!