The “layer_max_pooling_2d” Function in R

  • Package: keras

  • Purpose: Downsample input data by taking the maximum value over a specified window.

  • General class: Layer

  • Required argument(s):

    • pool_size: Size of the pooling window.

  • Notable optional arguments:

    • strides: Stride length of the pooling operation.

    • padding: Padding mode (‘valid’ or ‘same’).

  • Example:

  • # Load the required library
    library(keras)

    # Define a simple neural network model with a 2D convolutional layer and a max pooling layer
    model <- keras_model_sequential() %>%
    layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = 'relu', input_shape = c(28, 28, 1)) %>%
    layer_max_pooling_2d(pool_size = c(2, 2)) %>%
    layer_flatten() %>%
    layer_dense(units = 128, activation = 'relu') %>%
    layer_dense(units = 10, activation = 'softmax')

    # Compile the model
    model %>% compile(
    loss = 'categorical_crossentropy',
    optimizer = optimizer_adam(),
    metrics = c('accuracy')
    )

    # Summary of the model
    summary(model)

  • In this example, the layer_max_pooling_2d function from the keras package is used to add a max pooling layer to the neural network model. The max pooling layer has a pooling window size of 2x2, which reduces the spatial dimensions of the input. This is followed by a flatten layer and two dense layers to perform classification.

Previous
Previous

The “naive_bayes” Function in R

Next
Next

The “layer_conv_2d” Function in R