计算了Keras模型中的不可训练参数参数

时间:2019-08-28 12:22:10

标签: python tensorflow keras

我从互联网上获取了以下程序

def my_model(input_shape):
    # Define the input placeholder as a tensor with shape input_shape. Think of this as your input image!
    X_input = Input(input_shape)

    # Zero-Padding: pads the border of X_input with zeroes
    X = ZeroPadding2D((3, 3))(X_input)

    # CONV -> BN -> RELU Block applied to X
    X = Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0')(X)
    X = BatchNormalization(axis = 3, name = 'bn0')(X)
    X = Activation('relu')(X)

    # MAXPOOL
    X = MaxPooling2D((2, 2), name='max_pool')(X)

    # FLATTEN X (means convert it to a vector) + FULLYCONNECTED
    X = Flatten()(X)
    X = Dense(1, activation='sigmoid', name='fc')(X)

    # Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
    model = Model(inputs = X_input, outputs = X, name='myModel')

    return model

mymodel = my_model((64,64,3))
mymodel.summary()

摘要的输出如下所示

Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         (None, 64, 64, 3)         0         
_________________________________________________________________
zero_padding2d_3 (ZeroPaddin (None, 70, 70, 3)         0         
_________________________________________________________________
conv0 (Conv2D)               (None, 64, 64, 32)        4736      
_________________________________________________________________
bn0 (BatchNormalization)     (None, 64, 64, 32)        128       
_________________________________________________________________
activation_2 (Activation)    (None, 64, 64, 32)        0         
_________________________________________________________________
max_pool (MaxPooling2D)      (None, 32, 32, 32)        0         
_________________________________________________________________
flatten_2 (Flatten)          (None, 32768)             0         
_________________________________________________________________
fc (Dense)                   (None, 1)                 32769     
=================================================================
Total params: 37,633
Trainable params: 37,569
Non-trainable params: 64

我的问题是,该不可训练参数是从哪一层获取的,即64。 另一个问题是批处理规范化如何具有参数128?

请求您的帮助,我们如何从上面定义的模型中获得上述数字。感谢您的时间和帮助。

1 个答案:

答案 0 :(得分:1)

BatchNormalization层由[gamma weights, beta weights, moving_mean(non-trainable), moving_variance(non-trainable)]组成,对于每个参数,最后一个轴中的每个元素都有一个值(默认情况下在keras中,但是您可以根据需要更改轴)。

在代码中,您在BatchNormalization层之前的最后一个维度中的尺寸为32,因此 32 * 4 = 128个参数,并且由于存在 2个不可训练的参数,因此存在32 * 2 = 64个不可训练的参数