Modify layer parameters in Keras

后端 未结 2 1420
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-20 20:24

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 w

2条回答
  •  心在旅途
    2021-01-20 20:47

    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()
    

    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()
    

    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
    _________________________________________________________________
    

    You can also modify the output of model.to_json() directly without modifying the model instance.


    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)
    

    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.

提交回复
热议问题