Modify layer parameters in Keras

后端 未结 2 1422
佛祖请我去吃肉
佛祖请我去吃肉 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:55

    Another solution is to again set the attributes of layer. For instance if someone wants to change the kernel initializer of convolutional layers, below is the small example:

    img_input = tf.keras.Input(shape=(256,256,1)) 
    
    x = tf.keras.layers.Conv2D(64, (7, 7), padding='same', use_bias=False, kernel_initializer=None,name='conv')(img_input) 
    
    model = tf.keras.Model(inputs=[img_input], outputs=[x], name='resnext') 
    for layer in model.layers:
            print(layer.get_config())
    

    Output:

    {'batch_input_shape': (None, 256, 256, 1), 'dtype': 'float32', 'sparse': False, 'name': 'input_1'}
    {'name': 'conv2d', 'trainable': True, 'dtype': 'float32', 'filters': 64, 'kernel_size': (7, 7), 'strides': (1, 1), 'padding': 'same', 'data_format': 'channels_last', 'dilation_rate': (1, 1), 'activation': 'linear', 'use_bias': False, 'kernel_initializer': None, 'bias_initializer': {'class_name': 'Zeros', 'config': {'dtype': 'float32'}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}
     
    

    after setting:

    init1 = tf.keras.initializers.TruncatedNormal() 
        for layer in model.layers:
              if hasattr(layer, 'kernel_initializer'):
                    setattr(layer, 'kernel_initializer', init1)
            
        for layer in model.layers:
              print(layer.get_config())
    

    Output:

    {'batch_input_shape': (None, 256, 256, 1), 'dtype': 'float32', 'sparse': False, 'name': 'input_1'}
    {'name': 'conv2d', 'trainable': True, 'dtype': 'float32', 'filters': 64, 'kernel_size': (7, 7), 'strides': (1, 1), 'padding': 'same', 'data_format': 'channels_last', 'dilation_rate': (1, 1), 'activation': 'linear', 'use_bias': False, 'kernel_initializer': {'class_name': 'TruncatedNormal', 'config': {'mean': 0.0, 'stddev': 0.05, 'seed': None, 'dtype': 'float32'}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {'dtype': 'float32'}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}
    

    The kernel initializer has been set

提交回复
热议问题