简体   繁体   English

Keras 中的特定丢失

[英]Specific Dropout in Keras

I would like to train an autoencoder by using only specific PARTS of a layer (the layer named FEATURES in the autoencoder example at the bottom of this question).我想通过仅使用层的特定部分来训练自动编码器(此问题底部的自动编码器示例中名为 FEATURES 的层)。

In my case, NOK pictures for a new product are very rare, but needed for training.就我而言,新产品的 NOK 图片非常罕见,但需要培训。 The aim is generate NOK pictures from OK pictures (all examples I found did the opposite).目的是从 OK 图片生成 NOK 图片(我发现的所有示例都相反)。 The idea is to force learning OK-picture structure in features[0:nx] and learning NOK-picture structure (maybe from a similiar product) in features[nx:n] in order to use the NOK-features as parameters to generate NOK-pictures from OK-pictures.这个想法是强制在 features[0:nx] 中学习 OK-picture 结构并在 features[nx:n] 中学习 NOK-picture 结构(可能来自类似的产品),以便使用 NOK-features 作为参数来生成 NOK - 图片来自 OK-pictures。

Two ideas came to my mind using a non-random dropout我想到了两个使用非随机辍学的想法

(1) keras.layers.Dropout(rate, noise_shape=None, seed=None) has the noise_shape argument, but I am not sure if it helps me as it only describes the shape. (1) keras.layers.Dropout(rate, noise_shape=None, seed=None)有 noise_shape 参数,但我不确定它是否对我有帮助,因为它只描述了形状。 It would be perfect to be able to provide a mask consisting of {0,1} to apply on the layer in order to switch on/off specific nodes能够提供由 {0,1} 组成的掩码以应用于图层以打开/关闭特定节点是完美的

(2) creating a custom layer (named MaskLayer below) which performs masking specific nodes of the layer eg as a tuple of {0,1}. (2)创建一个自定义层(下面命名为 MaskLayer),它执行屏蔽层的特定节点,例如作为 {0,1} 的元组。

I have read this , but I do not think it applies (generate a layer by concatenating layers which can be freezed separately).我已阅读内容,但我认为它不适用(通过连接可以单独冻结的图层来生成图层)。

def autoEncGenerate0( imgSizeX=28, imgSizeY=28, imgDepth=1):  ####:
    ''' keras blog autoencoder'''
    input_img = Input(shape=(imgSizeX, imgSizeY, imgDepth)) 
    x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
    x = MaxPooling2D((4, 4), padding='same')(x)
    x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
    encoded0 = MaxPooling2D((8, 8), padding='same', name="FEATURES")(x) 
    encoded1 = MaskLayer(mask)(encoded0) # TO BE DONE (B2) masking layer parts
    x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded1)
    x = UpSampling2D((8, 8))(x)
    x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
    x = UpSampling2D((4, 4))(x)
    decoded = Conv2D( imgDepth, (3, 3), activation='sigmoid', padding='same')(x)
    autoencoder = Model(input_img, decoded)
    autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
    return( autoencoder)

Thanks for hints.感谢您的提示。

There is trainable attribute that each instance of tf.keras.layer.Layer has which disables training of the variables of that layer. tf.keras.layer.Layer的每个实例都具有trainable属性,该属性禁用该层变量的训练。 UpSampling2D doesn't have any variables so you CAN'T train it. UpSampling2D没有任何变量,因此您无法对其进行训练。 What you want is to train the variables of the convolutional layer that comes before that upsampling layer.你想要的是训练上采样层之前的卷积层的变量。

You could do it like this:你可以这样做:

# define architecture here
autoencoder = Model(input_img, decoded)
layers_names = [l.name for l in autoencoder.layers]
trainable_layer_index = layers_names.index('FEATURES') - 1
for i in range(len(autoencoder.layers)):
    if i != trainable_layer_index:
        autoencoder.layers[i].trainable = False
# compile here

NOTE that you compile the model AFTER you set layers to trainable/non-trainable.请注意,在将层设置为可训练/不可训练之后编译 model。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM