Keras无法计算Keras自定义层中的参数数量

时间:2017-10-02 03:15:18

标签: tensorflow keras

我正在构建一个具有Tensorflow支持的Keras Custom层。在此之前,如果我在调用函数中使用Tensorflow的conv2d编写Keras层,我想测试一个Convolution2D层是否正常工作。

class Convolutional2D(Layer):
    def __init__(self, filters=None, kernel_size=None, padding='same', activation='linear', strides=(1,1), name ='Conv2D', **kwargs):
        self.filters = filters
        self.kernel_size = kernel_size
        self.padding = padding
        self.activation = activation
        self.strides = strides
        self.name = name
        self.input_spec = [InputSpec(ndim=4)]
        super(Convolutional2D, self).__init__(**kwargs)

    def call(self, input):
        out = tf.layers.conv2d(inputs=input, filters=self.filters, kernel_size=self.kernel_size, strides=self.strides, padding=self.padding, 
        data_format='channels_last')
        return(out)

    def compute_output_shape(self, input_shape):
        batch_size = input_shape[0]
        width = input_shape[1]/self.strides[0]
        height = input_shape[2]/self.strides[1]
        channels = self.filters
        return(batch_size, width, height, channels)

    def get_config(self):
        config = {'filters': self.filters, 'kernel_size': self.kernel_size, 'padding': self.padding, 'activation':self.activation, 'strides':self.strides, 
        'name':self.name}
        base_config = super(Convolutional2D, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

    def build(self, input_shape):
        self.input_spec = [InputSpec(shape=input_shape)]

这可以正确编译,但是当我使用model.summary()时,它不会计算该图层的参数数量。

当我检查模型的参数总数时,我需要做什么,这个数字包括该层的可训练参数数量?

1 个答案:

答案 0 :(得分:0)

我找到了这个问题的答案。

    def build(self, input_shape):
    if self.data_format == 'channels_first':
        channel_axis = 1
    else:
        channel_axis = -1
    if input_shape[channel_axis] is None:
        raise ValueError('The channel dimension of the inputs '
                         'should be defined. Found `None`.')
    input_dim = input_shape[channel_axis]
    kernel_shape = self.kernel_size + (input_dim, self.filters)

    self.kernel = self.add_weight(shape=kernel_shape,
                                  initializer=self.kernel_initializer,
                                  name='kernel',
                                  regularizer=self.kernel_regularizer,
                                  constraint=self.kernel_constraint)
    if self.use_bias:
        self.bias = self.add_weight(shape=(self.filters,),
                                    initializer=self.bias_initializer,
                                    name='bias',
                                    regularizer=self.bias_regularizer,
                                    constraint=self.bias_constraint)
    else:
        self.bias = None
    # Set input spec.
    self.input_spec = InputSpec(ndim=self.rank + 2,
                                axes={channel_axis: input_dim})
    self.built = True

添加权重定义了我在代码中没有完成的参数数量。但这并不妨碍模型的性能。除了无法获得参数数量的事实外,它工作正常。