简体   繁体   English

Keras 密集层在 kaggle 上显示太多参数

[英]Keras Dense layer is showing too many parameters on kaggle

def my_model():
    inputs = keras.Input(shape=(height,width,3))
    x = layers.Conv2D(32,3)(inputs)
    x = layers.BatchNormalization(input_shape=(32,32,3))(x)
    x = keras.activations.tanh(x)
    x = layers.MaxPooling2D(pool_size=(2,2))(x)
    x = layers.Conv2D(filters=64,kernel_size=(5,5),padding="valid")(x)
    x = layers.BatchNormalization()(x)
    x = keras.activations.tanh(x)
    x = layers.Conv2D(filters=128,kernel_size=(3,3),padding="valid")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Flatten()(x)
    x = layers.Dense(64,activation="tanh")(x)
    outputs = layers.Dense(35)(x)
    
    model = keras.Model(inputs=inputs, outputs = x)
    return model
model = my_model()
model.compile(
    loss='categorical_crossentropy',
    optimizer=keras.optimizers.Adam(lr=3e-4),
    metrics=["accuracy"]
)
model.summary()

this results in output:这导致 output:

Model: "functional_11" Model:“功能_11”


Layer (type) Output Shape Param #层(类型)Output 形状参数 #

input_7 (InputLayer) [(None, 200, 200, 3)] 0 input_7 (InputLayer) [(None, 200, 200, 3)] 0


conv2d_18 (Conv2D) (None, 198, 198, 32) 896 conv2d_18 (Conv2D) (无, 198, 198, 32) 896


batch_normalization_18 (Batc (None, 198, 198, 32) 128 batch_normalization_18 (Batc (None, 198, 198, 32) 128


tf_op_layer_Tanh_12 (TensorF [(None, 198, 198, 32)] 0 tf_op_layer_Tanh_12 (TensorF [(None, 198, 198, 32)] 0


max_pooling2d_6 (MaxPooling2 (None, 99, 99, 32) 0 max_pooling2d_6 (MaxPooling2 (无, 99, 99, 32) 0


conv2d_19 (Conv2D) (None, 95, 95, 64) 51264 conv2d_19 (Conv2D) (无, 95, 95, 64) 51264


batch_normalization_19 (Batc (None, 95, 95, 64) 256 batch_normalization_19 (Batc (None, 95, 95, 64) 256


tf_op_layer_Tanh_13 (TensorF [(None, 95, 95, 64)] 0 tf_op_layer_Tanh_13 (TensorF [(None, 95, 95, 64)] 0


conv2d_20 (Conv2D) (None, 93, 93, 128) 73856 conv2d_20 (Conv2D) (无, 93, 93, 128) 73856


batch_normalization_20 (Batc (None, 93, 93, 128) 512 batch_normalization_20 (Batc (None, 93, 93, 128) 512


flatten_6 (Flatten) (None, 1107072) 0 flatten_6(展平)(无,1107072)0


dense_6 (Dense) (None, 64) 70852672 dense_6(密集)(无,64)70852672


dense_7 (Dense) (None, 35) 2275 dense_7(密集)(无,35)2275

Total params: 70,981,859 Trainable params: 70,981,411 Non-trainable params: 448总参数:70,981,859 可训练参数:70,981,411 不可训练参数:448


Here, dense_6 layer with 64 units is showing a huge number of parameters.在这里,具有 64 个单元的dense_6 层显示了大量的参数。 I'm using kaggle, can somebody help me out by pointing out my mistake?我正在使用 kaggle,有人可以通过指出我的错误来帮助我吗?

It is normal, you have a 1,107,072 neurons fully connected to 64 neurons.这很正常,你有 1,107,072 个神经元完全连接到 64 个神经元。 So the number of parameters is equal to:所以参数的数量等于:

  • input_length * output_length + output_length = 1,107,072 * 64 + 64 = 70,852,672.输入长度 * 输出长度 + 输出长度 = 1,107,072 * 64 + 64 = 70,852,672。

If it's too much for your problem, you should reduce the size before the flatten() layer.如果你的问题太大了,你应该在flatten()层之前减小大小。 For instance, use more pooling layers or calculate less filters.例如,使用更多的池化层或计算更少的过滤器。

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

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