如何在每个时期后为自定义激活功能输出可学习的参数?

时间:2019-05-15 17:47:45

标签: python keras parameters output activation

我通过在下面编写我自己的图层在Keras中定义了一个带有可训练参数的自定义激活函数,并且想知道是否存在一种简单的方法来在每个时期之后输出此可训练参数。我知道Keras具有回调功能,可以让我获得权重和东西,但是我不确定此可训练参数存储在哪里。我在下面包含了我的代码,对您的帮助将不胜感激!

class CustomLayer(Layer):
    def __init__(self, **kwargs):
        super(CustomLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        self.kernel = self.add_weight(name='kernel', 
                                      shape=(input_shape[1], 1),
                                      initializer='uniform',
                                      trainable=True)
        super(CustomLayer, self).build(input_shape)

    def call(self, x):
        h1 = K.relu(x)
        h2 = K.relu(-x)
        return h1*self.kernal - h2*(1 - self.kernel)

    def compute_output_shape(self, input_shape):
        return input_shape

custom_activation=get_custom_objects().update({'custom_activation': 
Activation(CustomLayer)})

# Create the model
model = Sequential()
model.add(Conv2D(192, (5, 5), input_shape=(3, 32, 32), padding='same', 
activation=custom_activation, kernel_constraint=maxnorm(3)))
model.add(Conv2D(160, (1, 1), padding='same', 
activation=custom_activation, kernel_constraint=maxnorm(3)))
model.add(Conv2D(96, (1, 1), padding='same', activation=custom_activation, 
kernel_constraint=maxnorm(3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Conv2D(192, (5, 5), padding='same', 
activation=custom_activation, kernel_constraint=maxnorm(3)))
model.add(Conv2D(192, (1, 1), padding='same', 
activation=custom_activation, kernel_constraint=maxnorm(3)))
model.add(Conv2D(192, (1, 1), padding='same', 
activation=custom_activation, kernel_constraint=maxnorm(3)))
model.add(AveragePooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Conv2D(192, (3, 3), padding='same', 
activation=custom_activation, kernel_constraint=maxnorm(3)))
model.add(Conv2D(192, (1, 1), padding='same', 
activation=custom_activation, kernel_constraint=maxnorm(3)))
model.add(Conv2D(10, (1, 1), padding='same', activation=custom_activation, 
kernel_constraint=maxnorm(3)))
model.add(AveragePooling2D(pool_size=(8, 8)))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))

0 个答案:

没有答案