Keras用面膜冻结比重 [英] Keras freeze specific weights with mask
问题描述
我是Keras的新手.我想实现一个并非所有权重都会更新的图层.例如,在下面的代码中,我希望dilation
层将以某些中心权重永远不会更新的方式进行更新.例如,dilation
层中每个特征矩阵的形状(1024个中的一个)为448, 448
,并且所有特征矩阵中心的8x8
块将永远不会更新,即8x8
块为功能矩阵的(不可训练的)蒙版.
I am new in Keras. I want to implement a layer where not all the weights will update. For example, in the following code, I want the dilation
layer will update in a way that some center weights are never updated. For say, the shape of each feature matrix (out of 1024) in the dilation
layer is 448, 448
and a block of 8x8
at the center of all feature matrices will never be updated, i.e. the 8x8
block is a (non-trainable) mask to the feature matrices.
input_layer=Input(shape=(896,896,3))
new_layer = Conv2D(32, kernel_size=(3,3), padding="same", activation='relu', kernel_initializer='he_normal')(input_layer)
new_layer = MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same', data_format=None)(new_layer)
new_layer = Conv2D(64, kernel_size=(3,3), padding='same', activation='relu', kernel_initializer='he_normal')(new_layer)
new_layer = Conv2D(1024, kernel_size=(7,7), dilation_rate=8, padding="same", activation='relu', kernel_initializer='he_normal', name='dialation')(new_layer)
new_layer = Conv2D(32, kernel_size=(1,1), padding="same", activation='relu', kernel_initializer='he_normal')(new_layer)
new_layer = Conv2D(32, kernel_size=(1,1), padding="same", activation='relu', kernel_initializer='he_normal')(new_layer)
model = Model(input_layer, new_layer)
我正在尝试Keras的custom layer
[link] ,但是我很难理解.任何人都请帮忙.
I was trying with the Keras's custom layer
[link], but it was difficult for me to understand. Anyone would please help.
更新: 我添加了下图以更好地理解.膨胀层包含1024个要素.我希望每个功能的中间区域都是不可训练的(静态).
UPDATE: I added the following figure for a better understanding. The dilation layer contains 1024 features. I want the middle region of each feature to be non-trainable (static).
推荐答案
在两种情况下均使用此掩码:
Use this mask for both cases:
mask = np.zeros((1,448,448,1))
mask[:,220:228,220:228] = 1
替换部分功能
如果您用恒定值替换部分特征,则表示该特征将是静态的,但仍将参与反向传播(因为权重仍将乘以该部分图像并相加,并且存在连接)
Replacing part of the feature
If you replace part of the feature with constant values, this means the feature will be static, but it will still participate in backpropagation (because weights will still be multiplied and summed for this part of the image and there is a connection)
constant = 0 (will annulate kernel, but not bias)
def replace(x):
return x*(1-mask) + constant*mask
#before the dilation layer
new_layer=Lambda(replace)(new_layer)
保留特征值,但停止向后传播
此处,扩张层的权重以及进一步的权重将正常更新,但是扩张层之前的权重将不会受到中心区域的影响.
Keeping the feature value, but stopping backpropagation
Here, the weights of the dilation layer and further will be updated normally, but the weights before the dilation layer will not receive the influence of the central region.
def stopBackprop(x):
stopped=K.stop_gradients(x)
return x*(1-mask) + stopped*mask
#before the dilation layer
new_layer=Lambda(stopBackprop)(new_layer)
这篇关于Keras用面膜冻结比重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!